Android 中使用RecyclerView实现底部翻页

 更新时间:2017年11月08日 10:41:01   作者:yikunhan  
这篇文章主要介绍了Android 中使用RecyclerView实现底部翻页功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下

RecyclerView 是Android L版本中新添加的一个用来取代ListView的SDK,它的灵活性与可替代性比listview更好。接下来通过一系列的文章讲解如何使用RecyclerView,彻底抛弃ListView.

最近在做pad端的app,需要一个像网页一样效果,之前使用addView方式,页码少的时候还可以,能实现效果,但是碰到了一个1000多页的界面,就GG了,页码半天显示不出来,于是使用RecyclerView作为容器,主要是看中RecyclerView的复用,不说了,看代码:

BottomPagerView xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
 <LinearLayout
 android:id="@+id/bottom_ll_content"
 android:layout_gravity="center_vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:gravity="center_vertical"
 android:layout_marginLeft="10px"
 android:layout_marginRight="10px"
 android:layout_marginTop="10px"
 android:orientation="horizontal">
 <Button
  android:id="@+id/pre_page"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginRight="@dimen/y5"
  android:paddingBottom="@dimen/x4"
  android:paddingLeft="@dimen/y5"
  android:paddingRight="@dimen/y5"
  android:paddingTop="@dimen/x4"
  android:text="上一页"
  android:textSize="@dimen/middlesize"/>
 <android.support.v7.widget.RecyclerView
  android:id="@+id/recycler"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 <Button
  android:id="@+id/next_page"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="@dimen/y5"
  android:paddingBottom="@dimen/x4"
  android:paddingLeft="@dimen/y5"
  android:paddingRight="@dimen/y5"
  android:paddingTop="@dimen/x4"
  android:text="下一页"
  android:textSize="@dimen/middlesize"/>
 </LinearLayout>
</LinearLayout>

adapter的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 <RadioButton
 android:id="@+id/bottom_item_rb"
 android:layout_width="wrap_content"
 android:text="1"
 android:gravity="center_vertical"
 android:background="@drawable/tab_select"
 android:layout_height="wrap_content"/>
