Android中怎么利用RecyclerView實現底部翻頁功能,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
創新互聯成都企業網站建設服務,提供成都網站建設、成都網站制作網站開發,網站定制,建網站,網站搭建,網站設計,成都響應式網站建設,網頁設計師打造企業風格網站,提供周到的售前咨詢和貼心的售后服務。歡迎咨詢做網站需要多少錢:028-86922220
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實現底部翻頁功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創新互聯行業資訊頻道,感謝各位的閱讀!
本文題目:Android中怎么利用RecyclerView實現底部翻頁功能
當前鏈接:http://vcdvsql.cn/article30/pepeso.html
成都網站建設公司_創新互聯,為您提供域名注冊、網站排名、響應式網站、自適應網站、面包屑導航、做網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