Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android開發札記(13)——ListFragment完整用法解析

編輯:Android 開發教學

Android開發筆記(13)——ListFragment

轉載請注明:https://www.cnblogs.com/igoslly/p/6959108.html

ListFragment是什麼

ListFragment是繼承自Fragment的類,專門用於承載包含ListView的布局。

當然,如果你不想用ListFragment,通過普通Fragment手動setAdapter同樣可以實現,普通ListView的用法可參考前章:https://www.cnblogs.com/igoslly/p/6947225.html

 

配置ListFragment通常涉及3個Layout文件:

1、承載Fragment的主Activity布局:activity_main.xml(既可靜態添加fragment,也可用FrameLayout動態添加)

2、ListFragment自身的布局:history_list.xml

ListFragment的布局默認包含一個ListView,id必須命名為「@id/android:list」(與普通命名語法不同)

還可另設一個TextView用於無數據時顯示,id命名為「@id/android:empty」

3、列表中每個item的布局:history_list_competition.xml

 


以下是我實際項目中寫的實例,採用動態添加fragment並自定義BaseAdapter的方式。

—— ArrayAdapter 與 SimpleAdapter 的配置更簡單,可參考前章

—— 靜態添加fragment的方法,本質上是 findViewById 與 findViewByTag 的區別,也可詳見蘇白的專欄:https://blog.csdn.net/kakaxi1o1/article/details/29368645

 

activity_main.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/history_list" android:orientation="vertical"> </LinearLayout> </LinearLayout>

 

historyFragment.java

在 onCreateView() 中,指定 history_list.xml 作為該ListFragment的布局文件。

fragmentTranscation.replace(R.id.history_list, historyListFragment).commit();

 動態添加historyListFragment,並替換原有fragment

public class HistoryFragment extends Fragment { private FragmentManager fragmentManager; public HistoryFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.history_list, container, false); return rootView; } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Button competition_selected = (Button) getActivity().findViewById(R.id.history_competition); competition_selected.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fragmentManager = getFragmentManager(); fragmentTranscation = fragmentManager.beginTransaction(); HistoryListFragment historyListFragment = new HistoryListFragment(); fragmentTranscation.replace(R.id.history_list, historyListFragment).commit(); }} ); } }

 

history_list.xml

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/list_content"> <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="8dp"/> <TextView android:id="@id/android:empty" android:layout_width="match_parent" android:layout_height="match_parent" android:text=""/> </LinearLayout>

 

history_list_competition.xml

定義ListView每個item的布局格式

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_weight="8" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="Null" android:textSize="20sp" android:padding="2dp" android:textColor="@color/black" android:id="@+id/list_competition_player"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:text="Null" android:textSize="16sp" android:padding="2dp" android:id="@+id/list_competition_date"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_weight="3" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="100" android:gravity="center" android:textSize="24sp" android:textColor="@color/black" android:id="@+id/list_competition_scoreA"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="—" android:gravity="center" android:textSize="28sp"/> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_weight="3" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="100" android:gravity="center" android:textSize="24sp" android:textColor="@color/black" android:id="@+id/list_competition_scoreB"/> </LinearLayout> </LinearLayout>

 

 

HistoryListFragment.java

在 onCreate() 中,透過 setListAdapter() 綁定 R.layout.history_list_competition,也可以直接使用系統默認的 R.layout.simple_list_item_1;同時設置列表項的點擊事件並自定義BaseAdapter。

注意!若要運行以下Java代碼,請自行補上具體的 List<Map<String,Object>> 數據,否則會報錯。

public class HistoryListFragment extends ListFragment { private CompetitionListAdapter adapter; private List<Map<String,Object>> competitionlist; // 構造函數 public HistoryListFragment(){} @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); competitionlist = new ArrayList<Map<String,Object>>(); adapter = new CompetitionListAdapter(getActivity()); //綁定適配器時,必須透過ListFragment.setListAdapter()接口,而不是ListView.setAdapter()或其它方法 this.setListAdapter(adapter); } // 創建視圖 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.history_list, container, false); } // 設置點擊事件 @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); HashMap<String, Object> item =(HashMap<String, Object>) adapter.getItem(position); String scoreA = (String)item.get("scoreA"); String scoreB= (String)item.get("scoreB"); String log = (String)item.get("log"); } // 自定義 CompetitionListAdapter 繼承於BaseAdapter public class CompetitionListAdapter extends BaseAdapter { private LayoutInflater mInflater=null; public CompetitionListAdapter(Context context){ this.mInflater=LayoutInflater.from(context); } @Override public int getCount(){ return competitionlist.size(); } @Override public Object getItem(int position){ return competitionlist.get(position); } @Override public long getItemId(int position){ return position; } @Override public View getView(int position, View convertView, ViewGroup parent){ ViewHolder holder = null; if (convertView ==null){ holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.history_list_competition,null); holder.date=(TextView)convertView.findViewById(R.id.list_competition_date); holder.scoreA=(TextView)convertView.findViewById(R.id.list_competition_scoreA); holder.scoreB=(TextView) convertView.findViewById(R.id.list_competition_scoreB); holder.player=(TextView)convertView.findViewById(R.id.list_competition_player); convertView.setTag(holder); }else { holder = (ViewHolder)convertView.getTag(); } holder.date.setText((String)competitionlist.get(position).get("date")); holder.scoreA.setText((String)competitionlist.get(position).get("scoreA")); holder.scoreB.setText((String)competitionlist.get(position).get("scoreB")); holder.player.setText((String)competitionlist.get(position).get("player")); return convertView; } private class ViewHolder{ public TextView date; public TextView player; public TextView scoreB; public TextView scoreA; } } }

 

總體效果圖如下:

 

 

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved