AndroidRecycleView⾃定义Item的使⽤
⾃定义布局的RecycleView需要⾃⼰实现Adapter,ViewHolder和布局;
⾃定义Adapter继承RecycleView.Adapter,重写getItemCount(),onBindViewHolder()和onCreateViewHolder();
⾃定义ViewHolder继承于RecycleView.ViewHolder;
getItemCount()返回RecycleView中Item的个数,onBindViewHolder()主要实现数据和布局的绑定,onCreateViewHolder()加载布局;效果:
适配器代码:
1package leviewdemo;
2
3import android.support.annotation.NonNull;
4import android.support.v7.widget.RecyclerView;
5import android.view.LayoutInflater;
6import android.view.View;
7import android.view.ViewGroup;
8import android.widget.ImageView;
9import android.widget.TextView;
10
11import java.util.List;
12
13class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ItemHolder> { 14
15private List<Item> mItems;
16
17    RecycleViewAdapter(List<Item> items) {
18        mItems = items;
19    }
20
21    @Override
22public int getItemCount() {
23return mItems.size();
24    }
25
26    @Override
27public void onBindViewHolder(@NonNull ItemHolder holder, int position) {
28//设置Item图⽚
29        holder.image.(position).getImageResId());
30//设置Item⽂字
31        holder.title.(position).getTitle());
32    }
33
34    @NonNull
35    @Override
36public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
37return new ItemHolder(LayoutInflater
38                .Context()).inflate(R.layout.item, parent, false));
39    }
40
41class ItemHolder extends RecyclerView.ViewHolder {
42
43        ImageView image;
44        TextView title;
45
46        ItemHolder(View item) {
47super(item);
48            image = item.findViewById(R.id.image_view);
49            title = item.findViewById(_title);
50        }
51    }
52 }
Item类:
1package leviewdemo;
2
3class Item {
4
5private int mImageResId;
6private String mTitle;
7
8    Item(int imageResId, String title) {
9        mImageResId = imageResId;
10        mTitle = title;
11    }
12
13public int getImageResId() {
14return mImageResId;
15    }
16
17public void setImageResId(int imageResId) {
18        mImageResId = imageResId;
19    }
20
21public String getTitle() {
22return mTitle;
23    }
24
25public void setTitle(String title) {
26        mTitle = title;
27    }
28 }
android layout布局
MainActivity代码:
1package leviewdemo;
2
3import android.support.v7.app.AppCompatActivity;
4import android.os.Bundle;
5import android.support.v7.widget.DefaultItemAnimator;
6import android.support.v7.widget.LinearLayoutManager;
7import android.support.v7.widget.RecyclerView;
8
9import java.util.ArrayList;
10import java.util.List;
11
12public class MainActivity extends AppCompatActivity {
13
14private RecyclerView mRecyclerView;
15private LinearLayoutManager mLinearLayoutManager;
16private RecycleViewAdapter mRecycleViewAdapter;
17
18private List<Item> mData;
19
20    @Override
21protected void onCreate(Bundle savedInstanceState) {
23        setContentView(R.layout.activity_main);
24
25        bindViews();
26        initData();
27//初始化线性布局管理器
28        mLinearLayoutManager = new LinearLayoutManager(this);
29//设置布局管理器
30        mRecyclerView.setLayoutManager(mLinearLayoutManager);
31        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
32//初始化适配器
33        mRecycleViewAdapter = new RecycleViewAdapter(mData);
34//设置适配器
35        mRecyclerView.setAdapter(mRecycleViewAdapter);
36    }
37
38private void bindViews() {
39        mRecyclerView = findViewById(le_view);
40    }
41
42private void initData() {
43        mData = new ArrayList<>();
44        mData.add(new Item(R.drawable.witcher, "The Witcher 3: Wild Hunt"));
45        mData.add(new Item(, "Recore"));
46        mData.add(new Item(igins, "Assassin's Creed Origins"));
47        mData.add(new Item(R.drawable.hunter, "theHunter: Call of the Wild"));
48    }
49 }
Item布局:
1<?xml version="1.0" encoding="utf-8"?>
2<straint.ConstraintLayout
3xmlns:android="schemas.android/apk/res/android"
4    xmlns:app="schemas.android/apk/res-auto"
5    xmlns:tools="schemas.android/tools"
6    android:orientation="vertical"
7    android:layout_width="match_parent"
8    android:layout_height="wrap_content"
9    android:layout_margin="4dp"
10    android:elevation="2dp"
11    android:background="@drawable/background_item">
12
13<ImageView
14android:id="@+id/image_view"
15        android:contentDescription="@null"
16        android:layout_width="0dp"
17        android:layout_height="235dp"
18        android:layout_margin="6dp"
19        tools:src="@drawable/witcher"
20        app:layout_constraintTop_toTopOf="parent"
21        app:layout_constraintStart_toStartOf="parent"
22        app:layout_constraintEnd_toEndOf="parent"/>
23
24<TextView
25android:id="@+id/text_title"
26        android:layout_width="wrap_content"
27        android:layout_height="wrap_content"
28        android:textSize="18sp"
29        android:textColor="#000000"
30        android:layout_margin="6dp"
31        android:paddingBottom="4dp"
32        app:layout_constraintTop_toBottomOf="@+id/image_view"
33        app:layout_constraintStart_toStartOf="parent"
34        app:layout_constraintBottom_toBottomOf="parent"
35        tools:text="The Witcher 3: Wild Hunt"/>
36
37</straint.ConstraintLayout>
MainActivity布局:
1<?xml version="1.0" encoding="utf-8"?>
2<straint.ConstraintLayout xmlns:android="schemas.android/apk/res/android"
3    xmlns:app="schemas.android/apk/res-auto"
4    xmlns:tools="schemas.android/tools"
5    android:layout_width="match_parent"
6    android:layout_height="match_parent"
7    tools:context=".MainActivity">
8
9<android.support.v7.widget.Toolbar
10android:id="@+id/toolbar"
11        android:layout_width="match_parent"
12        android:layout_height="48dp"
13        android:background="@color/colorPrimary"
14        android:elevation="5dp"
15        app:layout_constraintEnd_toEndOf="parent"
16        app:layout_constraintStart_toStartOf="parent"
17        app:layout_constraintTop_toTopOf="parent">
18
19<ImageView
20android:contentDescription="@null"
21            android:layout_width="36dp"
22            android:layout_height="36dp"
23            android:src="@drawable/icon"/>
24
25<TextView
26android:layout_width="wrap_content"
27            android:layout_height="wrap_content"
28            android:layout_gravity="center"
29            android:text="@string/app_name"
30            android:textColor="#FFFFFF"
31            android:textSize="18sp"/>
32
33</android.support.v7.widget.Toolbar>
34
35<android.support.v7.widget.RecyclerView
36android:id="@+id/recycle_view"
37        android:layout_width="0dp"
38        android:layout_height="0dp"
39        app:layout_constraintBottom_toBottomOf="parent"
40        app:layout_constraintEnd_toEndOf="parent"
41        app:layout_constraintStart_toStartOf="parent"
42        app:layout_constraintTop_toBottomOf="@+id/toolbar"
43        app:layout_constraintVertical_bias="1.0">
44
45</android.support.v7.widget.RecyclerView>
46
47</straint.ConstraintLayout>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。