Android 使用RecyclerView的方法

1.在build.gradle(Module:app)中添加依赖

implementation 'com.android.support:recyclerview-v7:26.1.0'

注:版本号应该与implementation 'com.android.support:appcompat-v7:26.1.0'中的版本号相同

2.新建RecyclerViewActivity和布局文件activity_recycler_view.xml

activity_recycler_view.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.wzlab.widgetbox.RecyclerViewActivity">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/rv_list"
        android:background="@color/gray"/>
LinearLayout>

RecyclerViewActivity.java

public class RecyclerViewActivity extends AppCompatActivity {

    private RecyclerView mRvList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        mRvList = findViewById(R.id.rv_list);
        mRvList.setLayoutManager(new LinearLayoutManager(RecyclerViewActivity.this));
        mRvList.setAdapter(new LinearAdapter(RecyclerViewActivity.this));

    }
}

3.为了填充数据,需要新建一个Adapter,这里起名为LinearAdapter

这里的LinearAdapter需要继承RecyclerView.Adapter,并且实现三个未完成的方法
//这里的LinearViewHolder需要自己写
public class LinearAdapter extends RecyclerView.Adapter<LinearAdapter.LinearViewHolder>{
    private Context mContext;

    public LinearAdapter(Context context){
        this.mContext = context;
    }
    //这个方法用来讲Item注入
    @Override
    public LinearAdapter.LinearViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new LinearViewHolder(LayoutInflater.from(mContext).inflate(R.layout.layout_linear_item,parent,false));
    }
    //这个方法填充数据
    @Override
    public void onBindViewHolder(LinearAdapter.LinearViewHolder holder, int position) {
        holder.mTvAuthor.setText(String.valueOf(position));
        holder.mTvMusicName.setText("music");
        holder.mTvDuration.setText("30");
    }
    //这个方法指定了显示item的数目
    @Override
    public int getItemCount() {
        return 30;
    }
    // 新建的内部类来定义item里的组件
    class LinearViewHolder extends RecyclerView.ViewHolder{

        private TextView mTvAuthor;
        private TextView mTvMusicName;
        private TextView mTvDuration;
        public LinearViewHolder(View itemView) {
            super(itemView);
            mTvAuthor = itemView.findViewById(R.id.tv_author);
            mTvMusicName = itemView.findViewById(R.id.tv_music_name);
            mTvDuration = itemView.findViewById(R.id.tv_duration);

        }
    }
}

4.给每一个Item设定一个布局,新建layout_linear_item.xml,这个布局可以按照自己的需求去设计


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_music_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_duration"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

LinearLayout>

你可能感兴趣的:(android)