简体   繁体   中英

how to update Listview while at the end of Listview scrolling in Android?

I'm trying to display the listview from sqlite. At the end of Listview I want to load next 10 data from sqlite and this process will continue till data ends in SQLite. But the issue is data load at first time successfully and when i scroll 3rd time page/data does not load and after LocalListPageIndex= 2 then progress bar is continuously running.

Here is my Code of ListView Scrolling.

 listView.removeFooterView(progressBar);
    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

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

            lastInScreen = firstVisibleItem + visibleItemCount;

            if (X != firstVisibleItem) {
                if (LocalListPageIndex <= 5) {
                    Toast.makeText(getApplicationContext(), "Your Last Item." + lastInScreen, Toast.LENGTH_SHORT).show();

                    if (lastInScreen == totalItemCount) {

                        listView.addFooterView(progressBar);

                        // Execute some code after 15 seconds have passed
                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable()
                        {
                            public void run()
                            {
                                LocalListPageIndex += 1;

                                int OFFSET_SCROLL = 10
                                List<All_Post> allDesc = dbhelper.getAllDescriptions(OFFSET_SCROLL);
                                for (All_Post all_Post : allDesc) {
                                    descArray.add(all_Post);
                                }
                                if (adapter != null) {
                                    adapter.notifyDataSetChanged();
                                   // adapter = new AllPostAdapter(getApplicationContext(), R.layout.allpostlist, descArray);
                                    listView.setAdapter(adapter);
                                    //listView.invalidateViews();
                                    listView.setSelection(totalItemCount);

                                }
                            }
                        }, 15000);
                    }

                }   if (LocalListPageIndex == 5)  {
                    Log.e("hide footer", "footer hide");
                    listView.removeFooterView(progressBar);
                }
            }
            X = firstVisibleItem;

        }
    });

使用无限加载更多listview来加载数据,请检查此URL, https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews

You could try using RecyclerView: The RecyclerView widget is a more advanced and flexible version of ListView. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views. Use the RecyclerViewwidget when you have data collections whose elements change at runtime based on user action or network events.

A layout manager positions item views inside a RecyclerView and determines when to reuse item views that are no longer visible to the user. To reuse (or recycle) a view, a layout manager may ask the adapter to replace the contents of the view with a different element from the dataset. Recycling views in this manner improves performance by avoiding the creation of unnecessary views or performing expensivefindViewById() lookups Example:

Add this view to your layout:

android.support.v7.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

Once you have added a RecyclerView widget to your layout, obtain a handle to the object, connect it to a layout manager, and attach an adapter for the data to be displayed:

public class MyActivity extends Activity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_activity);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);
        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        // specify an adapter (see also next example)
        // Set your myDataSet with the url of your images.
        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    }
    ...
}

Create an adapter to manage the recycler view:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private MySQLClass mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public ViewHolder(TextView v) {
            super(v);
            textView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(MySQLClass myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                               .inflate(R.layout.my_image_view, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ...
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        textView.setText(mDataset.get(i));

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

info from:

http://developer.android.com/training/material/lists-cards.html

当您从数据库中检索数据时,最好使用装载程序。装载程序将根据您的查询装载所有详细信息,并以Cursor的形式提供给您,现在您只需将光标提供给CursorAdapter(如果您使用的是Cursor适配器),则可以将数据从光标提取到列表中,然后将该列表提供给普通适配器。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM