91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

如何在Android項目中使用RecyclerView實現上拉或下拉刷新功能

發布時間:2020-11-24 16:53:19 來源:億速云 閱讀:147 作者:Leah 欄目:移動開發

這篇文章給大家介紹如何在Android項目中使用RecyclerView實現上拉或下拉刷新功能,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

首頁布局:activity_notice

 <&#63;xml version="1.0" encoding="utf-8"&#63;>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@android:color/white"
  android:orientation="vertical"
  >

  <include layout="@layout/toolbar" />

  <android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/SwipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
      android:id="@+id/recyclerView"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scrollbars="vertical"></android.support.v7.widget.RecyclerView>
  </android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>  

子item布局:item_base

<&#63;xml version="1.0" encoding="utf-8"&#63;>

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginLeft="@dimen/margin_10"
  android:layout_marginRight="@dimen/margin_10"
  android:layout_marginTop="6dp"
  android:orientation="vertical"
  app:cardBackgroundColor="@color/line"
  app:cardPreventCornerOverlap="true"
  app:cardUseCompatPadding="true"
  app:contentPadding="6dp">

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
      android:id="@+id/tv_date"
      
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="2016-10-9 10:00" />

    <android.support.v7.widget.CardView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      app:cardBackgroundColor="@color/white"
      app:cardPreventCornerOverlap="true"
      app:cardUseCompatPadding="true"
      app:contentPadding="10dp">

      <TextView
        android:id="@+id/tv_title"
        
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="2"
        android:text="。。。,看到這個場景。我看著她她看著我,說道:“怎么樣,我當年的手工還不錯吧。。。。。。。。。”" />

    </android.support.v7.widget.CardView>
  </LinearLayout>

</android.support.v7.widget.CardView>

腳布局 item_foot

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="40dp"
  android:gravity="center"
  android:orientation="horizontal"
  android:id="@+id/ll"
 >


  <ProgressBar
    android:layout_marginRight="6dp"
    android:id="@+id/progressBar"
    
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

  <TextView
    android:id="@+id/tv"
    
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/loading" />


</LinearLayout>

toolbar

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">

  <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

    <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@color/blueStatus"
      android:minHeight="&#63;attr/actionBarSize"
      app:layout_scrollFlags="scroll|enterAlways"
      app:navigationIcon="&#63;attr/homeAsUpIndicator"
      app:theme="@style/Theme.AppCompat.NoActionBar">

    </android.support.v7.widget.Toolbar>
  </android.support.design.widget.AppBarLayout>

首頁 MainActivity

package com.lxl.refresh;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Description:
 * Author:lxl
 * Date: 2016/10/9 10:15
 */
public class MainActivity extends AppCompatActivity {


  @InjectView(R.id.toolbar)
  Toolbar toolbar;
  @InjectView(R.id.recyclerView)
  RecyclerView recyclerView;
  @InjectView(R.id.SwipeRefreshLayout)
  SwipeRefreshLayout swipeRefreshLayout;
  boolean isSlidingToLast = false; //判斷滾動狀態
  boolean isLoading;
  private List<Map<String, Object>> data = new ArrayList<>();
  private RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, data);
  private Handler handler = new Handler();

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_notice);
    ButterKnife.inject(this);
    initView();
    Map<String, Object> map = new HashMap<>();
    initData();
  }

  public void initView() {
    setSupportActionBar(toolbar);
    toolbar.setTitle(R.string.notice);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        finish();
      }
    });

    swipeRefreshLayout.setColorSchemeResources(R.color.blueStatus);
    swipeRefreshLayout.post(new Runnable() {
      @Override
      public void run() {
        swipeRefreshLayout.setRefreshing(true);
      }
    });

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
      @Override
      public void onRefresh() {
        handler.postDelayed(new Runnable() {
          @Override
          public void run() {
            data.clear();
            getData();
          }
        }, 2000);
      }
    });
    final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(adapter);
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

      @Override
      public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        Log.d("test", "StateChanged = " + newState); //滑動狀態改變時觸發有0,1,2三種狀態


      }

      @Override
      public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);
        if (dy > 0) {
          //大于0表示正在向下滾動
          isSlidingToLast = true;
        } else {
          //小于等于0表示停止或向上滾動
          isSlidingToLast = false;
        }
        Log.d("test", "onScrolled"); //當RecyclerView滑動時觸發類似點擊事件的MotionEvent.ACTION_MOVE
        int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
        if (lastVisibleItemPosition + 1 == adapter.getItemCount()&& isSlidingToLast) {
          Log.d("test", "loading executed");//當且僅當滑動到最后一項并且手指上拉拋出時才執行

          boolean isRefreshing = swipeRefreshLayout.isRefreshing();
          Log.d("test", "hahh"+isRefreshing);
          if (isRefreshing) { //如何在下拉刷新則隱藏腳布局并且返回
            adapter.notifyItemRemoved(adapter.getItemCount());
            return;
          }
          if (!isLoading) {
            isLoading = true;
            handler.postDelayed(new Runnable() {
              @Override
              public void run() {
                getData(); //延時執行加載
                Log.d("test", "load more completed");
                isLoading = false;
              }
            }, 1000);
          }
        }
      }
    });

    //添加點擊事件
    adapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClickListener() {
      @Override
      public void onItemClick(View view, int position) {
        Log.d("test", "item position = " + position);
      }

      @Override
      public void onItemLongClick(View view, int position) {

      }
    });
  }


  public void initData() { //第一次進來的時候加載數據
    handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        getData();
      }
    }, 1500);

  }

  /**
   * 獲取測試數據
   */
  private void getData() {
    for (int i = 0; i < 6; i++) { //每次加載六條數據
      Map<String, Object> map = new HashMap<>();
      data.add(map);
    }
    adapter.notifyDataSetChanged();
    swipeRefreshLayout.setRefreshing(false); //加載數據后收起下拉刷新
    Log.d("test", adapter.getItemCount()+"");

//    adapter.notifyItemRemoved(adapter.getItemCount()); //加載數據后上拉刷新的腳布局移除掉

//    adapter.footerHolder.ll.setVisibility(View.GONE);
    //加載數據后上拉刷新的腳布局隱藏掉
    adapter.setmCreateViewHolder(new RecyclerViewAdapter.onHolderCreate() {
      @Override
      public void created() {
        if (isSlidingToLast==false){
          adapter.footerHolder.ll.setVisibility(View.GONE);
        }else{
          adapter.footerHolder.ll.setVisibility(View.VISIBLE);
        }
      }
    });
  }


}

