欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 中使用RecyclerView实现底部翻页

程序员文章站 2023-10-18 23:57:28
recyclerview 是android l版本中新添加的一个用来取代listview的sdk,它的灵活性与可替代性比listview更好。接下来通过一系列的文章讲解如何...

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实现底部翻页,希望对大家有所帮助