从另一个片段调用一个片段

时间:2013-08-22 08:22:27

标签: android list android-fragments tabview

我在解释事情上非常糟糕,我会努力做到最好:)

我试图使用Tabhost并在每个标签中显示列表片段。

[tabview中的ListFragments]:enter image description here

这就是它的样子。

单击list元素,调用另一个片段显示详细信息,显示如下。 [在列表元素上单击]:enter image description here 有两个问题。

  1. DetailsFragment不是全屏。即能够看到列表(你可以在上图中看到)
  2. 当我回到标签视图并点击列表时,它说: "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)] “ 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>
    

  3. detailsFragment 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>
    

    提前致谢

4 个答案:

答案 0 :(得分:5)

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()。问题是绑定到适配器的集合在后台线程中已更改。因此,解决方案是将其移动到UI线程(如上所述)或简单地用runOnUiThread()包装它。

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

要调用另一个片段:

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

修改

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

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

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

答案 1 :(得分:0)

对于上述内容您只需添加一个片段,只需要一个实例:

例如 -

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Search fragment = new  Search();
fragmentTransaction.add(R.id.mainLayout,fragment , "MY_FRAG");
fragmentTransaction.commit();

答案 2 :(得分:0)

试试这个例子

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    Search fragment = new  Search();
    fragmentTransaction.add(R.id.mainLayout,fragment , "MY_FRAG");
    fragmentTransaction.commit();

它确实帮助您使用来自另一个片段的片段。

答案 3 :(得分:0)

使用hadler通知您的列表,因为您无法从后台或工作线程访问任何UIComponents,因此必须从UI Thread访问它。确保使用以下技巧。

Handler mHandler = new Handler();
mHandler.post(new Runnable() {

@Override
public void run() {
    listadapter.notifyDataSetChanged();
    }
});