Android

RecyclerView使用(一)

2019-07-23  本文已影响0人  Sraindy

RecyclerView前期准备

1、依赖的工具包
implementation 'com.android.support:recyclerview-v7:27.0.0'
2、item的布局

RecyclerView简单使用步骤

一、界面如下:

ezgif.com-video-to-gif.gif

二、步骤:

1、一个继承RecyclerView.ViewHolder的类,该类用于获取每个itemview中的控件
2、一个继承RecyclerView.Adapter 的类,该类实现数据和itemview的绑定
3、item布局
4、在activity中的使用

三、代码实现

1、item布局,adapter_recyclerview.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="60dp">

        <TextView
            android:id="@+id/tv_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="A"
            android:textColor="#4DB6AC"
            android:textSize="25sp" />
</RelativeLayout>

2、继承RecyclerView.ViewHolder的类

public class RecyclerViewHolder extends RecyclerView.ViewHolder {
    public TextView tvItem;

    public RecyclerViewHolder(View itemView) {
        super(itemView);
        tvItem = (TextView) itemView.findViewById(R.id.tv_item);
    }
}

3、继承RecyclerView.Adapter的类

public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerViewHolder> {

    private List<String> mDataList;

    public RecyclerviewAdapter(List<String> mDataList) {
        this.mDataList = mDataList;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_recyclerview, parent, false);

        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerViewHolder holder, int position) {
        holder.tvItem.setText(mDataList.get(position));
    }


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

4、在acivity中使用,
xml布局:activity_recycler_view_test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="线性RecyclerView" />

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

    </android.support.v7.widget.RecyclerView>
</LinearLayout>

activity类:RecyclerViewTestActivity

public class RecyclerViewTestActivity extends AppCompatActivity {

    private RecyclerView mLineRecyclerView;
    private List<String> mDataList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_test);

        initRecyclerViewData();
        initView();
    }

    private void initRecyclerViewData() {
        for (int i = 1; i <= 99; i++) {
            String str = "测试" + i;
            mDataList.add(str);
        }

    }

    private void initView() {
        mLineRecyclerView = findViewById(R.id.rv_line);
        LinearLayoutManager tLinearLayoutManager = new LinearLayoutManager(this);

        mLineRecyclerView.setLayoutManager(tLinearLayoutManager); //线性布局

        //3.设置适配器
        RecyclerviewAdapter newsAdapter = new RecyclerviewAdapter(mDataList);
        mLineRecyclerView.setAdapter(newsAdapter);

        //添加分割线
        // mLineRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }
}

上一篇下一篇

猜你喜欢

热点阅读