91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

如何在android中嵌套二級列表

小樊
83
2024-10-13 03:10:30
欄目: 編程語言

在Android中嵌套二級列表,通常可以使用RecyclerView結合LinearLayoutManager來實現。以下是一個簡單的步驟指南,幫助你實現嵌套二級列表:

1. 添加依賴

首先,在你的build.gradle文件中添加RecyclerView的依賴:

dependencies {
    implementation 'com.android.support:recyclerview-v7:28.0.0'
}

2. 創建布局文件

在你的主布局文件中添加RecyclerView

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

3. 創建數據模型

創建一個數據模型來表示你的列表項。例如,假設你有一個包含列表項的列表:

public class MainList {
    private List<String> mainItems;

    public MainList() {
        mainItems = new ArrayList<>();
        mainItems.add("Item 1");
        mainItems.add("Item 2");
        mainItems.add("Item 3");
    }

    public List<String> getMainItems() {
        return mainItems;
    }
}

對于每個主列表項,你可以創建一個子列表:

public class SubList {
    private List<String> subItems;

    public SubList() {
        subItems = new ArrayList<>();
        subItems.add("Sub Item 1");
        subItems.add("Sub Item 2");
        subItems.add("Sub Item 3");
    }

    public List<String> getSubItems() {
        return subItems;
    }
}

4. 創建適配器

創建一個適配器來填充RecyclerView

public class MainListAdapter extends RecyclerView.Adapter<MainListAdapter.MainViewHolder> {
    private List<MainList> mainLists;

    public MainListAdapter(List<MainList> mainLists) {
        this.mainLists = mainLists;
    }

    @NonNull
    @Override
    public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        return new MainViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MainViewHolder holder, int position) {
        MainList mainList = mainLists.get(position);
        holder.textViewMain.setText(mainList.getMainItems().get(0)); // 假設每個主列表項只有一個子列表

        // 創建子列表適配器
        SubListAdapter subListAdapter = new SubListAdapter(mainList.getSubItems());
        holder.recyclerViewSubList.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext(), LinearLayoutManager.VERTICAL, false));
        holder.recyclerViewSubList.setAdapter(subListAdapter);
    }

    @Override
    public int getItemCount() {
        return mainLists.size();
    }

    static class MainViewHolder extends RecyclerView.ViewHolder {
        TextView textViewMain;
        RecyclerView recyclerViewSubList;

        MainViewHolder(View itemView) {
            super(itemView);
            textViewMain = itemView.findViewById(R.id.textViewMain);
            recyclerViewSubList = itemView.findViewById(R.id.recyclerViewSubList);
        }
    }
}

5. 創建子列表適配器

創建一個適配器來填充子列表:

public class SubListAdapter extends RecyclerView.Adapter<SubListAdapter.SubViewHolder> {
    private List<String> subItems;

    public SubListAdapter(List<String> subItems) {
        this.subItems = subItems;
    }

    @NonNull
    @Override
    public SubViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sub_list_item, parent, false);
        return new SubViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SubViewHolder holder, int position) {
        holder.textViewSub.setText(subItems.get(position));
    }

    @Override
    public int getItemCount() {
        return subItems.size();
    }

    static class SubViewHolder extends RecyclerView.ViewHolder {
        TextView textViewSub;

        SubViewHolder(View itemView) {
            super(itemView);
            textViewSub = itemView.findViewById(R.id.textViewSub);
        }
    }
}

6. 初始化RecyclerView

在你的Activity或Fragment中初始化RecyclerView并設置適配器:

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerViewMain;
    private MainListAdapter mainListAdapter;
    private List<MainList> mainLists;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerViewMain = findViewById(R.id.recyclerView);
        recyclerViewMain.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        mainLists = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            MainList mainList = new MainList();
            mainLists.add(mainList);
        }

        mainListAdapter = new MainListAdapter(mainLists);
        recyclerViewMain.setAdapter(mainListAdapter);
    }
}

7. 創建布局文件

創建list_item.xmlsub_list_item.xml布局文件:

list_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/textViewMain"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textStyle="bold"/>

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

sub_list_item.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:id="@+id/textViewSub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"/>
</LinearLayout>

這樣,你就成功地在Android中實現了一個嵌套的二級列表。

0
门头沟区| 桓台县| 潼关县| 洪泽县| 将乐县| 汾阳市| 吉林省| 河源市| 北安市| 南澳县| 和平区| 和静县| 陈巴尔虎旗| 长海县| 泾源县| 玛多县| 磴口县| 当阳市| 怀集县| 镶黄旗| 南漳县| 仁怀市| 枞阳县| 神池县| 镶黄旗| 富蕴县| 长葛市| 灌云县| 山东省| 泸溪县| 兰溪市| 丹寨县| 方山县| 闸北区| 金门县| 襄汾县| 汉沽区| 旬阳县| 蒙山县| 东海县| 遂平县|