RecyclerView(滚动控件)的用法

Posted yl007

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecyclerView(滚动控件)的用法相关的知识,希望对你有一定的参考价值。

1.首先在build.gradle中添加依赖库

  compile ‘com.android.support:recyclerview-v7:24.2.1‘

2.修改activity_main.xml

<LinearLayout ......
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="maych_parent"
        android:layout_height="match_parent" />
</LinearLayout>

3.类似ListView,新建一个适配器FruitAdapter,定义一个ViewHolder内部类,传入一个View参数,重写onCreateViewHolder()、onBindViewHolder()、getItemCount()

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder>{
    private List<Fruit> mFruitList;
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView Fruitimage;
        TextView FruitName;
  //创建ViewHolder内部类
public ViewHolder(View view){ super(view); fruitImage = (ImageView)findViewById(R.id.fruit_image); fruitName = (TextView)findViewById(R.id.fruit_name); } } public FruitAdapter(List<Fruit> fruitList){ mFruitList = fruitList; }
  //创建ViewHolder实例 @Override
public ViewHolder onCreateViewHolder(ViewGroup parent,int viewType){ View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fruit_item,parent,false); ViewHolder holder = new ViewHolder(view); return holder; }
  //为子项赋值 @Override
public void onBindViewHolder(ViewHolder holder,int position){ Fruit fruit = mFruitList.get(position); holder.fruitImage.setImageResource(fruit.getImageId()); holder.fruitName.setText(fruit.getName()); }
  //子项的数目 @Override
public int getItemCount(){ return mFruitList.size(); } }

 4.在MainAcitivity中编写RecyclerView代码

public class MainActivity extends AppCompatActivity{
    private Lsit<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);
        recyclerView.setLayoutManager(layoutManager);    //指定布局方式
        FruitAdapter adapter = new FruitAdapter(fruitList);
        recyclerView.setAdapter(adapter);
    }
    private void initFruits(){
        for(int i=0;i<2;i++){
            Fruit apple = new Fruit("Apple",R.drawable.apple_pic);
            fruitList.add(apple);
            ......
        }
    }
}

 

RecyclerView特殊用法

  ListView只能实现纵向滚动,而RecyclerView可以实现横向和瀑布流。

  横向滚动:

    修改fruit_item.xml中LinearLayout布局方向:android:orientation="vartical";  并将ImageView和TextView设置成水平居中。

    在MainActivity中onCreate()添加:layoutManager.setOrientation(LinearLayoutManger.HORIZONTAL);

  瀑布流:

    调整fruit_item.xml

    在MainActivity中onCreate()添加:staggeredGridLayoutManager layoutManager = staggeredGridLayoutManager(3,StaggeredGridLayoutManger.VERTICAL); //1参:列数,2参:排列方向

 

RecyclerView的点击事件

  

......
    @Override
    public 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 v){
                int position = holder.getAdapterPosition();
                Fruit fruit = mFruitList.get(position);
                Toast.makeText(v.getContext(),"you clicked view"+fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });
        holder.fruitImage.setOnClickListener(new View.onClickListener(){
            @Override
            public void onClick(View v){
                int position = holder.getAdapterPosition();
                Fruit fruit = mFuritList.get(position);
                Toast.makeText(v.getContext(),"you clicked image"+fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });
        return holder;
    }
......

 

    

以上是关于RecyclerView(滚动控件)的用法的主要内容,如果未能解决你的问题,请参考以下文章

Android10.5 滚动视图(RecyclerView)

Recyclerview 滚动在嵌套滚动视图中的片段中不起作用

RecyclerView 可以与其余的片段布局一起滚动吗?

我应该使用 ScrollView 还是 RecyclerView 在片段中滚动?

从其他活动返回到同一片段时,如何保存和恢复片段中 RecyclerView 的滚动位置?

从 ListView 到 RecyclerView 的用法浅析