RecyclerView每个Position显示不同布局
程序员文章站
2022-06-17 22:41:13
1.写多个布局作为ViewType插入每个Position中2.写adpater(1)用一个list来存放数据(2)为每一个ViewType创建ViewHolder类来获取布局中的view(3)onCreateViewHolder:创建holder,将view传入(4)onBindViewHolder:将资源放入holder中的view(5)getItemViewType:设置每个position对应的ViewType3.写fragment/activity(1)init数据...
1.写多个布局作为ViewType插入每个Position中
2.写adpater
(1)用一个list来存放数据
(2)为每一个ViewType创建ViewHolder类来获取布局中的view
(3)onCreateViewHolder:创建holder,将view传入
(4)onBindViewHolder:将资源放入holder中的view
(5)getItemViewType:设置每个position对应的ViewType
3.写fragment/activity
(1)init数据():将数据存入list中
(2)createContentView:inflat、init数据 、new Adapter。。。
1.布局:
(1)picture_item
<?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">
<ImageView
android:id="@+id/picture_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/picture_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="10dp"/>
</LinearLayout>
(2)picture_describe
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/picture_describe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="10dp"/>
</RelativeLayout>
(3)activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
2.Picture类
package com.example.recyclerviewdemo;
public class Picture {
private String name;
private int imageId;
private String describe;
public Picture(String name,int imageId,String describe){
this.name=name;
this.imageId=imageId;
this.describe=describe;
}
public String getName(){
return name;
}
public int getImageId(){
return imageId;
}
public String getDescribe(){
return describe;
}
}
3.Adapter
package com.example.recyclerviewdemo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class PictureAdapter extends RecyclerView.Adapter {
private List<Picture> mPictureList;
static class NameHolder extends RecyclerView.ViewHolder {
ImageView pictureImage;
TextView pictureName;
public NameHolder(View view) {
super(view);
pictureImage = (ImageView) view.findViewById(R.id.picture_image);
pictureName = (TextView) view.findViewById(R.id.picture_name);
}
}
static class DescribeHolder extends RecyclerView.ViewHolder {
TextView pictureDescribe;
public DescribeHolder(View view) {
super(view);
pictureDescribe = (TextView) view.findViewById(R.id.picture_describe);
}
}
public PictureAdapter(List<Picture> pictureList) {
mPictureList = pictureList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.picture_item, parent, false);
NameHolder nameHolder = new NameHolder(view);
return nameHolder;
case 1:
View view2 = LayoutInflater.from(parent.getContext()).inflate(R.layout.pixture_describe, parent, false);
DescribeHolder describeHolder = new DescribeHolder(view2);
return describeHolder;
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Picture picture=mPictureList.get(position);
if (holder instanceof NameHolder){
NameHolder nameHolder=(NameHolder) holder;
nameHolder.pictureImage.setImageResource(picture.getImageId());
nameHolder.pictureName.setText(picture.getName());
}
else if(holder instanceof DescribeHolder){
DescribeHolder describeHolder=(DescribeHolder) holder;
describeHolder.pictureDescribe.setText(picture.getDescribe());
}
}
@Override
public int getItemCount() {
return mPictureList.size();
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return 0;
} else {
return 1;
}
}
}
4.MainActivity
package com.example.recyclerviewdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.ServiceConnection;
import android.media.MediaPlayer;
import android.os.Bundle;
import java.util.List;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private List<Picture> fruitList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
PictureAdapter adapter = new PictureAdapter(fruitList);
recyclerView.setAdapter(adapter);
}
private void initFruits() {
for (int i = 0; i < 2; i++) {
Picture apple = new Picture("Apple", R.drawable.apple_pic,"不爱吃");
fruitList.add(apple);
Picture banana = new Picture("Banana", R.drawable.banana_pic,"不爱吃");
fruitList.add(banana);
Picture orange = new Picture("Orange", R.drawable.orange_pic,"爱吃");
fruitList.add(orange);
Picture watermelon = new Picture("Watermelon", R.drawable.watermelon_pic,"爱吃");
fruitList.add(watermelon);
Picture pear = new Picture("Pear", R.drawable.pear_pic,"不爱吃");
fruitList.add(pear);
Picture grape = new Picture("Grape", R.drawable.grape_pic,"爱吃");
fruitList.add(grape);
Picture pineapple = new Picture("Pineapple", R.drawable.pineapple_pic,"爱吃");
fruitList.add(pineapple);
Picture strawberry = new Picture("Strawberry", R.drawable.strawberry_pic,"爱吃");
fruitList.add(strawberry);
Picture cherry = new Picture("Cherry", R.drawable.cherry_pic,"爱吃");
fruitList.add(cherry);
Picture mango = new Picture("Mango", R.drawable.mango_pic,"爱吃");
fruitList.add(mango);
}
}
}
本文地址:https://blog.csdn.net/m0_37934678/article/details/107943559
推荐阅读
-
Android RecyclerView显示Item布局不一致解决办法
-
如何让echarts柱状图的每个柱子显示不同的颜色
-
Android NestedScrollView嵌套LinearLayout嵌套RecyclerView子Item布局显示不全
-
RecyclerView每个Position显示不同布局
-
css判断不同分辨率显示不同宽度布局实现自适应宽度_html/css_WEB-ITnose
-
android使用同一个RecyclerView实现两种不同Item布局
-
ListView 显示两种不同的Item 布局
-
我想根据数据库里存的每个人的不同职位值来显示对应include php文件要怎么实现
-
使用RecyclerView结合jiaozivideoplayer去加载不同类型的布局
-
我想根据数据库里存的每个人的不同职位值来显示对应include php文件要怎么实现