在第106行中通過這三個條件判斷就可以下拉刷新的執行。
RecyclerView的適配器:RecyclerViewAdapter

package com.lxl.refresh;

import android.content.Context;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.util.List;

public class RecyclerViewAdapter extends Adapter<ViewHolder> {

  private static final int TYPE_ITEM = 0;
  private static final int TYPE_FOOTER = 1;
  private Context context;
  private List data;

  protected FootViewHolder footerHolder;

  public RecyclerViewAdapter(Context context, List data) {
    this.context = context;
    this.data = data;
  }
  //-------------------------------------
  public interface onHolderCreate{ //定義一個viewholder回調接口
    void created();
  }

  public onHolderCreate getmCreateViewHolder() {
    return mCreateViewHolder;
  }

  public void setmCreateViewHolder(onHolderCreate mCreateViewHolder) {
    this.mCreateViewHolder = mCreateViewHolder;
  }

  private onHolderCreate mCreateViewHolder;

  //-------------------------------------

  public interface OnItemClickListener {
    void onItemClick(View view, int position);

    void onItemLongClick(View view, int position);
  }

  private OnItemClickListener onItemClickListener;

  public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
    this.onItemClickListener = onItemClickListener;
  }

  @Override
  public int getItemCount() {
    return data.size() == 0 &#63; 0 : data.size() + 1; //添加腳布局
  }

  @Override
  public int getItemViewType(int position) {
    if (position + 1 == getItemCount()) {
      return TYPE_FOOTER; //腳布局
    } else {
      return TYPE_ITEM; //普通item
    }
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_ITEM) { //創建普通類型的item布局
      View view = LayoutInflater.from(context).inflate(R.layout.item_base, parent,
          false);
      Log.d("test", "創建普通類型的item布局");
      return new ItemViewHolder(view);
    } else if (viewType == TYPE_FOOTER) { //創建腳布局類型的item布局
      View view = LayoutInflater.from(context).inflate(R.layout.item_foot, parent,
          false);
      footerHolder=new FootViewHolder(view);
      Log.d("test", "創建腳布局類型的item布局");

      return footerHolder;
    }
    return null;
  }


  @Override
  public void onBindViewHolder(final ViewHolder holder, int position) {
    //在onCreateViewHolder執行完成后回調
    if (holder instanceof FootViewHolder){
      if (mCreateViewHolder!=null){
        mCreateViewHolder.created();
      }
    }
    if (holder instanceof ItemViewHolder) {
      //holder.tv.setText(data.get(position));
      if (onItemClickListener != null) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            int position = holder.getLayoutPosition();
            onItemClickListener.onItemClick(holder.itemView, position);
          }
        });

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
          @Override
          public boolean onLongClick(View v) {
            int position = holder.getLayoutPosition();
            onItemClickListener.onItemLongClick(holder.itemView, position);
            return false;
          }
        });
      }
    }
  }


  static class ItemViewHolder extends ViewHolder {

    TextView tv;

    public ItemViewHolder(View view) {
      super(view);
      tv = (TextView) view.findViewById(R.id.tv_date);
    }
  }

   class FootViewHolder extends ViewHolder {
     LinearLayout ll;
     ProgressBar pb;
     TextView tv;
    public FootViewHolder(View view) {
      super(view);
      ll=(LinearLayout) view.findViewById(R.id.ll);
      pb=(ProgressBar) view.findViewById(R.id.progressBar);
      tv=(TextView) view.findViewById(R.id.tv);
    }
  }
}

在第30行定義一個借口用來通知下拉刷新后隱藏腳布局,主要在onCreateViewHolder執行完成后回調。

關于如何在Android項目中使用RecyclerView實現上拉或下拉刷新功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

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

AI

琼中| 普安县| 维西| 绥滨县| 长宁区| 长汀县| 德安县| 浑源县| 宣武区| 佛冈县| 荥阳市| 阿克苏市| 泸溪县| 四子王旗| 贵港市| 二连浩特市| 米脂县| 江山市| 佛山市| 花莲县| 新河县| 嘉禾县| 青海省| 鄂伦春自治旗| 通道| 海门市| 彭山县| 宁强县| 若羌县| 灵台县| 修文县| 玉溪市| 江达县| 钟祥市| 即墨市| 临汾市| 榆中县| 息烽县| 寿阳县| 龙里县| 辽中县|