Android recycleview 多布局

本文介绍如何在Android开发中使用RecyclerView实现不同类型的布局切换。通过设置不同的item视图类型,可以根据数据源的变化来动态调整列表项的显示样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android recycleview 多布局

在Android开发项目中我们会经常遇到过列表多部局 如下:
这里写图片描述
那么这又是如何实现的呢其实很简单
main.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">

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

布局写完了开始写代码


/**
 * Created by zhm .
 */

public class RecyCleTypeClass extends Activity {
    private RecyclerView mRecycleView;
    private YueZhiPerSionAdapter adapter;
    private List<Map<String, String>> mapList;
    private Map<String, String> map;
    //数据源
    private List<String> ml = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mRecycleView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecycleView.setLayoutManager(new LinearLayoutManager(this));
        mapList = new ArrayList<>();//这里模拟服务器返回来的数据类型
        ml.add("类型1");
        ml.add("类型2");
        ml.add("类型2");
        ml.add("类型1");
        ml.add("类型2");
        ml.add("类型1");
        for (int i = 0; i < ml.size(); i++) {
            map = new HashMap<>();
            if (ml.get(i).equals("类型1")) {
                map.put("ListType", "0");
                map.put("wenzi", "这是类型1横向排列");
            } else {
                map.put("ListType", "1");
                map.put("tupian", "这是类型2纵向排列" +
                        "");

            }
            mapList.add(map);
        }


        adapter = new YueZhiPerSionAdapter(mapList);
        mRecycleView.setAdapter(adapter);
    }
}

自定义Adapter

package com.yuezhi.ap50;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import java.util.List;
import java.util.Map;

/**
 * Created by PVer on 2017/7/31.
 * <p>
 * RecycleView 多种类型
 */

public class YueZhiPerSionAdapter extends RecyclerView.Adapter {
    private View mView;
    private List<Map<String, String>> mapList;
    public static final int VALUE1 = 1;
    public static final int VALUE2 = 2;

    public YueZhiPerSionAdapter(List<Map<String, String>> mapList) {
        this.mapList = mapList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        VideoViewHolder mViewHolder;
        TextViewHolder mTViewHolder;
        if (viewType == VALUE1) {
            mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.one, null);
            mTViewHolder = new TextViewHolder(mView);
            return mTViewHolder;

        } else if (viewType == VALUE2) {
            mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.two, null);
            mViewHolder = new VideoViewHolder(mView);
            return mViewHolder;
        }
        return null;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VideoViewHolder) {
            ((VideoViewHolder) holder).yuezhi_radio_content.setText(mapList.get(position).get("tupian"));

        } else if (holder instanceof TextViewHolder) {
            ((TextViewHolder) holder).text_number.setText(mapList.get(position).get("wenzi"));
        }
    }

    @Override
    public int getItemCount() {
        return mapList == null ? 0 : mapList.size();
    }

    @Override
    public int getItemViewType(int position) {
        int Type = Integer.parseInt(mapList.get(position).get("ListType"));
        switch (Type) {
            case 0:
                return VALUE1;  //文字
            case 1:
                return VALUE2;    //图片
            default:
                return -1;
        }
    }


    public static class TextViewHolder extends RecyclerView.ViewHolder {
        public TextView text_number;

        public TextViewHolder(View itemView) {
            super(itemView);
            text_number = (TextView) itemView.findViewById(R.id.leixing1);

        }
    }

    public static class VideoViewHolder extends RecyclerView.ViewHolder {
        public TextView yuezhi_radio_content;

        public VideoViewHolder(View itemView) {
            super(itemView);
            yuezhi_radio_content = (TextView) itemView.findViewById(R.id.leixing2);

        }
    }
}

item 布局控件 one.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:layout_marginTop="50dp"
    android:gravity="center"
    android:background="#ccffee"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/leixing1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:text="version:1.1.0" />

    <TextView
        android:drawableRight="@drawable/ic_menu_camera"
        android:layout_width="60dp"
        android:layout_height="50dp"
        android:background="#eeddff"
        android:text="" />
</LinearLayout>

two.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:background="#eedd"
    android:layout_marginTop="20dp"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/ic_menu_camera" />

    <TextView
        android:id="@+id/leixing2"
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="match_parent"
        android:text="类型1" />

</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值