写一个根椐返回的数据类型是图片,mp4,还是文件夹的recyclerview的列表实例
时间: 2024-03-01 16:54:21 浏览: 160
好的,以下是一个根据返回的数据类型展示不同布局的RecyclerView列表的实例:
1. 首先,在布局文件中定义RecyclerView:
```xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
```
2. 创建三种不同的布局文件,分别对应图片、MP4和文件夹类型的数据项。
image_item.xml:
```xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
```
mp4_item.xml:
```xml
<VideoView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
```
folder_item.xml:
```xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:padding="16dp"/>
```
3. 创建RecyclerView Adapter,并在其中根据数据类型加载不同的布局:
```java
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<DataItem> mDataList;
public MyAdapter(List<DataItem> dataList) {
mDataList = dataList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case DataItem.TYPE_IMAGE:
View imageView = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_item, parent, false);
return new ImageViewHolder(imageView);
case DataItem.TYPE_MP4:
View videoView = LayoutInflater.from(parent.getContext()).inflate(R.layout.mp4_item, parent, false);
return new VideoViewHolder(videoView);
case DataItem.TYPE_FOLDER:
View folderView = LayoutInflater.from(parent.getContext()).inflate(R.layout.folder_item, parent, false);
return new FolderViewHolder(folderView);
default:
return null;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
DataItem dataItem = mDataList.get(position);
switch (holder.getItemViewType()) {
case DataItem.TYPE_IMAGE:
ImageViewHolder imageViewHolder = (ImageViewHolder) holder;
// 加载图片
imageViewHolder.imageView.setImageResource(dataItem.getImageRes());
break;
case DataItem.TYPE_MP4:
VideoViewHolder videoViewHolder = (VideoViewHolder) holder;
// 加载MP4视频
videoViewHolder.videoView.setVideoURI(dataItem.getVideoUri());
break;
case DataItem.TYPE_FOLDER:
FolderViewHolder folderViewHolder = (FolderViewHolder) holder;
// 加载文件夹名称
folderViewHolder.textView.setText(dataItem.getFolderName());
break;
}
}
@Override
public int getItemViewType(int position) {
return mDataList.get(position).getType();
}
@Override
public int getItemCount() {
return mDataList.size();
}
// 图片类型的ViewHolder
private static class ImageViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
ImageViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
}
}
// MP4类型的ViewHolder
private static class VideoViewHolder extends RecyclerView.ViewHolder {
VideoView videoView;
VideoViewHolder(View itemView) {
super(itemView);
videoView = itemView.findViewById(R.id.videoView);
}
}
// 文件夹类型的ViewHolder
private static class FolderViewHolder extends RecyclerView.ViewHolder {
TextView textView;
FolderViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
}
}
}
```
4. 最后,在Activity或Fragment中使用RecyclerView和Adapter:
```java
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
List<DataItem> dataList = getDataList(); // 获取数据列表
MyAdapter adapter = new MyAdapter(dataList);
recyclerView.setAdapter(adapter);
```
其中,getDataList()方法需要根据业务逻辑实现,返回一个包含不同类型数据项的列表。每个数据项都应该包含类型信息(如DataItem.TYPE_IMAGE、DataItem.TYPE_MP4、DataItem.TYPE_FOLDER等)、对应的数据(如图片资源ID、MP4视频URI、文件夹名称等)。
阅读全文