Androidx通过SmartRefreshLayout&&recyclerview实现上拉加载下拉刷新

Androidx版本实现下拉刷新上拉加载框架SmartRefreshLayout(点击查看GitHub源码)

1、引入强大的SmartRefreshLayout依赖,同时引入recyclerview;

implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.2'
implementation 'androidx.recyclerview:recyclerview:1.0.0'

2、在需要的xml写入框架界面



    

    


    

3、这个时候你需要一个实体类:

public class Fruit {
    private String name;
    private String money;
    private int imageId;
    //(也可通过getset方式来实现)
    public Fruit(String name,String money,int imageId){
        this.name = name;
        this.money = money;
        this.imageId = imageId;
    }
    public String getName() {
        return name;
    }
    
    public String getMoney() {
        return money;
    }

    public int getImageId() {
        return imageId;
    }
}

4、这个时候你需要一个adapter:

public class FruitAdapter extends RecyclerView.Adapter{
    private List mFruitList;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.fruitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Fruit fruit = mFruitList.get(position);
                Toast.makeText(view.getContext(), "你点击了View"+ fruit.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        holder.fruitImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int position = holder.getAdapterPosition();
                Fruit fruit = mFruitList.get(position);
                Toast.makeText(view.getContext(), "你点击了图片"+ fruit.getName(), Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Fruit fruit = mFruitList.get(position);
        holder.fruitImage.setImageResource(fruit.getImageId());
        holder.fruitName.setText(fruit.getName());
        holder.fruitMoney.setText(fruit.getMoney());
    }

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

    public FruitAdapter(List fruitList) {
        mFruitList = fruitList;
    }

    public class ViewHolder  extends RecyclerView.ViewHolder {
        View fruitView;
        ImageView fruitImage;
        TextView fruitName;
        TextView fruitMoney;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            fruitView = itemView;
            fruitImage = itemView.findViewById(R.id.fruit_image);
            fruitName = itemView.findViewById(R.id.fruit_name);
            fruitMoney = itemView.findViewById(R.id.fruit_money);
        }
    }

5、接下来,你就在主页面去使用啦,开心不:

public class MainActivity extends AppCompatActivity {
    private List fruitList = new ArrayList<>();
    final private String TAG = "MainActivity";
    private RecyclerView recyclerView;
    private SmartRefreshLayout refreshLayout;
    private FruitAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();
        initView();
    }

    private void initView() {
        
        recyclerView = findViewById(R.id.recycler_view);
        refreshLayout = findViewById(R.id.refreshLayout);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new FruitAdapter(fruitList);
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                //下拉刷新
                Log.e(TAG,"onRefresh");
                RefreshFruits();
                adapter.notifyDataSetChanged();
                refreshLayout.finishRefresh(true);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //上拉加载
                Log.e(TAG,"onLoadMore");
                loadFruits();
                adapter.notifyDataSetChanged();
                refreshLayout.finishLoadMore(true);
            }
        });
    }

    private void loadFruits() {
        Log.e(TAG,"loadFruits");
        for (int i = 0; i < 2; i++) {
            Fruit banana = new Fruit("西瓜", "5", R.mipmap.xigua);
            fruitList.add(banana);
        }
    }

    private void RefreshFruits() {
        Log.e(TAG,"RefreshFruits");
        fruitList.clear();
        for (int i = 0; i < 4; i++) {
            Fruit apple = new Fruit("西瓜", "10", R.mipmap.xigua);
            fruitList.add(apple);
            Fruit banana = new Fruit("草莓", "10", R.mipmap.cm);
            fruitList.add(banana);
        }
    }

    private void initFruits() {
        Log.e(TAG,"initFruits");
        for (int i = 0; i < 3; i++) {
            Fruit apple = new Fruit("苹果", "20", R.mipmap.apple);
            fruitList.add(apple);
            Fruit banana = new Fruit("香蕉", "20", R.mipmap.banana);
            fruitList.add(banana);
        }
    }
}

6、缺少的item布局文件,图片资源麻烦各位自己动动小手来实现喽,这样毕竟你能增加记忆的。

以上就是实现框架的代码,具体一些问题,可通过查看源码,里面也有问题解决方式。

奥里给!!!

你可能感兴趣的:(Android)