1.主界面布局文件
?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文件
<?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 内容
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中
// 设置默认进来是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();// 提交事务 }