android – onCreateView未在ViewPager中使用Fragment调用

我正在使用ACL中的ViewPager来显示几个片段.首先,这些片段所有ListFragments都包含一个列表.我正在尝试模仿2列表,因此列表中的每个元素都包含另外两个对象的列表.我希望每个单元格都可以长按(不是整个列表项),因此我实现了自己的ColumnLayout(也许它应该被称为CellLayout).每个列表项包含两个可长按的ColumnLayouts,并实现getContextMenuInfo()以返回有关长按哪个实体的信息.此方法查找ViewPager,请求当前片段,然后调用片段以从长时间单击的视图中返回实体ID.为了获得片段需要执行的正确行:

getListView().getPositionForView(v)

这就是问题开始的地方.有时getListView()会抛出IllegalStateException,说“尚未创建内容视图”.即onCreateView(…)尚未调用.这有时仅在应用程序恢复时发生.今天,我设法通过在“设置”中启用“不要保留活动”选项来重新产生问题.我的galaxy Nexus上的开发人员选项.我启动应用程序,按Home,然后从最近的应用程序菜单重新打开应用程序,现在,如果我长按listview中的任何单元格,则抛出此异常.通过一些调试输出,我设法验证从未调用onCreateView.这有点奇怪,因为整个ViewPager及其Fragment及其ListView及其单元格项目都被绘制出来了!

我在#android-dev上被告知ACL中的ListFragment不支持自定义布局,所以我重新实现了我的android.support.v4.app.Fragment而没有使用ListFragment,但这没有帮助.

简短摘要我有一个自定义布局,处理长按布局的事件,目的是创建一个MenuInfo对象,其中包含有关按下的单元格中的权限的信息.为了找到布局中包含的实体,最终会调用listview.getPositionForView(View v),有时会导致IllegalStateException.

在这里我的自定义布局(也许有一种更简单的方法获取ViewPager而不是在视图层次结构中挖掘):

package org.remotestick;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.widget.LinearLayout;

public class ColumnLayout extends LinearLayout {

    public ColumnLayout(Context context,AttributeSet attrs) {
        super(context,attrs);
    }

    public ColumnLayout(Context context) {
        super(context);
    }

    @Override
    protected ContextMenuInfo getContextMenuInfo() {
        int itemTypeId = getChildAt(0).getId();
        int positionTypeId = getId();
        View currentView = this;
        ViewPager viewPager = null;
        while(currentView.getParent() != null) {
            currentView = (View) currentView.getParent();
            if(currentView.getId() == R.id.pager) {
                viewPager = (ViewPager) currentView;
                break;
            } else if (currentView.getId() == R.id.rootLayout)
                break;
        }
        if(viewPager == null)
            return null;

        WorkspaceAdapter adapter = (WorkspaceAdapter) viewPager.getAdapter();
        Pairirst,itemIdAndListPosition.second);
    }

    public static class MatrixAdaptableContextMenuInfo implements ContextMenuInfo {

        public final int itemTypeId;
        public final Integer itemId;
        public final Integer positionInList;

        public MatrixAdaptableContextMenuInfo(int itemTypeId,Integer itemId,Integer positionInList) {
            this.itemTypeId = itemTypeId;
            this.itemId = itemId;
            this.positionInList = positionInList;
        }

    }
}

而这里是片段的片段:

public class EntityTableFragment extends Fragment implements TelldusEntitychangelistener {

    protected static final String ARG_TITLE = "title";

    protected MatrixAdapter mAdapter;
    protected ProgressViewer mProgressViewer;
    protected MatrixFilter mFilter;
    protected Handler mHandler = new Handler();
    protected RemoteStickData db;

    public boolean onAttach = false;
    public boolean onCreateView = false;

    private ListView listView;

    public static EntityTableFragment newInstance(String title) {
        EntityTableFragment f = new EntityTableFragment();

        Bundle args = new Bundle();
        args.putString(ARG_TITLE,title);
        f.setArguments(args);
        return f;
    }

    @Override
    public void onAttach(SupportActivity activity) {
        super.onAttach(activity);
        onAttach = true;
            try {
            mProgressViewer = (ProgressViewer) activity;
            mAdapter = new MatrixAdapter(getActivity(),mProgressViewer,new ArrayListgalStateException("Listview not yet created");
        int positionForView = listView.getPositionForView(v);
        if(isLeft)
            return new Pair

恢复应用程序(如果活动被销毁)并不奇怪,ViewPager及其Fragments及其ListView及其自定义布局都被绘制出来.但是我得到了IllegalStateException,如果我长按listview中的任何一个单元格,那么该视图还没有被创建?

编辑/
实际上,一个Log.d(Constants.TAG,“onCreateView !!!!”); onCreateView中的输出显示,我第一次启动应用程序时会调用两次方法(ViewPager中每个Fragment一次),但是当应用程序恢复时,它永远不再被调用!这是一个错误还是我可能做错了什么?

最佳答案
关键部分似乎是FragmentPagerAdapter(我没有包含).调试显示重新创建活动时不调用getItem().碎片是通过其他方式重新创建的.这意味着我的FragmentPagerAdapter实现对FragmentManager中的片段有错误的引用.

所以相反,当我需要查找一个片段时,我使用findFragmentByTag()(在FragmentManager上)使用makeFragmentName()来获取相应的标记名称.这是添加片段时FragmentPagerAdapter使用的方法.但不确定这是否是一种安全的方法.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...