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



我将MainActivity作为

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

然后,我定义了ListViewFragment和ListViewAdapter类,它们运行良好。

 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.我在当前列表顶部看到新文本查看项目。如果我使用R.id.listViewSensors,我会得到错误java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView

简单的答案是:你不能。

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

OnClickListener在视图中有可聚焦项目时不会触发。我不得不在activity_main中添加android:focutable="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网页碎片。在文本代码中搜索"公共静态类DetailsFragment扩展Fragment"。您可以覆盖CreateView上的方法。看来你对视图有问题。

相关内容

最新更新