在两个listfragment之间导航

时间:2013-12-28 15:33:43

标签: android android-listfragment

我知道有几个这样的类似已经存在,但是他们都没有帮助我,我也试过在网上搜索但没有解决方案。

无论如何,我有一个从FragmentActivity启动的listfragment(fragA)。 FragA包含一个按钮,单击该按钮时,它应该打开另一个ListFragment(fragB)。

除了我在fragB中得到这个例外外,一切看起来都很好:

  适配器视图

不支持

unsupportedoperationexception addview

请问我该如何解决?

感谢。

代码:

FRAG A:

public class FragA extends ListFragment {

public static ListView _listView;
private CAdapter _adapter;
private ObjCollection _data;
private Context _context;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = LayoutInflater.from(getActivity()).inflate(
            R.layout.layout, null);

    _listView = (ListView) view.findViewById(android.R.id.list);

    _data = new ObjCollection();

    Button btn = (Button) view.findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NavigateFragment(new FragB(), true, FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        }
    });

    return view;
}

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

    _adapter = new CAdapter(getActivity(), _data);
    _listView.setAdapter(_contactAdapter);

}

@Override
public void onStart() {
    super.onStart();
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}

private void NavigateFragment(ListFragment listFragment, Boolean addtostack, int transition) {

    FragmentTransaction ft = getFragmentManager().beginTransaction();

    ft.replace(android.R.id.list, listFragment);
    ft.setTransition(transition);
    if (addtostack)
        ft.addToBackStack(null);

    ft.commit();

}
  }

XML:

<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" >
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/button" >
    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:id="@id/android:empty"
        style="@style/DefaultText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp"
        android:gravity="center_horizontal"
        android:text="empty"
        android:textSize="25sp" />
</FrameLayout>
 <Button
    android:id="@+id/button"
    style="@style/buttonText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="button" />

FRAG B:

public class FragB extends ListFragment {

public static ListView _listView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = LayoutInflater.from(getActivity()).inflate(
            R.layout.layoutB, null);

    _listView = (ListView) view.findViewById(android.R.id.list);


    return view;
}

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

}

@Override
public void onStart() {
    super.onStart();
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
}

XML:

<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" >
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:id="@id/android:empty"
        style="@style/DefaultText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="60dp"
        android:gravity="center_horizontal"
        android:textSize="25sp" />
</FrameLayout>
</RelativeLayout>

logcat的:

12-28 11:40:53.368: E/AndroidRuntime(15426): FATAL EXCEPTION: main
12-28 11:40:53.368: E/AndroidRuntime(15426): java.lang.UnsupportedOperationException:           addView(View) is not supported in AdapterView
12-28 11:40:53.368: E/AndroidRuntime(15426):    at  android.widget.AdapterView.addView(AdapterView.java:454)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at   android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:922)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at android.os.Handler.handleCallback(Handler.java:730)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at   android.os.Handler.dispatchMessage(Handler.java:92)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at android.os.Looper.loop(Looper.java:137)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at android.app.ActivityThread.main(ActivityThread.java:5450)  
12-28 11:40:53.368: E/AndroidRuntime(15426):    at java.lang.reflect.Method.invokeNative(Native Method)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at java.lang.reflect.Method.invoke(Method.java:525)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
12-28 11:40:53.368: E/AndroidRuntime(15426):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

  

请问我该如何解决?

您在事务中使用了错误的ID来显示第二个片段。现在使用NavigateFragment()方法:

//...
ft.replace(android.R.id.list, listFragment);
ft.setTransition(transition);
//...

因此您使用第一个ListViewListFragment的ID作为事务的容器,因此ListView将成为第二个ListFragment的容器。这是错误的,当FragmentManager尝试将第二个片段的视图添加到ListView时,它会失败(因为ListView不允许直接使用{{1}方法)。

解决方案是在addView()方法中使用容器的ID,其中添加了第一个NavigateFragment() 而不是ListFragment