欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 中使用RecyclerView實現(xiàn)底部翻頁

 更新時間:2017年11月08日 10:41:01   作者:yikunhan  
這篇文章主要介紹了Android 中使用RecyclerView實現(xiàn)底部翻頁功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

RecyclerView 是Android L版本中新添加的一個用來取代ListView的SDK,它的靈活性與可替代性比listview更好。接下來通過一系列的文章講解如何使用RecyclerView,徹底拋棄ListView.

最近在做pad端的app,需要一個像網(wǎng)頁一樣效果,之前使用addView方式,頁碼少的時候還可以,能實現(xiàn)效果,但是碰到了一個1000多頁的界面,就GG了,頁碼半天顯示不出來,于是使用RecyclerView作為容器,主要是看中RecyclerView的復(fù)用,不說了,看代碼:

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) {
// 跳轉(zhuǎn)位置在第一個可見項之后,最后一個可見項之前
// smoothScrollToPosition根本不會動,此時調(diào)用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;
 }
}

調(diào)用:

直接在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"/>

代碼中調(diào)用:

初始化:

mBottomPagerView.setPageSize(AllPage);

回調(diào):

mBottomPagerView.setCurPage(new BottomPagerView.getCurPage() {
 @Override public void serCurPage(int p) { //獲取點擊的頁碼數(shù),操作
 }
});

總結(jié)

以上所述是小編給大家介紹的Android 中使用RecyclerView實現(xiàn)底部翻頁,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Android數(shù)據(jù)共享 sharedPreferences 的使用方法

    Android數(shù)據(jù)共享 sharedPreferences 的使用方法

    這篇文章主要介紹了Android數(shù)據(jù)共享 sharedPreferences 的使用方法的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解使用sharedpreferences,需要的朋友可以參考下
    2017-10-10
  • Android拖拽助手ViewDragHelper的創(chuàng)建與使用實例

    Android拖拽助手ViewDragHelper的創(chuàng)建與使用實例

    ViewDragHelper是針對 ViewGroup 中的拖拽和重新定位 views 操作時提供了一系列非常有用的方法和狀態(tài)追蹤,下面這篇文章主要給大家介紹了關(guān)于Android拖拽助手ViewDragHelper的創(chuàng)建與使用的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • 接口對象的實例化在接口回調(diào)中的使用方法

    接口對象的實例化在接口回調(diào)中的使用方法

    下面小編就為大家?guī)硪黄涌趯ο蟮膶嵗诮涌诨卣{(diào)中的使用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • PagerSlidingTabStrip制作Android帶標(biāo)簽的多界面滑動切換

    PagerSlidingTabStrip制作Android帶標(biāo)簽的多界面滑動切換

    這篇文章主要介紹了使用PagerSlidingTabStrip制作Android帶標(biāo)簽的多界面滑動切換效果的方法,PagerSlidingTabStrip是GitHub上的一個開源項目,調(diào)用這個庫可以少寫不少代碼XD 需要的朋友可以參考下
    2016-04-04
  • 自定義Android注解系列教程之注解變量

    自定義Android注解系列教程之注解變量

    這篇文章主要給大家介紹了關(guān)于自定義Android注解系列教程之注解變量的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Android HttpURLConnection.getResponseCode()錯誤解決方法

    Android HttpURLConnection.getResponseCode()錯誤解決方法

    在使用HttpURLConnection.getResponseCode()的時候直接報錯是IOException錯誤,一直想不明白,同一個程序我調(diào)用了兩次,結(jié)果有一個鏈接一直O(jiān)K,另一個卻一直報這個錯誤
    2013-06-06
  • Android 實現(xiàn)自定義圓形進度條的功能

    Android 實現(xiàn)自定義圓形進度條的功能

    這篇文章主要介紹了Android 實現(xiàn)自定義圓形進度條的功能的相關(guān)資料,開發(fā)Android應(yīng)用的朋友肯定對自定義View不陌生,很多都有重新寫的,這里就對實現(xiàn)圓形進度條介紹下,需要的朋友可以參考下
    2016-11-11
  • android實現(xiàn)圖片反轉(zhuǎn)效果

    android實現(xiàn)圖片反轉(zhuǎn)效果

    這篇文章主要介紹了android實現(xiàn)圖片反轉(zhuǎn)效果的方法,需要的朋友可以參考下
    2015-09-09
  • android 幀動畫,補間動畫,屬性動畫的簡單總結(jié)

    android 幀動畫,補間動畫,屬性動畫的簡單總結(jié)

    本文主要對android 幀動畫,補間動畫,屬性動畫進行了簡單總結(jié),具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Android編程實現(xiàn)圖片的瀏覽、縮放、拖動和自動居中效果

    Android編程實現(xiàn)圖片的瀏覽、縮放、拖動和自動居中效果

    這篇文章主要介紹了Android編程實現(xiàn)圖片的瀏覽、縮放、拖動和自動居中效果,以具體實例形式分析了Android針對圖片各種常見顯示效果的布局及功能實現(xiàn)技巧,需要的朋友可以參考下
    2015-11-11

最新評論