最近在做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,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实现底部翻页,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android 中使用RecyclerView实现底部翻页全部内容,希望文章能够帮你解决Android 中使用RecyclerView实现底部翻页所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)