Simpson
Simpson

Reputation: 593

Why Listfragment scroll the list behind the view and I see duplicate the elements?

I'm trying to find same problem but I don't found anything similar. My problem is:

I have a listFragment that get the data from the sqlite. All works fine but when the listview have more elements and I need to do a scroll to see more elements, the view have a rare behaviour. I can see in the view how all the elements are static but in the behind of the view all the elements are doing scroll.

I add a picture to see better the behaviour.

enter image description here

This is my listFragment class:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBase = new DataBaseWrapper(getActivity());

    }


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

        ViewGroup rootView = null;
        final String[] name;
        final String[] url;

        dataBase.getAllData();

        name = new String[dataBase.getAllData().size()];
        url = new String[dataBase.getAllData().size()];

        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Map<String, String> map;


        for (int i = 0; i < dataBase.getAllData().size(); i++) {
            name[i] = dataBase.getAllData().get(i).getName();
            url[i] = dataBase.getAllData().get(i).getUrl();


            map = new HashMap<String, String>();
            map.put("name", name[i]);
            map.put("url", url[i]);
            list.add(map);

        }

        rootView = (ViewGroup) inflater.inflate(R.layout.fragment_listview, container, false);

        SimpleAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.favourites, new String[] { "name", "url" }, new int[] { R.id.newspaperNameFavourite, R.id.passUrl });
        setListAdapter(adapter);
        setRetainInstance(true);


        return rootView;
    }

Fragment_Listview xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/favourites_fragment">


    <ListView android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:layout_marginTop="50dp"/>


</FrameLayout>

Favourites xml:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:padding="5dp">


    <TextView
        android:id="@+id/newspaperNameFavourite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textSize="20sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:id="@+id/passUrl"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="135dp"
        android:layout_marginEnd="135dp"
        android:visibility="gone"/>

</RelativeLayout>

This is where I create the fragmentList:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);


        Fragment fr = new FavouritesActionBar();

        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_place, fr);
        fragmentTransaction.commit();

    }

listview xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <fragment
        android:name="com.exagon.goalnews.favourites.FavouritesActionBar"
        android:id="@+id/the_actual_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    </FrameLayout>

Upvotes: 1

Views: 282

Answers (1)

kris larson
kris larson

Reputation: 30985

I have to change my answer. I don't think the ListView is duplicated. Either version of the fragment creation code should work fine. I think the problem is that your Fragment is duplicated.

I didn't look carefully enough at listview.xml. You already declared a FavouritesActionBar fragment in listview.xml, yet in your code you create another one and are trying to replace the declared one. This is redundant.

I remember seeing some comments about how it's not good to mix XML-declared fragments with coded fragments. I can't find the reference to that, but I can tell you that every time I've tried to do something like what you're doing, I've gotten into trouble. Now I always just declare empty container ViewGroups in XML and do fragment transactions on them in the code.

You have two choices:

  • Use the coded fragment and change your listview.xml to this:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_place"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

or

  • Use the XML-declared fragment and change your onCreate() to this:

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listview);
        }
    

Upvotes: 1

Related Questions