【问题标题】:Android use id from another layout in MainActivityAndroid 使用 MainActivity 中另一个布局的 id
【发布时间】:2020-12-31 12:55:34
【问题描述】:

我创建了一个使用 Navigation Drawer Activity(Android Studio 内置)的 Android 应用程序。现在我将一个 ListView 添加到一个名为 fragment_home.xml 的片段中。这是我的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/songList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.home.HomeFragment">

    <ListView
        android:id="@+id/myListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:listSelector="#2196F3"
        />
</RelativeLayout>

这是我的HomeFragment.java 文件

import androidx.fragment.app.Fragment;
 
public class HomeFragment extends Fragment {

}

现在我想使用来自 fragment_home 布局的 ListView 到我的MainActivity class。那么我该怎么做。 请帮帮我

【问题讨论】:

    标签: android listview android-activity layout fragment


    【解决方案1】:

    我建议您使用 recyclerView 而不是列表视图:

    您可以查看此官方文档以获取更多详细信息:

    Create a List with RecyclerView

    【讨论】:

      猜你喜欢
      • 2015-03-16
      • 2017-07-06
      • 2018-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多