Android:使用Fragmenttransaction通过片段显示两个ListView

时间:2014-02-27 16:48:54

标签: android android-listview android-fragments fragmentpageradapter fragmenttransaction

对于我的应用程序,我正在尝试显示列表,一旦此列表结束,第二个将开始。使用ListAdapter显示列表,ListAdapter也是片段的一部分。一切都运作良好,列表正确显示,但我无法找到一种方法将一个列表放在另一个列表之下。我认为这不应该太难。 总结:

我有什么: 一个FragmentPagerAdapter有3个碎片 两个片段,每个片段包含一个ListView

我的搜索 除了在这个网站上进行多次搜索外,这个家伙最接近我所寻求的: 这个人Fragmenttransaction in 1 tab of a Fragmentpageradapter遇到了同样的问题,但是答案并不令人满意,所以我想我可以在这里提出一个有效的问题。

我的问题: 如何在一个片段中放置两个ListViews?最重要的是,例如,如果第一个ListView比屏幕大,我不希望第二个ListView在第一个完全向下滚动之前显示。

当前输出: 目前,两个ListViews处于相同的位置,这意味着一个ListView位于另一个之上,使得两者都不可读

我认为我可以使用FragmentTransaction的指定布局。但我无法弄清楚如何。

这是片段,我将我的上下组合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);

}

}

这与xml布局文件相对应。

<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" />

这是我的两个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);
        }
       });
}

}

这是相应的xml文件

    <?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>

非常感谢你阅读和思考它!

2 个答案:

答案 0 :(得分:0)

原因是因为你已经在你显示的match_parent的XML文件中设置了LinearLayout,它将占用所有可用空间,然后你的下一个ListView带有它自己的LinearLayout(我认为它也设置为match_parent),因此它没有空间可供展示。 FrameLayout和LinearLayout遵循最早的孩子第一种方法,这意味着第一种布局占用了它所请求的空间。设置你需要的LinearLayout wrap_content,我认为这应该解决你的片段。

答案 1 :(得分:0)

首先使用视图filpper.you可以在片段中加载您的第一个列表视图,在项目点击监听器的适配器视图中,您可以通过flipper.setdisplayedchild(1)翻转视图,它将显示您想要的第二个列表。