Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.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 setRetainInstance(true),但未显示UI_Android_Gridview_Android Fragments - Fatal编程技术网

Android setRetainInstance(true),但未显示UI

Android setRetainInstance(true),但未显示UI,android,gridview,android-fragments,Android,Gridview,Android Fragments,我正在尝试创建一个图像库,作为一个指南,并做了一些调整 这是我的片段代码的一部分: public static class PlaceholderFragment extends Fragment { private static final String TAG = "com.example.imageencryptor.RetainedFragment"; LruCache<String, Bitmap> cache; public Placehol

我正在尝试创建一个图像库,作为一个指南,并做了一些调整

这是我的片段代码的一部分:

public static class PlaceholderFragment extends Fragment {

    private static final String TAG = "com.example.imageencryptor.RetainedFragment";

    LruCache<String, Bitmap> cache;

    public PlaceholderFragment() {

        initCache();

    }

    public void initCache() {
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        final int cacheSize = maxMemory / 8;
        cache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // The cache size will be measured in kilobytes rather than
                // number of items.
                return ImageLoader.getSizeInBytes(bitmap) / 1024;
            }
        };

    }

    public static PlaceholderFragment findOrCreateRetainFragment(
            FragmentManager fm) {
        PlaceholderFragment fragment = (PlaceholderFragment) fm
                .findFragmentByTag(TAG);
        if (fragment == null) {
            fragment = new PlaceholderFragment();
            fm.beginTransaction().add(fragment, TAG).commit();
        }
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

        GridView imageGrid = (GridView) rootView
                .findViewById(R.id.gridview);

        ImageLoader loader = new ImageLoader(getActivity(), cache);
        imageGrid.setAdapter(new ImageAdapter(getActivity(), loader));

        return rootView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

}
然而,在开始时,我最终得到了一个空白的UI。没有网格视图。为什么呢

另一方面,如果我的onCreate更改为:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    PlaceholderFragment pFragment = PlaceholderFragment
            .findOrCreateRetainFragment(getSupportFragmentManager());


     if (savedInstanceState == null) {
     getSupportFragmentManager().beginTransaction()
     .add(R.id.container, pFragment,PlaceholderFragment.TAG).commit();
     }

}

但是,当屏幕方向发生变化时,是否会反复向FragmentManager添加多个具有相同标记的相同片段?

我猜UI不会显示,因为您在名为
findOrCreateRetainFragment
的界面内执行
FragmentTransaction
。此接口用于将数据从
活动
传递到动态添加的
片段
,即使该片段以某种方式由Android重新创建,此接口也可用。您需要在活动中执行碎片事务,如下所示:

// Init your fragment
PlaceholderFragment pFragment = (PlaceholderFragment) 
    getSupportFragmentManager().findFragmentByTag(PlaceholderFragment.TAG);

// If it is null: the fragment is not created yet - create it
if(pFragment == null) {
    // send data to interface - which will create a new one
    PlaceholderFragment pFragment = 
            new PlaceholderFragment().instanceRetainFragment(value);
    // add the new fragment
    getSupportFragmentManager().beginTransaction()
            .add(R.id.container, pFragment, PlaceholderFragment.TAG).commit();
}
// else: avoid to create the fragment again
然后,片段中的接口可能是:

public static PlaceholderFragment instanceRetainFragment(int value) {
    // create a new fragment
    PlaceholderFragment mFragment = new PlaceholderFragment();
    // create a bundle to store data
    Bundle args = new Bundle();
    // insert data
    args.putInt("mValue", value);
    // attach data
    mFragment.setArguments(args);
    // return new fragment in the activity
    return mFragment;
}
您还可以创建一个不存储数据的接口,只调用第一行和最后一行。但是,存储和接收活动中的某些信息可能是有用的(例如:使用
布尔值
过滤
网格视图
,使用
整数
仅显示一个类别等)。

因此,主要问题是在Fragment中调用
FragmentManager
,而不是活动。这可能是“不显示UI”的原因。让我知道这是否有效。

嗨,你测试过我下面的答案了吗?你找到解决办法了吗?还是需要更多的解释?
public static PlaceholderFragment instanceRetainFragment(int value) {
    // create a new fragment
    PlaceholderFragment mFragment = new PlaceholderFragment();
    // create a bundle to store data
    Bundle args = new Bundle();
    // insert data
    args.putInt("mValue", value);
    // attach data
    mFragment.setArguments(args);
    // return new fragment in the activity
    return mFragment;
}