Android開發筆記(13)——ListFragment

来源:http://www.cnblogs.com/igoslly/archive/2017/06/07/6959108.html
-Advertisement-
Play Games

轉載請註明:http://www.cnblogs.com/igoslly/p/6959108.html ListFragment ListFragment是繼承於Fragment的類,專門用於包含ListView的佈局文件設置。 當然如果你不想瞭解ListFragment,通過使用普通Fragmen ...


轉載請註明:http://www.cnblogs.com/igoslly/p/6959108.html

ListFragment

ListFragment是繼承於Fragment的類,專門用於包含ListView的佈局文件設置。

當然如果你不想瞭解ListFragment,通過使用普通Fragment進行setAdapter設置亦是可以的,普通ListView設置參見前章:http://www.cnblogs.com/igoslly/p/6947225.html

 

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

1、包含Fragment的主Activity Layout:activity_main.xml  (可直接靜態添加fragment,或設置framelayout動態添加)

2、應用ListFragment的 Layout:history_list.xml

ListFragment的佈局預設包含一個listVew,命名為:“@id/android:id” (和普通命名語法不同)

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

3、佈局中ListView每個item的設置Layout:history_list_competition.xml

 


以下我實際應用所寫的實例,使用的是動態添加fragment,自定義BaseAdapter的方法。

—— ArrayAdapter & SimpleAdapter的設置更為簡單,可參考前章

—— 靜態添加fragment的方法,即是一個函數findViewById findViewByTag的區別,也可詳見蘇白的專欄:http://blog.csdn.net/kakaxi1o1/article/details/29368645

 

activity_main.xml

<LinearLayout xmlns:android="http://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="http://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="http://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

添加ListView的點擊事件自定義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;
        }
    }
}

 

總體效果圖如下:

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 今天複習了一下麵向對象編程,下麵是我的效果圖 看著這個圖片你會覺得這沒有什麼,那麼上代碼: html部分: 必要的css代碼 js部分: 輪播的js 希望能幫到大家,還有就是這個不是基於jQuery的,我自己封裝了一個簡單的方法。 以後我們圖片輪播可以使用 這樣使用於比較多的輪播組件的網站,當然我們 ...
  • 參考文檔:https://developer.mozilla.org/en-US/docs/AJAX 本文進行了大致翻譯。 Ajax 本身本不是一門技術,而是在2005年由Jesse James Garrett首創的描述為一個“新”途徑來應用許多已存在的技術,包括:HTML 或者 XHTML, Ca ...
  • 學習 AngularJS 要先瞭解 MVC 模式 , 即 " 模型--視圖--控制器 " . 模型: 包含了需要用到的數據 ; 有兩種廣義上的模型 : 視圖模型 , 只表示從控制器傳往視圖的數據 ; 領域模型 , 包含了業務領域的數據 , 以及用於創建 , 存儲和操縱這些數據的各種操作 , 轉換和規... ...
  • 在項目中有時候會用到圖片的延遲載入,那麼延遲載入的好處是啥呢? 我覺得主要包括兩點吧,第一是在包含很多大圖片長頁面中延遲載入圖片可以加快頁面載入速度;第二是幫助降低伺服器負擔。 下麵介紹一下常用的延遲載入插件jquery.lazyload.js以及怎樣實現一個延遲載入的插件。 一:jquery.la ...
  • <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style type="text/css">.list { margin: 50px auto; padding: 0; list-s ...
  • 代碼: ...
  • iOS的單例模式有兩種官方寫法,如下: (1)不使用GCD dispatch_once這個函數,它可以保證整個應用程式生命周期中某段代碼只被執行一次! ...
  • Original. 今天有同事問說, 充電電壓不是 4.35V 嗎? 充電到 100 %時,為什麼 Vbat 只有 4.2V? 可能有三種原因。 1. 溫度。 safety 會在某個溫度區間,使用較低的電壓去充電。 2. fuel gauge。 Q_max 設太小了, 沒一會兒就算到100%, 雖然 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...