![](/img/trans.png)
[英]How to make List of button using arrayAdapter and Listfragment in android?
[英]Android: Use different layouts in ListFragment using ArrayAdapter
大家好,謝謝您閱讀我的問題!
我目前正在開發具有以下層次結構的android應用程序:一個FragmentActivity
除以三個ListFragments
。 其中一個的ListFragments
使用自定義ArrayAdapter
顯示List<>
在一個XML模板ListView
此List<>
填充有被賦予ID的數據集。 ID小於10的數據集被標記為“重要”,其他數據集被標記為“不太重要”
這就是我要尋找的:我有兩個布局模板,並且我想用一種布局顯示“重要”數據集,而用另一種布局顯示“不太重要”的數據集。
我已經搜索了很長時間,但似乎以前沒有人問過這樣的問題。 我嘗試使用第二個ListView
並在其上應用第二個自定義ArrayAdapter
,但是很遺憾,存在一個限制:由於我使用的是ListFragment
,因此我不得不像這樣使用findViewById
。
ListView list;
list = (ListView) rootView.findViewById(android.R.id.list);
當我嘗試編寫第二個自定義ArrayAdapter
,它在此處顯示錯誤:
ListView list;
list_flat = (ListView) rootView.findViewById(android.R.id.list_flat);
這是整個ListFragment
public class LeaguePage extends ListFragment{
Global global_var;
ListView list, list_flat;
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, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
list_flat = (ListView) rootView.findViewById(android.R.id.list_flat);
try {
leagues = Leagues_Parser.parse(getActivity().getAssets().open("league_raw.xml"));
} 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.selectedTeam = Integer.valueOf(leagues.get(arg2).getInternalID());
Global.mViewPager.setCurrentItem(1, true);
}
});
}
}
這是我傳遞給 ArrayAdapter
的xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/liga_flat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:paddingBottom ="10dip"
android:textColor="#040404"
android:textSize="25sp"
android:textStyle="normal"
android:typeface="serif" />
</RelativeLayout>
這是 ListFragment
的xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main_Activity$search_page" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main_Activity$search_page" >
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="0dp"
android:divider="@null">
</ListView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main_Activity$search_page" >
<ListView
android:id="@+id/android:list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@null"
android:dividerHeight="0dp" >
</ListView>
</RelativeLayout>
</RelativeLayout>
您不應該給ListView
的ID: android:id="@+id/android:list2"
,您的ListView
的ID應該是
<ListView android:id="@id/android:list"
...... ></ListView>
要么
<ListView android:id="@+id/sampleList"
...... ></ListView>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.