美文网首页
BaseRecyclerViewAdapter+SmartRef

BaseRecyclerViewAdapter+SmartRef

作者: xulj100 | 来源:发表于2020-01-02 12:12 被阅读0次

1、 gradle 添加库文件

 implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34'
 implementation 'com.android.support:cardview-v7:27.1.1'
 implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14'

2、布局layout

<?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="match_parent"
    android:background="#F1F1F1"
    android:orientation="vertical">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            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、列表item布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"/>
</android.support.v7.widget.CardView>

4、BaseQuickAdapter

public class MyAdapter extends BaseQuickAdapter<String, MyAdapter.MyListViewHolder> {
    public MyAdapter(int layoutResId, @Nullable List<String> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(MyListViewHolder helper, String item) {
        if (!TextUtils.isEmpty(item)) {
            helper.setText(R.id.tv_name, item);
        }
    }

    public class MyListViewHolder extends BaseViewHolder {
        public TextView tv_name;

        public MyListViewHolder(View view) {
            super(view);
            tv_name = view.findViewById(R.id.tv_name);
        }
    }
}

4、显示列表、实现下拉刷新、加载更多

class TypeFragment : BaseFragment() {

    private lateinit var mAdapter: MyAdapter
    private val list = ArrayList<String>()

    override fun getContentLayout(): Int = R.layout.fragment_type

    override fun initData() {}

    override fun bindView(view: View, savedInstanceState: Bundle?) {
        getData()
        mAdapter = MyAdapter(R.layout.func_list_item, list)

        recyclerView.run {
            layoutManager = LinearLayoutManager(activity)
            adapter = mAdapter
        }

        refreshLayout.setOnRefreshListener {
            Handler().postDelayed({
                list.clear()
                getData()
                refreshLayout.finishRefresh()
                mAdapter.notifyDataSetChanged()
            }, 500)
        }

        refreshLayout.setOnLoadMoreListener {
            Handler().postDelayed({
                getData()
                refreshLayout.finishLoadMore()
                if (list.size > 15) {
                    refreshLayout.finishLoadMoreWithNoMoreData()
                }
                mAdapter.notifyDataSetChanged()
            }, 500)
        }
    }

    private fun getData() {
        list.add("Activity")
        list.add("Service")
        list.add("自定义View")
        list.add("View 动画")
        list.add("Java 开发模式")
        list.add("Java 排序")
        list.add("Android blog")
        list.add("learn kotlin")
        list.add("video player")
    }
      
}

4、查看效果


Screenshot.jpg

相关文章

网友评论

      本文标题:BaseRecyclerViewAdapter+SmartRef

      本文链接:https://www.haomeiwen.com/subject/hamnoctx.html