2个RecyclerView 联动
生活随笔
收集整理的這篇文章主要介紹了
2个RecyclerView 联动
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
看下效果圖, 底部在給出demo地址
如果想實現類似拼多多的商品聯動也是可以的,修改方法即可 下面看下實現方法
幾個參數需要知道
findFirstVisibleItemPosition 顯示界面第一個position
findLastVisibleItemPosition 顯示界面最后一個position
scrollToPositionWithOffset 跳轉到指定位置
具體代碼如下
public class MainActivity extends AppCompatActivity {private RecyclerView top_recy;RecyclerViewAdapter topAdapter;List<Integer> topList = new ArrayList<>();private RecyclerView bottom_recy;RecyclerViewAdapter bottomAdapter;List<Integer> bottomList = new ArrayList<>();int currentPosition = 1;private int[] img ={R.mipmap.one,R.mipmap.ele,R.mipmap.two,R.mipmap.et,R.mipmap.three,R.mipmap.nine,R.mipmap.four,R.mipmap.seven,R.mipmap.five,R.mipmap.ten};@RequiresApi(api = Build.VERSION_CODES.M)@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);top_recy = findViewById(R.id.hor_recy);bottom_recy = findViewById(R.id.hor_max_recy);for(int i=0;i<img.length;i++){topList.add(img[i]);}top_recy.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));topAdapter = new RecyclerViewAdapter(this,R.layout.hot_item_layout,topList);top_recy.setAdapter(topAdapter);topAdapter.setCallBack(new RecyclerViewAdapter.CallBack() {@Overridepublic <T> void convert(IViewHolder holder, T bean, int position) {ImageView horImageView = (ImageView) holder.getView(R.id.hor_item_img);Glide.with(MainActivity.this).load(bean).into(horImageView);}});top_recy.addOnScrollListener(new RecyclerView.OnScrollListener() {@Overridepublic void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {super.onScrollStateChanged(recyclerView, newState);//獲得recyclerView的線性布局管理器LinearLayoutManager topManager = (LinearLayoutManager) top_recy.getLayoutManager();LinearLayoutManager bottomManager = (LinearLayoutManager) bottom_recy.getLayoutManager();//獲取滾動時的第一條展示的positioncurrentPosition = topManager.findFirstVisibleItemPosition();//打這些log 是為了查看下表,剛開始話滑動不好,就是下表出現了問題,配圖的時候注意一下呢Log.i("-----firsttop", String.valueOf(currentPosition));Log.i("------firstbottom", String.valueOf(bottomManager.findFirstVisibleItemPosition()));Log.i("------lastbottom", String.valueOf(bottomManager.findLastVisibleItemPosition()));if (bottomManager.findFirstVisibleItemPosition()>=currentPosition){//跳到并行位置,一般商城類商品類跳轉頂部的使用這個方法
// bottomManager.scrollToPositionWithOffset(currentPosition,0);bottom_recy.smoothScrollToPosition(currentPosition);}else if (bottomManager.findLastVisibleItemPosition() < currentPosition) {
// bottomManager.scrollToPositionWithOffset(currentPosition, 0);bottom_recy.smoothScrollToPosition(currentPosition);}bottomAdapter.notifyDataSetChanged();}});for(int i=0;i<img.length;i++){bottomList.add((img[i]));}bottom_recy.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));bottomAdapter = new RecyclerViewAdapter(this,R.layout.max_item_layout,bottomList);bottom_recy.setAdapter(bottomAdapter);bottomAdapter.setCallBack(new RecyclerViewAdapter.CallBack() {@Overridepublic <T> void convert(IViewHolder holder, T bean, int position) {ImageView maxImage = (ImageView) holder.getView(R.id.max_image);Glide.with(MainActivity.this).load(bean).into(maxImage);}});bottomAdapter.setOnItemClickListner(new RecyclerViewAdapter.OnItemClickListner() {@Overridepublic void onItemClickListner(View v, int position) {LinearLayoutManager linearLayoutManager = (LinearLayoutManager) top_recy.getLayoutManager();linearLayoutManager.scrollToPositionWithOffset(position,0); // 定位到某個item,并將其置頂顯示}});findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(MainActivity.this,RecyActivity.class));}});findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {startActivity(new Intent(MainActivity.this,GridActivity.class));}});}
}
?布局適配器就不寫了,?
demo地址,如果幫助了您希望給個star
?
總結
以上是生活随笔為你收集整理的2个RecyclerView 联动的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2013年起亚狮跑11月份的车能卖多少钱
- 下一篇: Android webview 加载的h