Android - RecyclerView

0.添加引用

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

命名空间:

android.support.v7.widget.RecyclerView
android.support.v7.widget.LinearLayoutManager 

1.首先添加控件

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

2.子布局

    <ImageView
        android:id="@+id/recyclerview_item_iv"
        tools:background="@drawable/img2"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="80dp"
        android:layout_height="80dp"/>

    <TextView
        android:id="@+id/recyclerview_item_tv"
        android:text="This is a computer!"
        android:layout_marginTop="30dp"
        android:textSize="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

3.Adpater


public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {

    private List<String> list;
    private Context mContext;

    public MyRecyclerViewAdapter(Context context, List<String> list) {
        mContext=context;
        this.list = list;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView imageView;
        TextView textView;
        View view;
        public ViewHolder(View itemView) {
            super(itemView);
            textView=(TextView)itemView.findViewById(R.id.recyclerview_item_tv);
            imageView=(ImageView)itemView.findViewById(R.id.recyclerview_item_iv);
            view=itemView;
        }
    }

    @Override
    public MyRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view =View.inflate(parent.getContext(),R.layout.recyclerview_item_layout,null);
        final ViewHolder holder = new ViewHolder(view);

        //整个子项点击事件
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, "Item:"+String.valueOf(holder.getAdapterPosition()), Toast.LENGTH_SHORT).show();
            }
        });

        // 图片点击事件
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mContext, String.valueOf(holder.getAdapterPosition()), Toast.LENGTH_SHORT).show();
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(MyRecyclerViewAdapter.ViewHolder holder, int position) {

        holder.textView.setText(list.get(position));
        if(position%2==0){
            holder.imageView.setImageResource(R.drawable.img2);
        }else {
            holder.imageView.setImageResource(R.drawable.img3);
        }
    }

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

4.MainActivity

        
        //初始化数据
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add("This is computer "+String.valueOf(i));
        }

        RecyclerView recyclerView=(RecyclerView)findViewById(R.id.activity_recycler_view_recyclerView);
        MyRecyclerViewAdapter adapter=new MyRecyclerViewAdapter(this,list);
        recyclerView.setAdapter(adapter);

        //设置布局方式
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        // 瀑布式流布局                                                              // 3 表示布局的列数
        /*StaggeredGridLayoutManager layoutManager=new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);*/

5.点击回到顶部

recyclerView.scrollToPosition(0)
posted @ 2018-01-14 16:30  -Tiger  阅读(84)  评论(0编辑  收藏