十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
Android中怎么利用RecyclerView实现底部翻页功能,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
创新互联成都企业网站建设服务,提供成都网站建设、成都网站制作网站开发,网站定制,建网站,网站搭建,网站设计,成都响应式网站建设,网页设计师打造企业风格网站,提供周到的售前咨询和贴心的售后服务。欢迎咨询做网站需要多少钱:028-86922220
BottomPagerView xml布局:
adapter的xml布局:
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 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 { Context mContext; List size; private boolean isFirst = true; private int currentPage = 0; public BottomAdapter(Context context, List 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中使用
代码中调用:
初始化:
mBottomPagerView.setPageSize(AllPage);
回调:
mBottomPagerView.setCurPage(new BottomPagerView.getCurPage() { @Override public void serCurPage(int p) { //获取点击的页码数,操作 } });
看完上述内容,你们掌握Android中怎么利用RecyclerView实现底部翻页功能的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!