android - 如何使用 OnListItemClick Action 打开一个新的 ListFragment?

标签 android android-listfragment

我有以下 ListItem 监听器,当我单击其中一个列表打开另一个具有不同选项的列表时,我需要它。

public class MyList extends ListFragment {

    ...

    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        switch (position) {
        case 0:
            Intent newActivity = new Intent(v.getContext(), AnotherList.class);
            startActivity(newActivity);
        }
    }
}

这是我希望它在我选择第一个选项时打开的另一个列表

public class AnotherList extends ListFragment {

    ArrayList<String> storage = new ArrayList<String>(
            Arrays.asList("Test", "Test"));
    ArrayAdapter<String> adapter;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        adapter = new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_1, storage);

        setListAdapter(adapter);

    }
}

虽然我收到以下错误消息

06-02 11:52:39.251: E/AndroidRuntime(1231): android.content.ActivityNotFoundException: Unable to find explicit activity class {.....}; have you declared this activity in your AndroidManifest.xml?

我是否在 list 文件中声明了 AnotherList?奇怪的是我必须这样做,因为我没有用我的第一个 ListFragment 这样做。

更新:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.list_fragment, new AnotherList());
ft.commit();

OLD main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <fragment
        android:id="@+id/list_fragment"
        android:name="com.sanguosha.MyList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

新的工作 main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/list_fragment"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" ></LinearLayout>

</LinearLayout>

最佳答案

  1. 应该是开始 fragment 而不是其他 fragment 的 Activity
  2. fragment

    Intent newActivity = new Intent(v.getContext(), AnotherList.class);
    startActivity(newActivity);
    

错了。您必须使用 startActivity 来启动 Activity 而不是 Fragment。对于 fragment ,您必须使用 FragmentTransaction

关于android - 如何使用 OnListItemClick Action 打开一个新的 ListFragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16882669/

相关文章:

android - ListFragment 从 BackStack 返回后加载空列表

java - Android - 定时器概念

android - Jetpack Compose 滚动 LazyColumn with CoroutineScope 结果错误 A MonotonicFrameClock 在此 CoroutineContext 中不可用

android - 在 Android 中显示 ArrayAdapter 的空 View

android - 单击该 ListItem 中的 RadioButton 时获取 ListItem id/位置。

android - 使用 ListFragment 填充自定义 View 列表

java - 无法使用异步任务复制程序中给出 URL 的网页的 html 代码。安卓工作室3.4.2

android - 当 Android 主屏幕 PWA 被杀死时,Cookie session 被破坏

Android:RemoteConnection 初始化失败:RemoteConnection 无法打开管道

android - 如何正确连接 SQLite 数据库和 ListFragment?