中文字幕日韩精品一区二区免费_精品一区二区三区国产精品无卡在_国精品无码专区一区二区三区_国产αv三级中文在线

AndroidRecyclerView實(shí)現(xiàn)下拉刷新和上拉加載更多

使用官方的刷新控件SwipeRefreshLayout來實(shí)現(xiàn)下拉刷新,當(dāng)RecyclerView滑到底部實(shí)現(xiàn)下拉加載(進(jìn)度條效果用RecyclerView加載一個(gè)布局實(shí)現(xiàn))

專注于為中小企業(yè)提供成都網(wǎng)站制作、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)大英免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了數(shù)千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

需要完成控件的下拉監(jiān)聽和上拉監(jiān)聽,其中,下拉監(jiān)聽通過SwipRefreshLayout的setOnRefreshListener()方法監(jiān)聽,而上拉刷新,需要通過監(jiān)聽列表的滾動(dòng),當(dāng)列表滾動(dòng)到底部時(shí)觸發(fā)事件,具體代碼如下

主布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
 android:id="@+id/refresh_layout"
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <android.support.v7.widget.RecyclerView
  android:id="@+id/recycler_list"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

</android.support.v4.widget.SwipeRefreshLayout>

public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
 private SwipeRefreshLayout refreshLayout;
 private RecyclerView recyclerView;
 private LinearLayoutManager layoutManager;

 private RecyclerAdapter mAdapter;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initViews();
 }

 private void initViews() {
  refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh_layout);
  recyclerView = (RecyclerView) findViewById(R.id.recycler_list);
  layoutManager = new LinearLayoutManager(this);

  refreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary);//設(shè)置刷新時(shí)進(jìn)度條

顏色,最多四種
  refreshLayout.setOnRefreshListener(this);

  mAdapter = new RecyclerAdapter();//自定義的適配器
  recyclerView.setAdapter(mAdapter);
  recyclerView.setLayoutManager(layoutManager);
  recyclerView.addOnScrollListener(new OnRecyclerScrollListener());
 }

 /**
  * 用于下拉刷新
  */
 @Override
 public void onRefresh() {
 }

 /**
  * 用于上拉加載更多
  */
 public class OnRecyclerScrollListener extends RecyclerView.OnScrollListener {

  int lastVisibleItem = 0;

  @Override
  public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
   super.onScrollStateChanged(recyclerView, newState);

   if (mAdapter != null && newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == 

mAdapter.getItemCount()) {
    //滾動(dòng)到底部了,可以進(jìn)行數(shù)據(jù)加載等操作
   }
  }

  @Override
  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
   super.onScrolled(recyclerView, dx, dy);
   lastVisibleItem = layoutManager.findLastVisibleItemPosition();
  }
 }
}

下面是實(shí)現(xiàn)上拉時(shí)進(jìn)度條轉(zhuǎn)動(dòng)的效果

item_list_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 <TextView
  android:id="@+id/tv_item_footer_load_more"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_margin="16dp"
  android:gravity="center"
  android:text="上拉加載更多"
 />

 <ProgressBar
  android:id="@+id/pb_item_footer_loading"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_margin="16dp"
android:visibility="gone"/>
</RelativeLayout>

適配器

public class RecyclerAdapter extends RecyclerView.Adapter<ViewHolder> {

 private static final int TYPE_CONTENT = 0;
 private static final int TYPE_FOOTER = 1;

 private ArrayList<DataBean> dataList;

 private ProgressBar pbLoading;
 private TextView tvLoadMore;

 public RecyclerAdapter() {
  dataList = new ArrayList<>();
 }

 @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  if (viewType == TYPE_CONTENT) {
   return new ContentViewHolder(LayoutInflater.from(parent.getContext()).inflate

(R.layout.item_list_content, parent, false));
  } else if (viewType == TYPE_FOOTER) {//加載進(jìn)度條的布局
   return new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate

(R.layout.item_list_footer, parent, false));
  }
  return null;
 }

 @Override
 public void onBindViewHolder(ViewHolder holder, int position) {
  int type = getItemViewType(position);
  if (type == TYPE_CONTENT) {
   DataBean bean = dataList.get(position);
   ((ContentViewHolder) holder).tvId.setText("" + bean.getId());
   ((ContentViewHolder) holder).tvName.setText(bean.getName());
  } else if (type == TYPE_FOOTER) {
   pbLoading = ((FooterViewHolder) holder).pbLoading;
   tvLoadMore = ((FooterViewHolder) holder).tvLoadMore;
  }
 }

 /**
  * 獲取數(shù)據(jù)集加上一個(gè)footer的數(shù)量
  */
 @Override
 public int getItemCount() {
  return dataList.size() + 1;
 }


 @Override
 public int getItemViewType(int position) {
  if (position + 1 == getItemCount()) {
   return TYPE_FOOTER;
  } else {
   return TYPE_CONTENT;
  }
 }

 /**
  * 獲取數(shù)據(jù)集的大小
  */
 public int getListSize() {
  return dataList.size();
 }



 /**
  * 內(nèi)容的ViewHolder
  */
 public static class ContentViewHolder extends ViewHolder {
  private TextView tvId, tvName;

  public ContentViewHolder(View itemView) {
   super(itemView);
   tvId = (TextView) itemView.findViewById(R.id.tv_item_id);
   tvName = (TextView) itemView.findViewById(R.id.tv_item_name);
  }
 }

 /**
  * footer的ViewHolder
  */
 public static class FooterViewHolder extends ViewHolder {
  private TextView tvLoadMore;
  private ProgressBar pbLoading;

  public FooterViewHolder(View itemView) {
   super(itemView);
   tvLoadMore = (TextView) itemView.findViewById(R.id.tv_item_footer_load_more);
   pbLoading = (ProgressBar) itemView.findViewById(R.id.pb_item_footer_loading);
  }
 }

 /**
  * 顯示正在加載的進(jìn)度條,滑動(dòng)到底部時(shí),調(diào)用該方法,上拉就顯示進(jìn)度條,隱藏"上拉加載更多"
  */
 public void showLoading() {
  if (pbLoading != null && tvLoadMore != null) {
   pbLoading.setVisibility(View.VISIBLE);
   tvLoadMore.setVisibility(View.GONE);
  }
 }

 /**
  * 顯示上拉加載的文字,當(dāng)數(shù)據(jù)加載完畢,調(diào)用該方法,隱藏進(jìn)度條,顯示“上拉加載更多”
  */
 public void showLoadMore() {
  if (pbLoading != null && tvLoadMore != null) {
   pbLoading.setVisibility(View.GONE);
   tvLoadMore.setVisibility(View.VISIBLE);
  }
 }
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

文章標(biāo)題:AndroidRecyclerView實(shí)現(xiàn)下拉刷新和上拉加載更多
地址分享:http://www.rwnh.cn/article48/ghcghp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站改版、營銷型網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)搜索引擎優(yōu)化、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)
余姚市| 郓城县| 通道| 昌邑市| 甘泉县| 德兴市| 土默特右旗| 扶沟县| 青冈县| 青铜峡市| 石柱| 福鼎市| 东安县| 蒲城县| 凉山| 汉寿县| 鹤峰县| 崇左市| 常山县| 灌云县| 秦安县| 内丘县| 淮安市| 穆棱市| 分宜县| 旺苍县| 民丰县| 晋城| 益阳市| 文昌市| 阳泉市| 长宁区| 新沂市| 岳普湖县| 屯门区| 洪洞县| 宁南县| 沙雅县| 鹿邑县| 阳新县| 武乡县|