android SwipeRefreshLayout 下拉刷新控件使用

导入库文件

implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-28'

ui


    

        

        

        

        

    

class


public class MainActivity extends AppCompatActivity {

    RecyclerView rv_list;
    RefreshLayout mRefreshLayout;

    ArrayList mEntityList = new ArrayList();

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

        initData();
        initUI();

    }

    private void initUI() {
        rv_list = findViewById(R.id.rv_list);
        mRefreshLayout = findViewById(R.id.refrest);

        // 定义一个线性布局管理器
        LinearLayoutManager manager = new LinearLayoutManager(this);

        // 设置布局管理器
        rv_list.setLayoutManager(manager);
        // 设置adapter
        final DemoAdapter adapter = new DemoAdapter(MainActivity.this, mEntityList);
        rv_list.setAdapter(adapter);


        adapter.setOnItemClickLitener(new DemoAdapter.OnItemClickLitener() {
            @Override
            public void onItemClick(View view, int position) {
                String str = (String) mEntityList.get(position);
                Log.i("demo", str);
            }

        });

        //刷新
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                initData();
                adapter.notifyDataSetChanged();
                mRefreshLayout.finishRefresh();
            }
        });


        //加载更多
        mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                initData();
                adapter.notifyDataSetChanged();
                refreshLayout.finishLoadMore();
            }
        });

//        //开始下拉
//        mRefreshLayout.setEnableRefresh(true);//启用刷新
//        mRefreshLayout.setEnableLoadMore(true);
//        //关闭下拉
//        mRefreshLayout.finishRefresh();
//        mRefreshLayout.finishLoadMore();
    }


    private void initData() {
        for (int i = 'A'; i <= 'z'; i++) {
            mEntityList.add((char) i + "");
        }


    }


}

适配器

public class DemoAdapter extends RecyclerView.Adapter {
    Context context;

    ArrayList lists;

    public DemoAdapter(Context context, ArrayList lists) {
        this.context = context;
        this.lists = lists;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.demo_adapter, viewGroup, false);
        return new DemoViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        String str = (String) lists.get(i);
        final DemoViewHolder holder = (DemoViewHolder) viewHolder;
        holder.tv_desc.setText(str);

        if(mOnItemClickLitener!=null)
        {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });
        }
    }

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


    private class DemoViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_desc;

        public DemoViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_desc = itemView.findViewById(R.id.tv_desc);
        }

    }

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

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener onItemClickLitener){
        this.mOnItemClickLitener = onItemClickLitener;
    }

}

适配器文件





    




你可能感兴趣的:(android)