2012-08-27 11 views
6

sto facendo un app in cui devo aggiungere finestra di avanzamento nel piè di pagina View, ma sono in grado di ottenere qualsiasi finestra di avanzamento in vista piè di pagina:Usa finestra di avanzamento nel piè di pagina

attività principale:

I desidera aggiungere finestra di avanzamento nel piè di pagina in questa classe

public class MainActivity extends Activity implements OnScrollListener { 

// All variables 
XmlParser parser; 
Document doc; 
String xml; 
ListView lv; 
ListViewAdapter adapter; 
ArrayList<HashMap<String, String>> menuItems; 
ProgressDialog pDialog; 

private String URL = "http://api.androidhive.info/list_paging/?page=1"; 

// XML node keys 
static final String KEY_ITEM = "item"; // parent node 
static final String KEY_ID = "id"; 
static final String KEY_NAME = "name"; 
ProgressDialog dialog; 
// Flag for current page 
int current_page = 1; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    lv = (ListView) findViewById(R.id.list); 

    menuItems = new ArrayList<HashMap<String, String>>(); 

    parser = new XmlParser(); 
    xml = parser.getXmlFromUrl(URL); // getting XML 
    doc = parser.getDomElement(xml); // getting DOM element 

    NodeList nl = doc.getElementsByTagName(KEY_ITEM); 
    // looping through all item nodes <item> 
    for (int i = 0; i < nl.getLength(); i++) { 
     // creating new HashMap 
     HashMap<String, String> map = new HashMap<String, String>(); 
     Element e = (Element) nl.item(i); 
     // adding each child node to HashMap key => value 
     map.put(KEY_ID, parser.getValue(e, KEY_ID)); // id not using any where 
     map.put(KEY_NAME, parser.getValue(e, KEY_NAME)); 

     // adding HashList to ArrayList 
     menuItems.add(map); 
    } 

    // LoadMore button 
    dialog=new ProgressDialog(this); 
    // Button btnLoadMore = new Button(this); 
    //btnLoadMore.setText("Load More"); 

    // Adding Load More button to lisview at bottom 
    // lv.addFooterView(dialog); 
// I want to use Progress Dialog in footer 
    /* lv.addFooterView(dialog);*/ 

    // Getting adapter 
    adapter = new ListViewAdapter(this, menuItems); 
    lv.setAdapter(adapter); 
    lv.setOnScrollListener(this); 
    lv.addFooterView(dialog.getListView()); 

    /** 
    * Listening to Load More button click event 
    * */ 
    if(dialog.isShowing()) 
    { 

    } 
    /* btnLoadMore.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View arg0) { 
      // Starting a new async task 
      new loadMoreListView().execute(); 
     } 
    });*/ 

    /** 
    * Listening to listview single row selected 
    * **/ 
    lv.setOnItemClickListener(new OnItemClickListener() { 


     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 
      // getting values from selected ListItem 
      String name = ((TextView) view.findViewById(R.id.name)) 
        .getText().toString(); 

      // Starting new intent 
      Intent in = new Intent(getApplicationContext(), 
        Test123.class); 
      in.putExtra(KEY_NAME, name); 
      startActivity(in); 
     } 
    }); 
} 

/** 
* Async Task that send a request to url 
* Gets new list view data 
* Appends to list view 
* */ 
private class loadMoreListView extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     // Showing progress dialog before sending http request 
     if(dialog.isShowing()) 
     { 
     dialog.cancel();  
     } 
     else 
     { 
     pDialog = new ProgressDialog(
       MainActivity.this); 
     pDialog.setMessage("Please wait.."); 
     pDialog.setIndeterminate(true); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
     } 
    } 

    protected Void doInBackground(Void... unused) { 
     runOnUiThread(new Runnable() { 
      public void run() { 
       // increment current page 
       current_page += 1; 

       // Next page request 
       URL = "http://api.androidhive.info/list_paging/?page=" + current_page; 

       xml = parser.getXmlFromUrl(URL); // getting XML 
       doc = parser.getDomElement(xml); // getting DOM element 

       NodeList nl = doc.getElementsByTagName(KEY_ITEM); 
       // looping through all item nodes <item> 
       for (int i = 0; i < nl.getLength(); i++) { 
        // creating new HashMap 
        HashMap<String, String> map = new HashMap<String, String>(); 
        Element e = (Element) nl.item(i); 

        // adding each child node to HashMap key => value 
        map.put(KEY_ID, parser.getValue(e, KEY_ID)); 
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME)); 

        // adding HashList to ArrayList 
        menuItems.add(map); 
       } 

       // get listview current position - used to maintain scroll position 
       int currentPosition = lv.getFirstVisiblePosition(); 

       // Appending new data to menuItems ArrayList 
       adapter = new ListViewAdapter(
         MainActivity.this, 
         menuItems); 
       lv.setAdapter(adapter); 
       // Setting new scroll position 
       lv.setSelectionFromTop(currentPosition + 1, 0); 

      } 
     }); 

     return (null); 
    } 

    protected void onPostExecute(Void unused) { 
     // closing progress dialog 
     pDialog.dismiss(); 
    } 
} 

