0

对于我的应用程序,我试图显示一个列表,一旦这个列表结束,第二个应该开始。列表正在使用ListAdapter显示,该列表又是片段的一部分。一切正常,列表显示正确,但我无法想出一种方法将一个列表放在另一个列表下。我认为这不应该太难。 摘要:Android:使用FragmentTransaction通过片段显示两个ListViews

我有什么: 一个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> 

非常感谢您阅读和思考!

回答

0

原因是因为您已将LinearLayout设置为显示为match_parent的XML文件,它将占用所有可用空间,然后将自己的下一个ListView与它自己的LinearLayout(我认为它也设置为match_parent)以及因此没有空间显示。 FrameLayout和LinearLayout遵循老大的第一种方法,这意味着第一个布局占用了它所需的尽可能多的空间。设置LinearLayout你必须wrap_content,我认为这应该解决你的片段。

+0

我刚试过你的解决方案,不幸的是它没有工作。我认为这是因为首先'ListView'已经占用了所有可用的屏幕空间,而且我无法用'FragmentTransaction'指定更多的布局设置:\ 不过谢谢您的回复! – Guntram

0

首先使用视图filpper.you可以加载你的第一个列表视图在片段和你的adapterview上的项目点击监听器,你可以翻转flipper.setdisplayedchild(1)显示你想要的第二个列表。