亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android RecyclerView實現下拉刷新和上拉加載更多

發布時間:2020-10-13 07:32:30 來源:腳本之家 閱讀:646 作者:kklxb 欄目:移動開發

使用官方的刷新控件SwipeRefreshLayout來實現下拉刷新,當RecyclerView滑到底部實現下拉加載(進度條效果用RecyclerView加載一個布局實現)

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

主布局

<?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);//設置刷新時進度條

顏色,最多四種
  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()) {
    //滾動到底部了,可以進行數據加載等操作
   }
  }

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

下面是實現上拉時進度條轉動的效果

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) {//加載進度條的布局
   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;
  }
 }

 /**
  * 獲取數據集加上一個footer的數量
  */
 @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;
  }
 }

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



 /**
  * 內容的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);
  }
 }

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

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

肇庆市| 吴忠市| 肃南| 无锡市| 泌阳县| 南雄市| 湘乡市| 郎溪县| 靖州| 山阳县| 五莲县| 太仓市| 天等县| 莫力| 和平区| 罗江县| 潞城市| 平乐县| 多伦县| 福州市| 句容市| 广德县| 漳州市| 合作市| 天水市| 福清市| 锡林浩特市| 田阳县| 平阴县| 龙海市| 桃园市| 黑水县| 霍山县| 金湖县| 新乐市| 丽江市| 安乡县| 通州市| 神农架林区| 新乡县| 岑巩县|