Android滑动组件----RecyclerView并且实现点击事件(2)

我们知道ListView的扩展性并不好,它只能实现纵向滚动的效果,如果想进行横向滚动的话,那么ListView就做不到了。刚好RecyclerView能做到。
接着上篇文章的代码进行改动。

首先我们对fruit_item布局进行修改:




    

    


接着我们修改FruitAdapter的代码:

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

    //构造函数将传入的fruitList赋值给mFruitList
    public FruitAdapter(List fruitList){
        this.mFruitList = fruitList;
    }

    //这里定义的是我们FruitAdapter的内部泛型类  就是类似与数据模型
    static class ViewHolder extends RecyclerView.ViewHolder{
        View fruitView;

        ImageView fruitImage;
        TextView fruitName;

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

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载  加载的xml布局  父组件  false  固定写法
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false);
        //初始化ViewHolder
        final ViewHolder holder = new ViewHolder(view);
        /*
        * 下面分别给holder的fruitView和fruitImage分别注册点击事件
        * */
        holder.fruitView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取点击的位置----即点击的是第几个(index)
                int position = holder.getAdapterPosition();
                //从mFruitList中获取Fruit的实例
                Fruit fruit = mFruitList.get(position);
                Toast.makeText(view.getContext(),"点击了" + position + "个" + fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });
        holder.fruitImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取 点击的位置----即点击的是第几个(index)
                int position = holder.getAdapterPosition();
                //从mFruitList获取Fruit的实例
                Fruit fruit = mFruitList.get(position);
                Toast.makeText(view.getContext(),"点击了" + position + "个" + fruit.getImageId(),Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //获取Fruit实例
        Fruit fruit = mFruitList.get(position);
        //设置ViewHolder模型的资源
        holder.fruitImage.setImageResource(fruit.getImageId());
        holder.fruitName.setText(fruit.getName());
    }

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

然后我们修改MainActivity中的代码:

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private List fruitList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruit();  //初始化水果数据
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);  //这里的layoutManager用于指定recyclerView的布局方式
        FruitAdapter adapter = new FruitAdapter(fruitList);
        recyclerView.setAdapter(adapter);
    }

    private void initFruit() {
        for(int i = 0;i < 4;i++){
            Fruit apple = new Fruit(getRandomLengthName("apple"),R.drawable.ic_launcher_background);
            Fruit banana = new Fruit(getRandomLengthName("banana"),R.drawable.ic_launcher_background);
            Fruit orange = new Fruit(getRandomLengthName("orange"),R.drawable.ic_launcher_background);
            Fruit watermelon = new Fruit(getRandomLengthName("watermelon"),R.drawable.ic_launcher_background);
            Fruit strawberry = new Fruit(getRandomLengthName("strawberry"),R.drawable.ic_launcher_background);
            Fruit purple = new Fruit(getRandomLengthName("purple"),R.drawable.ic_launcher_background);
            fruitList.add(apple);
            fruitList.add(banana);
            fruitList.add(orange);
            fruitList.add(watermelon);
            fruitList.add(strawberry);
            fruitList.add(purple);
        }
    }

    private String getRandomLengthName(String name){
        Random random = new Random();
        int length = random.nextInt(20) + 1;
        StringBuilder builder = new StringBuilder();
        for(int i = 0;i < length;i++){
            builder.append(name);
        }
        return  builder.toString();
    }
}

运行结果::::

好了我们做完了。这里也没什么好说的,基本的注释写好了。

成功又水了一篇文章。

(这一段时间Flutter群里的小伙伴做的越来越多,大部分也是问的provider的使用。现在还是不太想发出来,因为很多人不看文档直接拿去用,这几天我在慢慢的封装一下,准备老少皆宜都能用。准备过几天,放一下provider的使用类文章,包括我自己的provider封装以及多state的封装)

你可能感兴趣的:(Android)