滚动位置未保存在 Gridview 中

Scroll position is not saved in Grdiview

有人可以帮我吗 我在 GridView 的滚动中遇到问题我所做的是在 GridView 中显示 API 的前 10 个产品。现在我需要的是 - 当用户滚动并且获取列表的最后位置然后 API 再次点击页面值 2 .. 滚动显示从用户所在的当前位置开始.. 现在显示数据并且适配器更改其值.. 但是滚动显示在当新的 API 出现时顶部...有人可以告诉我要做什么吗???非常感谢,请帮助..

我做什么 -

        grid.setOnScrollListener(new AbsListView.OnScrollListener() {
        int last_item;

        @Override
        public void onScrollStateChanged(AbsListView view, int   scrollState) {

        }

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


            int lastitem = firstVisibleItem + visibleItemCount;
            if(lastitem == totalItemCount){

                       HitApi();//This is the method where API hits with the new page value


            }


        }
    });

xml 文件

       <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"

       xmlns:android="http://schemas.android.com/apk/res/android">


        <GridView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/products_GridView"
        android:numColumns="auto_fit"
        android:listSelector="@android:color/transparent"
        android:verticalSpacing="1dp"
        android:horizontalSpacing="1dp"
        >
       </GridView>


        </LinearLayout>

在你的适配器中尝试这样的事情来获得最后可见的位置

public class GridAdapter extends BaseAdapter { 
  ...
  int previouslyDisplayedPosition = -1;
  ...

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
     if(previouslyDisplayedPosition != position) {
      ....

      previouslyDisplayedPosition = position;
     }

     return convertedView;
   }

只是一个想法。不确定它是否有效。解决方法

试试这个...

    /***
     * LodeMore Variables
     */
    private int lastVisibleItem, totalItemCount;
    private int visibleThreshold = 1;
    private int index = 1;
    private boolean loading;

 private void initLodeMoreAdapter()
    {

        if (recyclerView.getLayoutManager() instanceof GridLayoutManager)
        {

            final GridLayoutManager linearLayoutManager = (GridLayoutManager) rvResponse.getLayoutManager();
            rvResponse.addOnScrollListener(new RecyclerView.OnScrollListener()
            {

                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy)
                {
                    super.onScrolled(recyclerView, dx, dy);


                    totalItemCount = linearLayoutManager.getItemCount();
                    lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                    if (!loading &&totalItemCount <= (lastVisibleItem + visibleThreshold))
                    {
                        // End has been reached
                        // Do something
            loading = true;
                        getResponseListing();
                    }
                }
            });
        }
    }

 private void getResponseListing()
    {
        if (utility.isNetworkAvailable())
        {
        API Call...

        adapter.notifyDataSetChanged();

        loading = false;
        }
    }

好吧,假设 hitApi() 用新项目更新 ListView,那么该方法可能会在当前列表上调用 .clear(),这将导致问题,然后它将添加新项目;你在这里必须做的是在 hitApi() 方法之前添加这个以保持最后一个位置:

int i = yourList.getFirstVisiblePosition();
View v = yourList.getChildAt(0);
int t = 0;
if(v!=null)v.getTop();

然后调用hitApi()方法后,设置回原来的位置:

yourList.setSelectionFromTop(i, t);