1.主界面佈局文件java
?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context=".TabBarActivity">
//用於界面佈局改變
<FrameLayout android:id="@+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</FrameLayout>
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:id="@+id/bottom_navigation_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
</LinearLayout>
2.每一個fragment的XML文件android
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.jarek.library.TitleView xmlns:title="http://schemas.android.com/apk/res-auto" android:id="@+id/title_tab" android:layout_width="match_parent" title:title_name ="當前會話" title:title_text_color="@color/bgcolor" title:right_text = "添加" android:background="@color/navBarcolor" android:layout_height="50dp"/> <ListView android:id="@+id/current_list" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> <ImageView android:id="@+id/empty_list" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" android:src="@drawable/create_session2x"/> </LinearLayout>
3.Fragment 內容session
package com.example.shuodao.fragment; import android.app.Fragment; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleAdapter; import com.example.shuodao.one.CurrentChartActivity; import com.example.shuodao.one.R; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by shuodao on 2017/3/22. */ public class CurrentFragment extends Fragment { private ListView listView; private View contentView; private ImageView emptyView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { contentView = inflater.inflate(R.layout.current_fragment,null); initListView(); return contentView; } private void initListView(){ listView = (ListView) contentView.findViewById(R.id.current_list); emptyView = (ImageView) contentView.findViewById(R.id.empty_list); emptyView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { System.out.println("建立 新的會話"); } }); listView.setEmptyView(emptyView); SimpleAdapter adapter = new SimpleAdapter(getActivity().getApplicationContext(),getData(),R.layout.current_list, new String[]{"title","info","time","unread","img"}, new int[]{R.id.name_tit,R.id.last_chat,R.id.time,R.id.unread_cont,R.id.header_img}); // 設置適配器 listView.setAdapter(adapter); } // 數據 private List<Map<String,Object>> getData(){ List<Map<String,Object>> list = new ArrayList<Map<String,Object>>(); Map<String,Object> map = new HashMap<String, Object>(); map.put("title","111111"); map.put("info","hello world"); map.put("time","11111"); map.put("unread","11"); map.put("img",R.drawable.icon2x); list.add(map); map = new HashMap<String, Object>(); map.put("title","333333"); map.put("info","hello world"); map.put("time","11111"); map.put("unread","11"); map.put("img",R.drawable.icon2x); list.add(map); return list; } @Override public void onResume() { super.onResume(); //listView中每一個item點擊事件 listView.setOnItemClickListener(new listenr().itemClickListener); } public class listenr{ public AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(); intent.setClass(getActivity().getApplicationContext(),CurrentChartActivity.class); startActivity(intent); } }; } }
4.在activity中app
// 設置默認進來是tab 顯示的頁面 private void setDefaultFragment(){ fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.beginTransaction(); currentFragment = new CurrentFragment(); transaction.replace(R.id.fragment_content,currentFragment); transaction.commit(); }
/*** * * 切換 TabBar item 同時設置 切換fragment 改變activity界面內容 */ @Override public void onTabSelected(int position) { lastSelectedPosition = position; FragmentTransaction transaction = fragmentManager.beginTransaction(); switch (position){ case 0: System.out.println("當前會話"); if (currentFragment == null){ currentFragment = new CurrentFragment(); } transaction.replace(R.id.fragment_content,currentFragment); break; case 1: System.out.println("歷史會話"); if (historyFragment == null){ historyFragment = new HistoryFragment(); } transaction.replace(R.id.fragment_content,historyFragment); break; case 2: System.out.println("我"); break; } transaction.commit();// 提交事務 }