</LinearLayout>
BottomPagerView 代码:
public class BottomPagerView extends LinearLayout {
 private final LinearLayout ll_content;
 private int pageSize = 0;
 private Button pre_page;
 private Button next_page;
 private RecyclerView recycler;
 private BottomAdapter mBottomAdapter;
 Context mContent;
 private boolean mShouldScroll = false;
 private int mToPosition = 0;
 private int smoothWidth = 0;
 public BottomPagerView(Context context) {
 this(context, null);
 }
 public BottomPagerView(Context context, @Nullable AttributeSet attrs) {
 super(context, attrs);
 this.mContent = context;
 LayoutInflater.from(context).inflate(R.layout.bottom_page, this, true);
 ll_content = (LinearLayout) findViewById(R.id.bottom_ll_content);
 pre_page = (Button) findViewById(R.id.pre_page);
 next_page = (Button) findViewById(R.id.next_page);
 recycler = (RecyclerView) findViewById(R.id.recycler);
 /*initView(context);*/
 }
 public BottomPagerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
 this(context, attrs);
 }
 private int currentPage = 0;
 public void initView(final Context context) {
 if (pageSize == 0) {
  ll_content.setVisibility(INVISIBLE);
 } else {
  ll_content.setVisibility(VISIBLE);
  final List<BottomBean> list = new ArrayList<>();
  for (int i = 0; i < pageSize; i++) {
  BottomBean bean = new BottomBean();
  bean.setPosition(i);
  if (i == 0) {
   bean.setSelect(true);
  } else {
   bean.setSelect(false);
  }
  list.add(bean);
  }
  final LinearLayoutManager manager = new LinearLayoutManager(context);
  manager.setOrientation(LinearLayoutManager.HORIZONTAL);
  recycler.setLayoutManager(manager);
  int width = 0;
  if (pageSize > 8) {
  int pixelSize = getResources().getDimensionPixelSize(R.dimen.y6);
  width = pixelSize * 10;
  } else {
  width = LayoutParams.WRAP_CONTENT;
  }
  LayoutParams params = new LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
  recycler.setLayoutParams(params);
  mBottomAdapter = new BottomAdapter(context, list);
  recycler.setAdapter(mBottomAdapter);
  mBottomAdapter.setCurPage(new BottomAdapter.getCurPage() {
  @Override
  public void serCurPage(int p) {
   list.get(currentPage).setSelect(false);
   list.get(p).setSelect(true);
   mBottomAdapter.notifyDataSetChanged();
   currentPage = p;
   smoothMoveToPosition(recycler, p);
   recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
   @Override
   public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    if (mShouldScroll){
    mShouldScroll = false;
    smoothMoveToPosition(recycler,mToPosition);
    }
   }
   });
   if (Curpage != null) {
   Curpage.serCurPage(p);
   }
  }
  });
  pre_page.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
   if (currentPage > 0) {
   int page = currentPage - 1;
   list.get(currentPage).setSelect(false);
   list.get(page).setSelect(true);
   currentPage = page;
   mBottomAdapter.notifyDataSetChanged();
   smoothMoveToPosition(recycler, page);
   recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    if (mShouldScroll){
     mShouldScroll = false;
     smoothMoveToPosition(recycler,mToPosition);
    }
    }
   });
   if (Curpage != null) {
    Curpage.serCurPage(page);
   }
   } else {
   return;
   }
  }
  });
  next_page.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
   if (currentPage < pageSize - 1) {
   list.get(currentPage).setSelect(false);
   int page = currentPage + 1;
   Log.d("BottomPagerView", "onClick: " + page);
   list.get(page).setSelect(true);
   currentPage = page;
   mBottomAdapter.notifyDataSetChanged();
   smoothMoveToPosition(recycler, page);
   recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
    super.onScrollStateChanged(recyclerView, newState);
    if (mShouldScroll){
     mShouldScroll = false;
     smoothMoveToPosition(recycler,mToPosition);
    }
    }
   });
   if (Curpage != null) {
    Curpage.serCurPage(page);
   }
   } else {
   return;
   }
  }
  });
 }
 }
 public void setPageSize(int size) {
 this.pageSize = size;
 initView(mContent);
 }
 private getCurPage Curpage;
 public interface getCurPage {
 void serCurPage(int p);
 }
 public void setCurPage(getCurPage page) {
 this.Curpage = page;
 }
 private void smoothMoveToPosition(RecyclerView mRecyclerView, final int position) {
 int firstItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(0));
 int lastItem = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(mRecyclerView.getChildCount()-1 ));
 Log.d("BottomPagerView", "smoothMoveToPosition: firstItem"+firstItem+" lastItem "+lastItem+" position"+position);
 if (position < firstItem) {
  mRecyclerView.smoothScrollToPosition(position);
  mShouldScroll = true;
  mToPosition = position;
 } else if (position <= lastItem) {
// 跳转位置在第一个可见项之后,最后一个可见项之前
// smoothScrollToPosition根本不会动,此时调用smoothScrollBy来滑动到指定位置
  int movePosition = position - firstItem;
  if (movePosition >= 0 && movePosition <= mRecyclerView.getChildCount()) {
  int top = mRecyclerView.getChildAt(movePosition).getLeft();
  int width = mRecyclerView.getMeasuredWidth() / 2;
  int scroll = top - width+mRecyclerView.getChildAt(movePosition).getMeasuredWidth()/2;
  Log.d("BottomPagerView", "smoothMove: "+scroll);
  mRecyclerView.smoothScrollBy(scroll, 0);
  }
 } else {
  mRecyclerView.smoothScrollToPosition(position);
  mShouldScroll = true;
  mToPosition = position;
 }
 }
}
BottomAdapter adapter:
public class BottomAdapter extends RecyclerView.Adapter<BottomAdapter.MyViewHolder> {
 Context mContext;
 List<BottomBean> size;
 private boolean isFirst = true;
 private int currentPage = 0;
 public BottomAdapter(Context context, List<BottomBean> size) {
 this.mContext = context;
 this.size = size;
 }
 @Override
 public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
 View view = View.inflate(mContext, R.layout.bottom_item, null);
 return new MyViewHolder(view);
 }
 @Override
 public void onBindViewHolder(final MyViewHolder holder, final int position) {
 holder.rb.setButtonDrawable(null);
 holder.rb.setText(position + 1 + "");
 holder.rb.setTag(position);
 holder.rb.setChecked(size.get(position).isSelect());
 holder.rb.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  if (!size.get((int) holder.rb.getTag()).isSelect()){
   Curpage.serCurPage((int) holder.rb.getTag());
  }
  }
 });
 }
 @Override
 public int getItemCount() {
 return size.size();
 }
 class MyViewHolder extends RecyclerView.ViewHolder {
 private final RadioButton rb;
 public MyViewHolder(View itemView) {
  super(itemView);
  rb = (RadioButton) itemView.findViewById(R.id.bottom_item_rb);
 }
 }
 private getCurPage Curpage;
 public interface getCurPage {
 void serCurPage(int p);
 }
 public void setCurPage(getCurPage page) {
 this.Curpage = page;
 }
}

