天天看點

Android SmartRefreshLayout下拉重新整理 上拉加載

Android SmartRefreshLayout下拉重新整理 上拉加載
Android SmartRefreshLayout下拉重新整理 上拉加載

1.添加依賴  app下的 build.gradle 

dependencies {

    ......

    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'
    implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.3'//沒有使用特殊Header,可以不加這行
    implementation 'com.android.support:appcompat-v7:25.3.1'//版本随意(必須)
}
           

2.布局activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/srl_control"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srlAccentColor="#00000000"
        app:srlPrimaryColor="#00000000"
        app:srlEnablePreviewInEditMode="true">

        <com.scwang.smartrefresh.layout.header.ClassicsHeader
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_refresh"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <com.scwang.smartrefresh.layout.footer.ClassicsFooter
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

</LinearLayout>
           

3.擴充卡布局 adapter_item.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="vertical">

    <LinearLayout
        android:id="@+id/group"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center"
        android:layout_margin="20dp"
        android:orientation="vertical"
        android:background="#00FFFF">

        <TextView
            android:id="@+id/tv_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="item1"
            android:textColor="#000000"
            android:textSize="16dp" />

    </LinearLayout>

</LinearLayout>
           

4.擴充卡 ListAdapter

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.Myvh> {

    Context context;
    List<String> list;

    public ListAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setList(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @NotNull
    @Override
    public Myvh onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_item,parent,false);
        return new Myvh(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull @NotNull Myvh holder, int position) {
        holder.tv_item.setText(position+"");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class Myvh extends RecyclerView.ViewHolder {

        private final TextView tv_item;

        public Myvh(@NonNull @NotNull View itemView) {
            super(itemView);
            tv_item = itemView.findViewById(R.id.tv_item);
        }
    }
}
           

5.MainActivity

public class MainActivity extends AppCompatActivity {

    private RecyclerView rv_refresh;
    private SmartRefreshLayout srl_control;

    List<String> list = new ArrayList<>();
    ListAdapter listAdapter = new ListAdapter(MainActivity.this);

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

        //ID
        rv_refresh = findViewById(R.id.rv_refresh);
        srl_control = findViewById(R.id.srl_control);

        //設定資料
        SetData();

//        srl_control.setRefreshHeader(new WaterDropHeader(this));
//        srl_control.setRefreshFooter(new BallPulseFooter(this));

        //重新整理
        srl_control.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {

                refreshlayout.finishRefresh(2000);

                listAdapter.notifyDataSetChanged();
            }
        });

        //加載
        srl_control.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {

                refreshlayout.finishLoadmore(2000);

                list.add("a");
                list.add("a");

                listAdapter.notifyDataSetChanged();
            }
        });
    }

    public void SetData(){
        //布局管理器
        LinearLayoutManager linearLayout = new LinearLayoutManager(this);
        //設定垂直布局
        linearLayout.setOrientation(LinearLayoutManager.VERTICAL);
        //設定recyclerview
        rv_refresh.setLayoutManager(linearLayout);

        //添加集合資料
        list.add("a");
        list.add("a");
        list.add("a");
        list.add("a");
        list.add("a");
        list.add("a");

        //将資料添加到Adapter中
        listAdapter.setList(list);
        rv_refresh.setAdapter(listAdapter);

    }
}