1

For my app, I'm trying to show a list, and as soon as this list ends, the second one shall begin. The Lists are being displayed using a ListAdapter, which again is part of a fragment. Everything works very well, the lists appear correctly, but I can't figure out a way to put one list under the other. I thought this shouldn't be all too hard. Summary:

What I have: A FragmentPagerAdapter with 3 Fragments Two Fragments, which contain one ListView each

My searches: Apart from multiple searches on this site, this guy came closest to what I'm seeking: This guy here Fragmenttransaction in 1 tab of a Fragmentpageradapterhas had the same problem, but it wasn't satisfyingly answered, so I thought I can make a valid question here.

My question: How can I place two ListViews in one Fragment? The big deal is that for example if the first ListView is bigger than the screen, I don't want the second ListView to show up before the first is completely scrolled down.

Current output: Currently, both ListViews are in the same position, meaning that one ListView is on top of the other, making both unreadable

I thought that I can maybe use a specified layout for the FragmentTransaction. But I just can't figure out how.

This is the Fragment where I combine my top and bottom ListViews

public class LeaguePageTransactionsAdapter extends Fragment{
Global global_var;
ListView list, list_flat;
List <League> leagues = null, leaguesFlat = null;
ListAdapter adapter = null, adapter_flat = null;
View rootView;
FragmentTransaction fragmentTransaction;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.league_page, container, false);
    fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.add(rootView.getId(), new LeaguePageTop(), "TopFragment");  
    fragmentTransaction.add(rootView.getId(), new LeaguePageBottom(), "BottomFragment");
    fragmentTransaction.commit();
    return rootView;
   }

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);

}

}

This is corresponding the xml layout file.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

This is one of my two ListViews

public class LeaguePageTop extends Fragment{
ListView list;
List <League> leagues = null;
ListAdapter adapter = null;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.league_page_top, container, false);
    return rootView;
   }

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);
    list = (ListView) rootView.findViewById(R.id.listView1);

    try {
        leagues = Leagues_Parser.parse(getActivity().getAssets().open("league_raw.xml"), 0);

    } catch (IOException e) {
        e.printStackTrace();
    }
    adapter = new LeagueAdapter (getActivity(), R.layout.list_row, leagues);

    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener()
       {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            Global.mViewPager.setCurrentItem(1, true);
        }
       });
}

}

This is the corresponding xml file

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

Thank you very much for reading and thinking about it!

Community
  • 1
  • 1
Guntram
  • 414
  • 2
  • 5
  • 18

2 Answers2

0

The reason is because you have set the LinearLayout in the XML file you have shown to match_parent which will occupy all available space and then comes your next ListView with it's own LinearLayout (which I presume is set to match_parent as well) and hence there is no space for it to display. FrameLayout and LinearLayout follows an eldest child first approach meaning that the first layout occupies as much space as it requests. Set the LinearLayout you have to wrap_content and I think that should solve your fragment.

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • I just tried your solution, unfortunately it didn't work. I think the reason is because first the `ListView` already occupies all of the available screen space, and I can't specify further layout settings with `FragmentTransaction` :\ Thank you for your reply, though! – Guntram Feb 27 '14 at 18:17
0

Firstly use a view filpper.you can load your first list view in fragment and in your adapterview on item click listener you can flip view by flipper.setdisplayedchild(1) which will show your desired second list.

TanvirChowdhury
  • 2,498
  • 23
  • 28