Android开发中使用Fragment切换BottomNavigationBar选项

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();// 提交事务
    }

 

转载于:https://my.oschina.net/u/2532095/blog/865298

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值