web-dev-qa-db-fra.com

liste dynamique en ajoutant "Charger plus d'éléments" à la fin du défilement

J'ai un listview qui récupère les données de la base de données sqlite par Json.

Je veux le transformer en listview dynamique qui, à la fin du défilement, affiche un "chargement d’éléments supplémentaires" dans le pied de page de la liste lors du chargement d’autres éléments et de leur ajout à l’adaptateur (par exemple, 10 éléments à chaque fois). J'ai des problèmes pour implémenter cette fonctionnalité. Je vous prie aider moi avec ceci. Merci.

public class AllProductsActivity extends Activity {

... defining variables...;

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

    new LoadAllProducts().execute();

}

class LoadAllProducts extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        ... progress dialog...
    }

    protected String doInBackground(String... args) {
        countryList = new ArrayList<Country>();
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

        Log.d("All Products: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);
                    String description = c.getString(TAG_DESCRIPTION);
                    String due_date = c.getString(TAG_DUE_DATE);
                    String staff = c.getString(TAG_STAFF);
                    String status = c.getString(TAG_STATUS);

                    country = new Country(id,name,description,
                            due_date, staff, status);
                    countryList.add(country);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {

                displayListView();

            }
        });
    }
}

private void displayListView() {
    dataAdapter = new MyCustomAdapter(this,
            R.layout.country_info, countryList);
    ListView listView = (ListView) findViewById(R.id.listView1);
    listView.setAdapter(dataAdapter);
}

private class MyCustomAdapter extends ArrayAdapter<Country> {

    ... blah blah blah ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        Log.v("ConvertView", String.valueOf(position));
        if (convertView == null) {
        ... blah blah blah ...
        }

        return convertView;
    }
}
}
48
Namikaze Minato

vous essayez le code suivant

list.setOnScrollListener(new OnScrollListener() {

        public void onScrollStateChanged(AbsListView view, int scrollState) {


        }

        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {

            if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
            {
                if(flag_loading == false)
                {
                    flag_loading = true;
                    additems();
                }
            }
        }
    });

dans la additem(), ajoutez les 10 éléments suivants à la liste de tableaux et définissez le paramètre flag_loading sur false. et appelez notifydatasetchanged. ça devrait marcher.

93
null pointer

Il y a la solution, l'activité doit implémenter l'interface AbsListView.OnScrollListener

En activité:

int currentFirstVisibleItem = 0;
int currentVisibleItemCount = 0;
int totalItemCount = 0;
int currentScrollState = 0;
boolean loadingMore = false;
Long startIndex = 0L;
Long offset = 10L;
View footerView;



@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    this.currentFirstVisibleItem = firstVisibleItem;
    this.currentVisibleItemCount = visibleItemCount;
    this.totalItemCount = totalItemCount;
}

@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
    this.currentScrollState = scrollState;
    this.isScrollCompleted();
}

private void isScrollCompleted() {
    if (this.currentVisibleItemCount > 0 && this.currentScrollState == SCROLL_STATE_IDLE && this.totalItemCount == (currentFirstVisibleItem + currentVisibleItemCount)) {
        /*** In this way I detect if there's been a scroll which has completed ***/
        /*** do the work for load more date! ***/
        if (!loadingMore) {
            loadingMore = true;
            new LoadMoreItemsTask(this).execute();
        }
    }
}



private class LoadMoreItemsTask extends AsyncTask<Void, Void, List<ListItem>> {

    private Activity activity;
    private View footer;

    private LoadMoreItemsTask(Activity activity) {
        this.activity = activity;
        loadingMore = true;
        footer = activity.getLayoutInflater().inflate(R.layout.base_list_item_loading_footer, null);
    }

    @Override
    protected void onPreExecute() {
        list.addFooterView(footer);
        list.setAdapter(adapter);
        super.onPreExecute();
    }

    @Override
    protected List<ListItem> doInBackground(Void... voids) {

        return getNextItems(startIndex, offset);
    }

    @Override
    protected void onPostExecute(List<ListItem> listItems) {
        if (footer != null) {
               list.removeFooterView(footer);
        }
        list.setAdapter(adapter);

        loadingMore = false;
        if (listItems.size() > 0) {
            startIndex = startIndex + listItems.size();
            setItems(listItems);
        }
        super.onPostExecute(listItems);
    }


}

Oncreate:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.base_list);
    list = (ListView) findViewById(R.id.list);
    list.setOnItemClickListener(this);
    list.setOnScrollListener(this);


        footerView = ((LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.base_list_item_loading_footer, null, false);



    initAdapter();


    new LoadMoreItemsTask(this).execute();

}

base_list_item_loading_footer.xml

<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:id="@+id/list_item_footer"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:gravity="center"
    Android:orientation="vertical"
    Android:padding="15dp" >

    <LinearLayout
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:layout_gravity="center"
        Android:orientation="horizontal" >

        <ProgressBar
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content" >
        </ProgressBar>

        <TextView
            style="@style/ItemHeadTextStyle"
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:layout_gravity="center"
            Android:text="@string/loading_list_items" />
    </LinearLayout>
</RelativeLayout> 

j'espère que ça aide.

14
Richard Key

Meilleure façon de mettre en œuvre Dynamic Expandaple LW avec le pied de page expliqué ci-dessous:

private int firstVisibleItem, visibleItemCount,totalItemCount;

expandapleListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScroll(AbsListView view,
                                 int firstVisibleItemm, int visibleItemCountt,
                                 int totalItemCountt) {
                firstVisibleItem = firstVisibleItemm;
                visibleItemCount = visibleItemCountt;
                totalItemCount = totalItemCountt;


            }

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                final int lastItem = firstVisibleItem + visibleItemCount;
                if (lastItem == totalItemCount && scrollState == SCROLL_STATE_IDLE) {
                    expandapleInt++;

                    new AsyncTask().execute();

                    //get next 10-20 items(your choice)items

                }
            }
        });

Mise en œuvre du pied de page:

Ci-dessous le code XML, devrait être en bas de la mise en page que votre ListView à l'intérieur ...

 <RelativeLayout
Android:layout_width="match_parent"
Android:layout_height="45dp"
Android:layout_gravity="center"
Android:id="@+id/footer">

<ProgressBar
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_alignParentTop="true"
    Android:layout_toLeftOf="@+id/textView3"
    Android:layout_toStartOf="@+id/textView3">
</ProgressBar>

<TextView

    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"

    Android:text="Loading"
    Android:layout_centerVertical="true"
    Android:layout_centerHorizontal="true"
    Android:id="@+id/textView3" />

Liez-le dans votre activité

footer = (RelativeLayout) findViewById(R.id.footer);
footer.setVisibility(View.GONE);

onPreExecute

footer.setVisibility(View.VISIBLE);

onPostExecute

footer.setVisibility(View.GONE);
8
Beyaz