RecyclerView+PagerSnapHelper如何实现抖音首页翻页Viewpager效果
这篇文章主要为大家展示了“RecyclerView+PagerSnapHelper如何实现抖音首页翻页Viewpager效果”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“RecyclerView+PagerSnapHelper如何实现抖音首页翻页Viewpager效果”这篇文章吧。
创新互联公司专注于鸡东网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供鸡东营销型网站建设,鸡东网站制作、鸡东网页设计、鸡东网站官网定制、微信小程序开发服务,打造鸡东网络公司原创品牌,更为您提供鸡东网站排名全网营销落地服务。
RecyclerView + PagerSnapHelper 实现抖音首页翻页的Viewpager效果,供大家参考,具体内容如下
先来个效果
实现方式
PagerSnapHelperActivity.java
public class PagerSnapHelperActivity extends Activity { /** * UI */ // recycleView private RecyclerView mRecyclerView = null; // adapter private PagerSnapHelperAdapter mMyadapter = null; /** * 数据 */ //data private ArrayListmDataList = new ArrayList (); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.recycle_pager_activity); // -----------创建数据集------------- for (int i = 1; i < 100; i++) { mDataList.add("item" + i); } // 纵向List initUI(); } public void initUI() { // ---RecyclerView--- mRecyclerView = (RecyclerView) findViewById(R.id.recyclerview_vertical); mRecyclerView.setNestedScrollingEnabled(false); // PagerSnapHelper PagerSnapHelper snapHelper = new PagerSnapHelper() { // 在 Adapter的 onBindViewHolder 之后执行 @Override public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) { // TODO 找到对应的Index Log.e("xiaxl: ", "---findTargetSnapPosition---"); int targetPos = super.findTargetSnapPosition(layoutManager, velocityX, velocityY); Log.e("xiaxl: ", "targetPos: " + targetPos); Toast.makeText(PagerSnapHelperActivity.this, "滑到到 " + targetPos + "位置", Toast.LENGTH_SHORT).show(); return targetPos; } // 在 Adapter的 onBindViewHolder 之后执行 @Nullable @Override public View findSnapView(RecyclerView.LayoutManager layoutManager) { // TODO 找到对应的View Log.e("xiaxl: ", "---findSnapView---"); View view = super.findSnapView(layoutManager); Log.e("xiaxl: ", "tag: " + view.getTag()); return view; } }; snapHelper.attachToRecyclerView(mRecyclerView); // ---布局管理器--- LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this); // 默认是Vertical (HORIZONTAL则为横向列表) linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); // mRecyclerView.setLayoutManager(linearLayoutManager); // TODO 这么写是为了获取RecycleView的宽高 mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { mRecyclerView.getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } /** * 这么写是为了获取RecycleView的宽高 */ // 创建Adapter,并指定数据集 mMyadapter = new PagerSnapHelperAdapter(mDataList, mRecyclerView.getWidth(), mRecyclerView.getHeight()); // 设置Adapter mRecyclerView.setAdapter(mMyadapter); } }); } }
recycle_pager_activity.xml
PagerSnapHelperAdapter.java
public class PagerSnapHelperAdapter extends RecyclerView.Adapter{ // 数据集 private ArrayList mDataList; private int mWidth; private int mHeight; // public PagerSnapHelperAdapter(ArrayList dataset, int width, int height) { super(); this.mDataList = dataset; // mWidth = width; mHeight = height; } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { Log.e("xiaxl: ", "---onCreateViewHolder---"); // 创建一个View,简单起见直接使用系统提供的布局,就是一个TextView View view = View.inflate(viewGroup.getContext(), R.layout.recycle_pager_item, null); View contentView = view.findViewById(R.id.add_btn); RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) contentView.getLayoutParams(); rl.width = mWidth; rl.height = mHeight; contentView.setLayoutParams(rl); // 创建一个ViewHolder ViewHolder holder = new ViewHolder(view); return holder; } @Override public void onBindViewHolder(ViewHolder viewHolder, int position) { Log.e("xiaxl: ", "---onBindViewHolder---"); // 绑定数据到ViewHolder上 viewHolder.itemView.setTag(position); // viewHolder.mTextView.setText(position + " item"); } @Override public int getItemCount() { return mDataList.size(); } /** * */ public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mTextView; public ViewHolder(View itemView) { super(itemView); mTextView = (TextView) itemView.findViewById(R.id.add_btn); } } }
recycle_pager_item.xml
以上是“RecyclerView+PagerSnapHelper如何实现抖音首页翻页Viewpager效果”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!
本文名称:RecyclerView+PagerSnapHelper如何实现抖音首页翻页Viewpager效果
本文地址:http://scyanting.com/article/gohesj.html