简体   繁体   中英

Duplication of listview rows in list fragment on orientation change

I have a ListFragment where I use a custom adapter to populate the listview. All is well until I change orientation and scroll. Then it looks like this: 一些方向更改后的ListView

I am guessing it has something to do with me fumbling with a view holder, but I can't access the code right now.

The reason for the overlapping fragment was that I used FrameLayout and added the fragment with FragmentTransition.add(...). When I changed .add() to .replace() the old fragment was removed and the new one was added and my problem was solved.

我有类似的问题,根据我说的是解决方案: - 你得到这个模糊因为listfragment创建的方向改变的地方新的实例每次(可能它是在OnCreate中()),所以你必须只是让一次列表片段的实例,并且在方向更改时替换该片段而不是再次添加。

Changing orientation causes onCreate to restart unless you include this method

public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
}

and put this in the activity section of your manifest

android:configChanges="orientation"

Check that this is the first time onCreate() is called, in other words, determine if the callback is not due to screen rotation.

protected void onCreate(Bundle savedInstanceState) {
        if(savedInstanceState == null) {
    // transition.add(Your fragment,...)
    }
}

I had the same problem, and its because of FrameLayout usage, First you have to check if your fragment has already added to the activity :

String TagName = "F";

Adding a fragment without a UI If you want to get the fragment from the activity later, you need to use findFragmentByTag(). "Google" http://developer.android.com/guide/components/fragments.html#Adding

Fragement F = getFragmentManager().findFragmentByTag(TagName);

Then check

if(F == null) {
  F = new F();
  getFragmentManager()
     .beginTransaction()
     .add(R.id.container, F, TagName)
     .commit();
}

the target here is to avoid adding or creating new instance of the fragment, that persist during the configuration change which causes the problem when using the FrameLayout as container.

Solution 2 (Simple): The only thing you have to do here is to change your container to ex:LinearLayout , and that's it. but in my opinion this is not the best solution because of multiple instances of the fragment.

Hope this help;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM