Android RecycleView自定义布局的使用

著论准过秦,作赋拟子虚。这篇文章主要讲述Android RecycleView自定义布局的使用相关的知识,希望能为你提供帮助。

Android RecycleView自定义布局的使用

文章图片
自定义布局的RecycleView需要自己实现Adapter,ViewHolder和布局;
自定义Adapter继承RecycleView.Adapter,重写getItemCount(),onBindViewHolder()和onCreateViewHolder();
【Android RecycleView自定义布局的使用】自定义ViewHolder继承于RecycleView.ViewHolder;
getItemCount()返回RecycleView中Item的个数,onBindViewHolder()主要实现数据和布局的绑定,onCreateViewHolder()加载布局;
效果:
Android RecycleView自定义布局的使用

文章图片

适配器代码:
1 package com.cloud.recycleviewdemo; 2 3 import android.support.annotation.NonNull; 4 import android.support.v7.widget.RecyclerView; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.ImageView; 9 import android.widget.TextView; 10 11 import java.util.List; 12 13 class RecycleViewAdapter extends RecyclerView.Adapter< RecycleViewAdapter.ItemHolder> { 14 15private List< Item> mItems; 16 17RecycleViewAdapter(List< Item> items) { 18mItems = 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图片 29holder.image.setImageResource(mItems.get(position).getImageResId()); 30//设置Item文字 31holder.title.setText(mItems.get(position).getTitle()); 32} 33 34@NonNull 35@Override 36public ItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 37return new ItemHolder(LayoutInflater 38.from(parent.getContext()).inflate(R.layout.item, parent, false)); 39} 40 41class ItemHolder extends RecyclerView.ViewHolder { 42 43ImageView image; 44TextView title; 45 46ItemHolder(View item) { 47super(item); 48image = item.findViewById(R.id.image_view); 49title = item.findViewById(R.id.text_title); 50} 51} 52 }

Item类:
1 package com.cloud.recycleviewdemo; 2 3 class Item { 4 5private int mImageResId; 6private String mTitle; 7 8Item(int imageResId, String title) { 9mImageResId = imageResId; 10mTitle = title; 11} 12 13public int getImageResId() { 14return mImageResId; 15} 16 17public void setImageResId(int imageResId) { 18mImageResId = imageResId; 19} 20 21public String getTitle() { 22return mTitle; 23} 24 25public void setTitle(String title) { 26mTitle = title; 27} 28 }

MainActivity代码:
1 package com.cloud.recycleviewdemo; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.support.v7.widget.DefaultItemAnimator; 6 import android.support.v7.widget.LinearLayoutManager; 7 import android.support.v7.widget.RecyclerView; 8 9 import java.util.ArrayList; 10 import java.util.List; 11 12 public 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) { 22super.onCreate(savedInstanceState); 23setContentView(R.layout.activity_main); 24 25bindViews(); 26initData(); 27//初始化线性布局管理器 28mLinearLayoutManager = new LinearLayoutManager(this); 29//设置布局管理器 30mRecyclerView.setLayoutManager(mLinearLayoutManager); 31mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 32//初始化适配器 33mRecycleViewAdapter = new RecycleViewAdapter(mData); 34//设置适配器 35mRecyclerView.setAdapter(mRecycleViewAdapter); 36} 37 38private void bindViews() { 39mRecyclerView = findViewById(R.id.recycle_view); 40} 41 42private void initData() { 43mData = https://www.songbingjia.com/android/new ArrayList< > (); 44mData.add(new Item(R.drawable.witcher,"The Witcher 3: Wild Hunt")); 45mData.add(new Item(R.drawable.recore, "Recore")); 46mData.add(new Item(R.drawable.origins, "Assassin\'s Creed Origins")); 47mData.add(new Item(R.drawable.hunter, "theHunter: Call of the Wild")); 48} 49 }

Item布局:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < android.support.constraint.ConstraintLayout 3xmlns:android="http://schemas.android.com/apk/res/android" 4xmlns:app="http://schemas.android.com/apk/res-auto" 5xmlns:tools="http://schemas.android.com/tools" 6android:orientation="vertical" 7android:layout_width="match_parent" 8android:layout_height="wrap_content" 9android:layout_margin="4dp" 10android:elevation="2dp" 11android:background="@drawable/background_item"> 12 13< ImageView 14android:id="@+id/image_view" 15android:contentDescription="@null" 16android:layout_width="0dp" 17android:layout_height="235dp" 18android:layout_margin="6dp" 19tools:src="https://www.songbingjia.com/android/@drawable/witcher" 20app:layout_constraintTop_toTopOf="parent" 21app:layout_constraintStart_toStartOf="parent" 22app:layout_constraintEnd_toEndOf="parent" /> 23 24< TextView 25android:id="@+id/text_title" 26android:layout_width="wrap_content" 27android:layout_height="wrap_content" 28android:textSize="18sp" 29android:textColor="#000000" 30android:layout_margin="6dp" 31android:paddingBottom="4dp" 32app:layout_constraintTop_toBottomOf="@+id/image_view" 33app:layout_constraintStart_toStartOf="parent" 34app:layout_constraintBottom_toBottomOf="parent" 35tools:text="The Witcher 3: Wild Hunt" /> 36 37 < /android.support.constraint.ConstraintLayout>

MainActivity布局:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7tools:context=".MainActivity"> 8 9< android.support.v7.widget.Toolbar 10android:id="@+id/toolbar" 11android:layout_width="match_parent" 12android:layout_height="48dp" 13android:background="@color/colorPrimary" 14android:elevation="5dp" 15app:layout_constraintEnd_toEndOf="parent" 16app:layout_constraintStart_toStartOf="parent" 17app:layout_constraintTop_toTopOf="parent"> 18 19< ImageView 20android:contentDescription="@null" 21android:layout_width="36dp" 22android:layout_height="36dp" 23android:src="https://www.songbingjia.com/android/@drawable/icon" /> 24 25< TextView 26android:layout_width="wrap_content" 27android:layout_height="wrap_content" 28android:layout_gravity="center" 29android:text="@string/app_name" 30android:textColor="#FFFFFF" 31android:textSize="18sp" /> 32 33< /android.support.v7.widget.Toolbar> 34 35< android.support.v7.widget.RecyclerView 36android:id="@+id/recycle_view" 37android:layout_width="0dp" 38android:layout_height="0dp" 39app:layout_constraintBottom_toBottomOf="parent" 40app:layout_constraintEnd_toEndOf="parent" 41app:layout_constraintStart_toStartOf="parent" 42app:layout_constraintTop_toBottomOf="@+id/toolbar" 43app:layout_constraintVertical_bias="1.0"> 44 45< /android.support.v7.widget.RecyclerView> 46 47 < /android.support.constraint.ConstraintLayout>

 

    推荐阅读