1

有人可以帮助我我在滚动 GridView 时遇到问题我所做的是我从 API 显示 GridView 中的前 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>
4

3 回答 3

1

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

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

然后在调用该hitApi()方法后,将旧位置设置回来:

yourList.setSelectionFromTop(i, t);
于 2016-11-05T07:21:52.120 回答
0

尝试这个...

    /***
     * 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;
        }
    }
于 2016-11-05T07:00:03.510 回答
-1

在您的适配器中尝试这样的操作以获得最后一个可见位置

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;
   }

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

于 2016-11-05T06:36:22.190 回答