android - 从一个 fragment 调用另一个 fragment

标签 android list android-fragments tabview

我不太擅长解释事情,我会尽力做到最好:)

我试图使用 Tabhost 并在每个选项卡中显示列表 fragment 。

[选项卡 View 中的ListFragments]:enter image description here

这就是它的样子。

单击列表元素时,会调用另一个 fragment 来显示详细信息,如下所示。 [单击列表元素]:enter image description here 有2个问题。

  1. DetailsFragment 未全屏。即能够看到列表(您可以在上图中看到)
  2. 当我返回选项卡 View 并单击列表时,它显示: “java.lang.IllegalStateException:适配器的内容已更改,但 ListView 未收到通知。请确保适配器的内容不是从后台线程修改,而是仅从 UI 线程修改 . [在 ListView(16908298, class android.widget.ListView) 和 Adapter(class android.widget.SimpleAdapter)] ” TabView xml:

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
    
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0"
                android:orientation="horizontal" />
    
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />
    
            <FrameLayout
                android:id="@+android:id/realtabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1" />
        </LinearLayout>
    </TabHost>
    

详细信息 fragment xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container1"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:shrinkColumns="*"
            android:stretchColumns="*" >

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="50dip"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Msg Id"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv1"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="PID"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv2"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Sender BS"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv3"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Sender NS"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv4"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Sender Interface"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv5"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Receiver BS"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv6"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Receiver NS"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv7"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Receiver Interface"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv8"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Msg Type"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv9"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Msg State"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv10"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Exe Time"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv11"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Init Time"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv12"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Send Time"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv13"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Admin User"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv14"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:text="Payload"
                    android:textColor="#000000"
                    android:textSize="13sp" />

                <TextView
                    android:id="@+id/tv15"
                    android:layout_weight="1"
                    android:background="#a09f9f"
                    android:gravity="center"
                    android:paddingBottom="5sp"
                    android:paddingTop="5sp"
                    android:textColor="#000000"
                    android:textSize="13sp" />
            </TableRow>
        </TableLayout>
    </ScrollView>

</FrameLayout>

提前致谢

最佳答案

java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification.

Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. [in ListView(16908298, class android.widget.ListView) with Adapter(class android.widget.SimpleAdapter)] "

添加项目后,请在 UI 线程中调用 notifyDataSetChanged()yourListView.requestLayout()。问题是绑定(bind)到 Adapter 的集合在后台线程。因此,解决方案是将其移动到 UI 线程(如上所述)或简单地用 runOnUiThread() 包装它。

runOnUiThread(new Runnable() {
  public void run() {
    your_collection.add(item);
    your_adapter.notifyDataSetChanged();
    yourListView.requestLayout();
  }
});

将另一个 fragment 称为当前 fragment :

SecondFragment secFrag = new SecondFragment();
FragmentTransaction fragTransaction = getFragmentManager().beginTransaction();
                    fragTransaction.replace(R.id.frame_fragment,secFrag );
                    fragTransaction.addToBackStack(null);
                    fragTransaction.commit();

编辑:

要全屏显示详细信息 fragment ,请进行以下更改:

  1. 在你的 ScrollView 中 将 android:layout_height="wrap_content" 替换为 android:layout_height="fill_parent"

  2. 在您的表格布局中 将 android:layout_height="wrap_content" 替换为 android:layout_height="fill_parent"

关于android - 从一个 fragment 调用另一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18375149/

相关文章:

应用计费中的 Android 市场 - 新信用卡失败,然后好吗?

android - 如何在Android中使用Groupie在Recyclerview中实现HeaderItems

Android:ShowCase View 只显示一次并重复动画

java - 从 Activity B 返回到 Activity A 的 fragment

android - 测试时,Hilt Fragment 必须附加到@AndroidEntryPoint Activity (它附加到@AndEntPoint 标记的 Activity )

不同 Action 的android sharingIntent

android - 为 Android 使用 OpenCv contrib 模块

list - 改进 Prolog 范围内的列表生成

python - 在 Python 中格式化列表

c# - 访问另一个类中列表的元素