Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从一个ListFragment转换到另一个ListFragment_Android_Android Listfragment - Fatal编程技术网

Android 从一个ListFragment转换到另一个ListFragment

Android 从一个ListFragment转换到另一个ListFragment,android,android-listfragment,Android,Android Listfragment,我正在尝试构建一个基于基本ListFragment的应用程序,它根据用户输入从一个ListFragment转换到另一个 我使用Android系统为ListFragment充气的默认ListView,因此我不会过度使用onCreateView() 为了设置列表片段周围的边距,我添加了一个GlobalLayoutListener 现在,当我启动应用程序时,包含默认片段的第一个屏幕正确显示,边距设置正确 但是,只要我在主布局中单击一个图像,它调用到第二个片段的转换,该片段具有与第一个片段相同的onAc

我正在尝试构建一个基于基本
ListFragment
的应用程序,它根据用户输入从一个
ListFragment
转换到另一个

我使用Android系统为
ListFragment
充气的默认
ListView
,因此我不会过度使用
onCreateView()

为了设置
列表片段
周围的边距,我添加了一个
GlobalLayoutListener

现在,当我启动应用程序时,包含默认片段的第一个屏幕正确显示,边距设置正确

但是,只要我在主布局中单击一个图像,它调用到第二个片段的转换,该片段具有与第一个片段相同的
onActivityCreated()
方法和
GlobalLayoutListener
,我就会在
OnGlobalLayout()中看到可怕的
内容视图尚未创建
方法,当我尝试访问第二个片段中的
列表视图时

public void onActivityCreated(Bundle savedInstanceState){

     super.onActivityCreated(savedInstanceState);



     setListAdapter(new ArrayAdapter<String>(getActivity(),
                    R.layout.listcommon, R.id.label, MAIN_TITLES));




     ListView lv = getListView();

     lv.setDivider(null);
     lv.setDividerHeight(0);
     lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
     lv.setItemChecked(0, true);
     lv.setSelection(0);



     ViewTreeObserver observer = getListView().getViewTreeObserver();

     observer.addOnGlobalLayoutListener(fragmentLayoutListener);

}

ViewTreeObserver.OnGlobalLayoutListener fragmentLayoutListener = new   ViewTreeObserver.OnGlobalLayoutListener(){

    public void onGlobalLayout() {

    //Crashes here for second fragment   
    ListView listView = getListView();

    FrameLayout.LayoutParams params = (LayoutParams) listView.getLayoutParams();

    params.setMargins(10, 10, 10, 10);

    }

};
有没有想过我应该做些什么来避免遇到这个错误

我是否应该超越
onCreateView()
并为自定义列表视图充气(但我没有这种需要)

编辑:

下面是我如何将默认片段添加到主活动的
onCreate()

要执行转换,我要做的是:

    fragment = (TitlesFragment) fragmentManager.findFragmentByTag(SECOND_FRAGMENT_TAG);
                if (fragment == null){

                    fragment = TitlesFragment.newInstance(SECOND_FRAGMENT);
                    fragmentTransaction.replace(R.id.TitlesContainer, fragment, SECOND_FRAGMENT_TAG);
                } else {
                    fragmentTransaction.replace(R.id.TitlesContainer, fragment);
                }
    fragmentTransaction.commit();

由于您没有提到它,也没有显示完整的代码,我在这里有点猜测,但以下可能是您的问题的原因

我认为您需要先创建视图,然后才能访问其中的元素。见和

我的一个项目中的一个示例:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.leftlistfragment, container, false);
    return v;
}
编辑

可能问题在于您创建片段的方式(我不熟悉您使用的标记方法)。也许这也适用于你:

        titleFrag = new TitlesFragment();
        getFragmentManager().beginTransaction()
                .replace(R.id.TitlesContainer, titleFrag, SECOND_FRAGMENT_TAG).commit();

我摆脱了OnGlobalLayoutListener。相反,我在第一个FrameLayout上设置了“layout\u padding”属性,以设置它所包含的片段上的边距。

能否显示将片段添加到布局中的代码?我想问题可能就在这里。我添加了显示添加/替换片段的代码。你能解释一下为什么默认的膨胀视图(我认为是针对ListFragment的android.R.layout.list_内容)在这里不够用吗?另外,我有点困惑,getListView()如何在onActivityCreated()中不会使应用程序崩溃?默认视图应该可以,如果我的回答让您感到困惑,很抱歉,因为它使用了自定义布局。请参阅下面我的修订答案,以进一步解释您的最后一个问题。很抱歉,我没有看到对下面答案的编辑。:)试图为你寻找更好的答案的时间用完了。如果到时候你还没有整理好,我明天再谈。在onCreateView中放大列表布局,我想你会没事的。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.leftlistfragment, container, false);
    return v;
}
        titleFrag = new TitlesFragment();
        getFragmentManager().beginTransaction()
                .replace(R.id.TitlesContainer, titleFrag, SECOND_FRAGMENT_TAG).commit();