首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

Android Fragment + ListView + DrawerLayout 點擊切換 Fragment 完整實作

     編輯:程式設計疑難解答

架構圖解

Activity (MainActivity)
 ├─ DrawerLayout
 │    ├─ ListView (id: lv)  ← 左側選單
 │    └─ FrameLayout (id: fm)  ← 內容容器,動態替換 Fragment
 └─ FragmentManager + FragmentTransaction 管理切換

關鍵代碼片段

1. 主 Activity 綁定 ListView 點擊

lv.setOnItemClickListener((parent, view, position, id) -> {
    Fragment target = null;
    switch (position) {
        case 0: target = new PieChartFragment(); break;   // 餅圖
        case 1: target = new StaticLineFragment(); break; // 靜態折線
        case 3: target = new WebFragment(); break;        // Web
        // 其他 case...
    }
    if (target != null) {
        getFragmentManager().beginTransaction()
            .replace(R.id.fm, target)
            .commit();
        drawerLayout.closeDrawer(Gravity.START); // 關閉抽屜
    }
});

2. Fragment 內部正確獲取 View

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_web, container, false); // 傳 false,不立即附加
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    // 在這裡 findViewById,不要在 onActivityCreated 裡用 getActivity().findViewById
    Button btn = view.findViewById(R.id.webbut);
    webView = view.findViewById(R.id.webweb);
    // 設定 WebView、點擊事件...
}

常見坑點與修正

錯誤做法後果正確做法
在 Fragment 用 getActivity().setContentView()覆蓋整個 Activity 佈局,導致 DrawerLayout 消失只在 onCreateView inflate 自己的布局
ListView 設定 android:choiceMode="singleChoice" 但未高亮視覺無反饋配合 listView.setItemChecked(position, true) + selector 背景
WebFragment 內部重複 setWebViewClient頁面跳轉異常在 onViewCreated 設定一次即可

完整專案結構建議

  • 每個功能一個 Fragment 類(PieChartFragment、WebFragment 等)。
  • 共用 BaseFragment 封裝通用邏輯(ToolBar、載入狀態)。
  • 使用 FragmentFactory 或簡單 Map 管理位置→Fragment 映射,避免巨大 switch。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved