1

对于我的应用程序,我试图显示一个列表,一旦这个列表结束,第二个就会开始。列表正在使用 ListAdapter 显示,它又是片段的一部分。一切运行良好,列表显示正确,但我想不出一种方法将一个列表放在另一个列表之下。我认为这不应该太难。概括:

我所拥有的: 一个FragmentPagerAdapter带有 3 个片段的两个片段,每个片段包含一个 ListView

我的搜索: 除了在这个网站上进行多次搜索外,这个人最接近我正在寻找的东西:这个人在Fragmentpageradapter 的 1 个选项卡中的 Fragmenttransaction遇到了同样的问题,但没有得到令人满意的回答,所以我想我可以在这里提出一个有效的问题。

我的问题: 我怎样才能将两个ListViews放在一个片段中?重要的是,例如,如果第一个 ListView 比屏幕大,我不希望在第一个 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>

非常感谢您阅读和思考!

4

2 回答 2

0

原因是您已经在显示的 XML 文件中将 LinearLayout 设置为 match_parent ,这将占用所有可用空间,然后您的下一个 ListView 带有它自己的 LinearLayout (我认为它也设置为 match_parent ),因此没有空间让它显示。FrameLayout 和 LinearLayout 遵循长子优先方法,这意味着第一个布局占用的空间与它请求的一样多。将您必须的 LinearLayout 设置为 wrap_content ,我认为这应该可以解决您的片段。

于 2014-02-27T17:14:21.387 回答
0

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

于 2014-10-27T16:44:08.763 回答