public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) { 
    // TODO Auto-generated method stub 
    dialog.show(); 
    lv.setOnScrollListener(this); 
    lv.addFooterView(dialog.getListView()); 
    new loadMoreListView().execute(); 
} 

public void onScrollStateChanged(AbsListView arg0, int arg1) { 
    // TODO Auto-generated method stub 
    new loadMoreListView().execute(); 
} 
} 

Adapter:

public class ListViewAdapter extends BaseAdapter { 

private Activity activity; 
private ArrayList<HashMap<String, String>> data; 
private static LayoutInflater inflater=null; 

public ListViewAdapter(Activity a, ArrayList<HashMap<String, String>> d) { 
    activity = a; 
    data=d; 
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

public int getCount() { 
    return data.size(); 
} 

public Object getItem(int position) { 
    return position; 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    View vi=convertView; 
    if(convertView==null) 
     vi = inflater.inflate(R.layout.list_item, null); 

    TextView name = (TextView)vi.findViewById(R.id.name); 

    HashMap<String, String> item = new HashMap<String, String>(); 
    item = data.get(position); 

    //Setting all values in listview 
    name.setText(item.get("name")); 
    return vi; 
} 
} 

XmlParser

public class XmlParser { 

// constructor 
public XmlParser() { 

} 


public String getXmlFromUrl(String url) { 
    String xml = null; 

    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     xml = EntityUtils.toString(httpEntity); 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // return XML 
    return xml; 
} 

/** 
* Getting XML DOM element 
* @param XML string 
* */ 
public Document getDomElement(String xml){ 
    Document doc = null; 
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
    try { 

     DocumentBuilder db = dbf.newDocumentBuilder(); 

     InputSource is = new InputSource(); 
      is.setCharacterStream(new StringReader(xml)); 
      doc = db.parse(is); 

     } catch (ParserConfigurationException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (SAXException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } catch (IOException e) { 
      Log.e("Error: ", e.getMessage()); 
      return null; 
     } 

     return doc; 
} 

/** Getting node value 
    * @param elem element 
    */ 
public final String getElementValue(Node elem) { 
    Node child; 
    if(elem != null){ 
     if (elem.hasChildNodes()){ 
      for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){ 
       if(child.getNodeType() == Node.TEXT_NODE ){ 
        return child.getNodeValue(); 
       } 
      } 
     } 
    } 
    return ""; 
} 

/** 
    * Getting node value 
    * @param Element node 
    * @param key string 
    * */ 
public String getValue(Element item, String str) { 
     NodeList n = item.getElementsByTagName(str); 
     return this.getElementValue(n.item(0)); 
    } 
} 

Qualsiasi aiuto sarà apprezzato.

risposta

1

si può provare questo:

ProgressDialog myDialog = ProgressDialog.show(MyActivity.this, "Display Information","atthe bottom...", true); 
myDialog.getWindow().setGravity(Gravity.BOTTOM); 
+0

Voglio visualizzare la finestra di dialogo sullo scorrimento solo nella risposta –

1

È possibile scrivere un semplice controllo personalizzato/arricchito con azioni speciali e comportamenti. Ad esempio, un sistema di menu completo su questa discussione https://stackoverflow.com/a/11805044/1290995

Un layout avanzato è molto più semplice del menu link sopra. Alcuni punti per fare un progressDialog come finestra:

  • Usa FrameLayout come vista radice
  • design in un file di layout XML
  • gonfiare in una sottoclasse di FrameLayout (in Constructor)
  • fornire alcune metodo come show(), hide(), stop() o qualsiasi altra cosa necessaria e implementa tali azioni
  • Passare un layout al controllo personalizzato da utilizzare come genitore come

    public void addTo(ViewGroup viewgroup){ 
        viewgroup.addView(this); 
    } 
    

e molto altro ancora ...

Se avete bisogno di ulteriori informazioni fatemi sapere.

12

credo, si vuole un ProgressBar e non ProgressDialog

Aggiungi un nuovo layout di pb_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <ProgressBar 
     android:id="@+id/progressBar1" 
     style="?android:attr/progressBarStyleSmall" 
     android:indeterminate="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

</LinearLayout> 

Nel codice aggiungere

view = getLayoutInflater().inflate(R.layout.pb_layout, null); 
TextView tv = (TextView)view.findViewById(R.id.textView1); 
tv.setText("Please wait.."); 
ProgressBar pb = (ProgressBar)view.findViewById(R.id.progressBar1); 
pb.setIndeterminate(true); 
lv.addFooterView(view); 

Si può solo fare addFooterView prima di fare setadapter.

+0

che funziona davvero. –

Problemi correlati