如何用另一个片段视图替换当前片段视图

How to replace current fragment view with another fragment view

我有 MainActivity 作为

listViewFragment = new ListViewFragment();

    getFragmentManager().beginTransaction().add(R.id.fragmentSensors, listViewFragment).commit();

然后我定义了 ListViewFragment 和 ListViewAdapter class,它们工作正常。

 listViewSensors.setAdapter(new ListViewAdapter(getActivity(), sensorArray));

但是当我定义 onClicklistener 时,当前视图不会被新的片段视图所取代。

这是布局文件。 activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"  tools:context=".MainActivity">

    <fragment android:id="@+id/fragmentSensors" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.testapp.fragment.ListViewFragment"/>

</RelativeLayout>

listview_fragment

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"  tools:context=".MainActivity">

    <ListView android:id="@+id/listViewSensors" android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

listview_adapter 有 imageView 和 textView。

然后我把test_fragment定义为

<?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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello Test"
        android:id="@+id/textViewTest"/>

</LinearLayout>

并通过ListViewFragment调用为

@Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        accelFragment = new AccelFragment();
        testFragment = new TestFragment();

        getFragmentManager().beginTransaction().replace(R.id.fragmentSensors, testFragment).commit();


        Toast.makeText(getActivity(),"Ok", Toast.LENGTH_SHORT).show();
    }

但是没有任何反应,不过我看到 Toast 弹出窗口。如果我使用 android.R.id.content。我在当前 listView 项目的顶部看到了新文本。如果我使用 R.id.listViewSensors 我得到错误 java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView

简单的答案是:你不能

您在布局文件中定义的任何片段都无法替换。 您只能替换 动态添加 的片段,即通过 FragmentTransaction 创建

如果视图中有可聚焦的项目,则不会触发 OnClickListener。我必须在 activity_main 中添加 android:focusable="false",如下所示。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"  tools:context=".MainActivity"
    android:layout_weight="1"
    >

    <RelativeLayout android:id="@+id/fragmentSensors" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.skakade.testsensorapp.fragment.ListViewFragment"
        android:focusable="false"/>

</RelativeLayout>

查看代码示例@Google 网页Fragments。搜索 "public static class DetailsFragment extends Fragment" 的文本代码。您可以覆盖方法 onCreateView。您的视图似乎有问题。