在我的2列交错网格中,我使用无限列表的效果自动按10乘10加载元素,除了向上滚动时,其他所有东西都工作正常。前两个元素更改其位置。就像前两个项目从一列跳到另一列一样。
如我所读,适配器必须保存每个元素的状态。但是,我不知道该怎么做。
这是我用来显示StaggeredGrid功能的主要代码
public class SpacesItemDecoration extends RecyclerView.ItemDecoration { private int space; public SpacesItemDecoration(int space) { this.space = space; } @Override public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { outRect.left = space; outRect.right = space; outRect.bottom = space+1; // Add top margin only for the first item to avoid double space between items if(parent.getChildLayoutPosition(view) == 0) outRect.top = space; } } public class SolventViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener { public TextView vh_name; public TextView vh_price; public ImageView vh_image; public SolventViewHolders(View v) { super(v); v.setOnClickListener(this); vh_image=(ImageView)v.findViewById(R.id.thumbnail); vh_name= (TextView) v.findViewById(R.id.title); vh_price= (TextView) v.findViewById(R.id.subtitle); } public void sendImage(String url) { Picasso.with(ctx) .load(url) .into(vh_image); } @Override public void onClick(View view) { int position = getPosition(); if (user_id != null) { openDetail(MyLogs.get(position).getId_item(), MyLogs.get(position).getItemName(), MyLogs.get(position).getPrice(), MyLogs.get(position).getDescrip(), MyLogs.get(position).getId_store(), MyLogs.get(position).getId_store()); } } } public class SolventRecyclerViewAdapter extends RecyclerView.Adapter{ private List itemList; private Context context; public SolventRecyclerViewAdapter(Context context, List itemList) { this.itemList = itemList; this.context = context; } @Override public SolventViewHolders onCreateViewHolder(ViewGroup parent, int viewType) { View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_mosaico_defoult, null); SolventViewHolders rcv = new SolventViewHolders(layoutView); return rcv; } @Override public void onBindViewHolder(SolventViewHolders holder, int position) { holder.vh_name.setText(itemList.get(position).getName()); holder.vh_price.setText("CLP:"+itemList.get(position).getPrice()); } @Override public int getItemCount() { return this.itemList.size(); } }
#编辑1:
这就是我实例化适配器的方式
recyclerView = (RecyclerView)view.findViewById(R.id.recycler_view); recyclerView.setHasFixedSize(true); gaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, 1); recyclerView.setLayoutManager(gaggeredGridLayoutManager); recyclerView.addItemDecoration(new SpacesItemDecoration(2)); populateList(currentPage); rcAdapter= new SolventRecyclerViewAdapter(ctx, MyLogs); recyclerView.setAdapter(rcAdapter);
populate方法作为json格式的asyntask响应发生:
... for(int i=0; i < lengthJsonArr; i++) { JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); ... MyLogs.add("name,price,etc"); rcAdapter.notifyDataSetChanged(); }
这就是我保持onScroll的方式:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { visibleItemCount = gaggeredGridLayoutManager.getChildCount(); totalItemCount = gaggeredGridLayoutManager.getItemCount(); int[] firstVisibleItems = null; firstVisibleItems = gaggeredGridLayoutManager.findFirstVisibleItemPositions(firstVisibleItems); if (firstVisibleItems != null && firstVisibleItems.length > 0) { pastVisiblesItems = firstVisibleItems[0]; } if (loading) { if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) { loading= false; Log.d("tag", "LOAD NEXT ITEM"); currentPage = currentPage + 1; populateList(currentPage); } } } });
感谢您的帮助
这是一个功能。重新排序和重新定位视图以实现无缝的项目组合。
使用 setGapStrategy(int gapStrategy)
设置StaggeredGridLayoutManager的间隙处理策略。如果gapStrategy参数不同于当前策略,则调用此方法将触发布局请求。
您可能会想要GAP_HANDLING_NONE
。
没有做任何事情来掩盖差距。