关闭

RecyclerView的用法

标签: Android
344人阅读 评论(0) 收藏 举报
分类:

使用RecyclerView

1、在app/build.gradle(Module:app)文件的dependencies闭包添加依赖

compile 'com.android.support:recyclerview-v7:25.1.0'

2、编写适配器类xxxAdapter,继承自RecyclerView.Adapter,并制定泛型为 xxxAdapter.ViewHolder,这个ViewHolder是在该Adapter中定义的一个内部类。示例代码如下:

package com.songzheng.recyclerviewtest;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by make on 2017/2/5.
 */

//RecyclerView 适配器,继承自RecyclerView.Adapter
public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {

    private List<Fruit> mFruitList;

    //创建ViewHolder,继承自RecyclerView.ViewHolder
    static class ViewHolder extends RecyclerView.ViewHolder{

        ImageView fruitImage;
        TextView fruitName;
        View fruitView; //设置View最外层点击事件所需

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

    public FruitAdapter(List<Fruit> fruitList){
        mFruitList = fruitList;
    }

    //加载item,实例化ViewHolder,设置点击监听事件
    @Override
    public FruitAdapter.ViewHolder onCreateViewHolder(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(), "you clicked 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(), "You clicked image " + fruit.getName(), Toast.LENGTH_SHORT).show();
            }
        });

        return holder;
    }

    //设置控件内容
    @Override
    public void onBindViewHolder(FruitAdapter.ViewHolder holder, int position) {
        Fruit fruit = mFruitList.get(position);
        holder.fruitName.setText(fruit.getName());
        holder.fruitImage.setImageResource(fruit.getImageId());
    }

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

3、在MainActivity中为RecyclerView指定布局形式(使用LayoutManager)和适配器。

其中指定布局形式可以使用LinearLayoutManager(实现ListView类似的效果,但RecyclerView不仅可以纵向排列,还可以横向排列)或者GridLayoutManager(网格布局)或者StaggeredGridLayoutManager(瀑布流布局)。

public class MainActivity extends AppCompatActivity {

    private List<Fruit> fruitList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFruits();
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
//      LinearLayoutManager layoutManager = new LinearLayoutManager(this);      //线性布局
//      layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);           //设置布局方向为横向
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);      //瀑布流
        recyclerView.setLayoutManager(layoutManager);
        FruitAdapter adapter = new FruitAdapter(fruitList);
        recyclerView.setAdapter(adapter);
    }
}
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10733次
    • 积分:372
    • 等级:
    • 排名:千里之外
    • 原创:23篇
    • 转载:0篇
    • 译文:0篇
    • 评论:19条
    最新评论