调用:

直接在xml中使用

<BottomPagerView
 android:id="@+id/part_part_tab"
 android:layout_width="wrap_content"
 android:layout_below="@+id/part_part_recycler"
 android:layout_height="wrap_content"
 android:layout_gravity="center_horizontal"
 android:layout_marginBottom="5dp"/>

代码中调用:

初始化:

mBottomPagerView.setPageSize(AllPage);

回调:

mBottomPagerView.setCurPage(new BottomPagerView.getCurPage() {
 @Override public void serCurPage(int p) { //获取点击的页码数,操作
 }
});

总结

以上所述是小编给大家介绍的Android 中使用RecyclerView实现底部翻页,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • 使用Flutter实现一个走马灯布局的示例代码

    使用Flutter实现一个走马灯布局的示例代码

    这篇文章主要介绍了使用 Flutter 实现一个走马灯布局的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Android中截取当前屏幕图片的实例代码

    Android中截取当前屏幕图片的实例代码

    该篇文章是说明在Android手机或平板电脑中如何实现截取当前屏幕的功能,并把截取的屏幕保存到SDCard中的某个目录文件夹下面。实现的代码如下:
    2013-08-08
  • Android自定义View绘制流程详解

    Android自定义View绘制流程详解

    这篇文章主要为大家介绍了Android自定义View绘制流程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • Android开发使用Handler的PostDelayed方法实现图片轮播功能

    Android开发使用Handler的PostDelayed方法实现图片轮播功能

    这篇文章主要介绍了Android开发使用Handler的PostDelayed方法实现图片轮播功能,结合实例形式分析了Android基于Handler的PostDelayed方法实现图片轮播功能的具体步骤与相关操作技巧,需要的朋友可以参考下
    2017-12-12
  • Android中异步类AsyncTask用法总结

    Android中异步类AsyncTask用法总结

    这篇文章主要介绍了Android中异步类AsyncTask用法,分析总结了Async Task类的功能、特点及相关的使用技巧与注意事项,需要的朋友可以参考下
    2016-01-01
  • Android10开发者常见问题(小结)

    Android10开发者常见问题(小结)

    这篇文章主要介绍了Android10开发者常见问题(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • Android中使用开源框架eventbus3.0实现fragment之间的通信交互

    Android中使用开源框架eventbus3.0实现fragment之间的通信交互

    本文主要介绍了Android中使用开源框架eventbus3.0实现fragment之间的通信交互的方法,具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • Android使用Service实现IPC通信的2种方式

    Android使用Service实现IPC通信的2种方式

    这篇文章主要介绍了Android使用Service实现IPC通信的2种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • Android中RecyclerView点击Item设置事件

    Android中RecyclerView点击Item设置事件

    这篇文章主要介绍了Android中RecyclerView点击Item设置事件的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-07-07
  • Android实现登录邮箱的自动补全功能

    Android实现登录邮箱的自动补全功能

    这篇文章主要为大家详细介绍了Android实现登录邮箱的自动补全功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-04-04

最新评论