赞
踩
项目中要实现无限循环自动滚动的轮播图,自然而然想到了RecyclerView,但我们常用的RecyclerView是不支持无限循环滚动的,所以就需要一些办法让它能够无限循环。
public class MainActivity extends AppCompatActivity { private RecyclerView mTipInfoRv; private TextView mTextView; private int showPageIndex; private boolean dragging = false; private Handler mCyclicRollHandler = new Handler(); Runnable cycleRollRunabler = new Runnable() { @Override public void run() { if (showPageIndex + 1 >= 5) { mTipInfoRv.scrollToPosition(0); showPageIndex = 0; } else { showPageIndex += 1; mTipInfoRv.smoothScrollToPosition(showPageIndex); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTipInfoRv = findViewById(R.id.rv_Tipinfo); mTextView = findViewById(R.id.textview); final LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); mTipInfoRv.setLayoutManager(layoutManager); MainAdapter tMainAdapter = new MainAdapter(this); mTipInfoRv.setAdapter(tMainAdapter); //用于辅助RecyclerView在滚动结束时将Item对齐到某个位置 PagerSnapHelper snapHelper = new PagerSnapHelper(); snapHelper.attachToRecyclerView(mTipInfoRv); mTipInfoRv.addOnScrollListener(new RecyclerView.OnScrollListener(
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。