小案例

MainActivity

package com.example.javaniceyou.weeek2test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.example.javaniceyou.weeek2test.fragment.Tab1Fragment;
import com.example.javaniceyou.weeek2test.fragment.Tab2Fragment;
import com.example.javaniceyou.weeek2test.fragment.Tab3Fragment;
import com.example.javaniceyou.weeek2test.fragment.Tab4Fragment;
import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.BottomBarTab;
import com.roughike.bottombar.OnTabSelectListener;

public class MainActivity extends AppCompatActivity {

    private BottomBar bottomBar;
    BottomBarTab tab1;
    BottomBarTab tab3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        tab1 = bottomBar.getTabWithId(R.id.tab1);//通过得到BottomBarTab的某个tab选项卡
        tab1.setBadgeCount(5);//设置未读消息的数量
        tab3 = bottomBar.getTabWithId(R.id.tab3);//通过得到BottomBarTab的某个tab选项卡
        tab3.setBadgeCount(10);//设置未读消息的数量

        //对BottomBar设置点击事件
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(int tabId) {//tabId表示的是每个tab的id
                switch (tabId){
                    case R.id.tab1:
                        //做具体的点击出来

                        tab1.removeBadge();//移除未读消息的数量
                        //切换Fragment
                         // replace(@IdRes int containerViewId, Fragment fragment)
                    getSupportFragmentManager().beginTransaction().replace(R.id.container, new Tab1Fragment()).commit() ;
                        break;
                    case R.id.tab2:
                        //做具体的点击出来

                        //切换Fragment
                        getSupportFragmentManager().beginTransaction().replace(R.id.container, new Tab2Fragment()).commit() ;
                        break;

                    case R.id.tab3:
                        //做具体的点击出来
                        tab3.removeBadge();//移除未读消息的数量
                        //切换Fragment
                        getSupportFragmentManager().beginTransaction().replace(R.id.container, new Tab3Fragment()).commit() ;
                        break;

                    case R.id.tab4:
                        //做具体的点击出来
                        getSupportFragmentManager().beginTransaction().replace(R.id.container, new Tab4Fragment()).commit() ;
                        break;
                }

            }
        });


    }

    private void initView() {
        bottomBar = (BottomBar) findViewById(R.id.bottomBar);
    }
}

  布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.javaniceyou.weeek2test.MainActivity"
    android:orientation="vertical">

   <LinearLayout
       android:id="@+id/container"
       android:layout_weight="4"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:orientation="vertical">

   </LinearLayout>

   <com.roughike.bottombar.BottomBar
       android:id="@+id/bottomBar"
       android:layout_weight="2"
       android:layout_width="wrap_content"
       android:layout_height="0dp"

       android:layout_alignParentBottom="true"
       app:bb_tabXmlResource="@xml/bottombar_color_activity"


       >

   </com.roughike.bottombar.BottomBar>

</LinearLayout>

  fragment

 Tab1Fragment

package com.example.javaniceyou.weeek2test.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.javaniceyou.weeek2test.R;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class Tab1Fragment extends Fragment {
    RecyclerView recyclerView;
    ArrayList<String> dataSourceInstance = new ArrayList<>() ;

    public Tab1Fragment() {
        // Required empty public constructor

        for (int i = 0; i < 100; i++) {
            dataSourceInstance.add("这是第" + i + "个条目");
        }
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_tab1, null);

        recyclerView  = view.findViewById(R.id.tab1_recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);

//        return inflater.inflate(R.layout.fragment_tab1, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //第一步从布局fragment_tab1找到RecyclerView
//        RecyclerView recyclerView  = view.findViewById(R.id.tab1_recyclerView);
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getActivity(), LinearLayoutManager.VERTICAL, false);
//        recyclerView.setLayoutManager(linearLayoutManager);

        //第二步:为RecyclerView创建适配器

        Tab1FragmentRecyclerViewAdapter tab1FragmentRecyclerViewAdapter = new Tab1FragmentRecyclerViewAdapter(dataSourceInstance );


        //第三步:为RecyclerView绑定适配器
        recyclerView.setAdapter(tab1FragmentRecyclerViewAdapter);
    }

    class Tab1FragmentRecyclerViewAdapter extends RecyclerView.Adapter<Tab1FragmentRecyclerViewAdapter.MyViewHolder> {


        ArrayList<String> dataSource ;

        public Tab1FragmentRecyclerViewAdapter(  ArrayList<String> dataSource) {
            this.dataSource = dataSource;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            View view = layoutInflater.inflate(R.layout.tab1_recyclerview_item_layout, null);

            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.tab1_recyclerView_item.setText(dataSourceInstance.get(position));

        }

        @Override
        public int getItemCount() {
            return dataSourceInstance.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder /*extends RecyclerView.ViewHolder*/{


            TextView tab1_recyclerView_item;
            public MyViewHolder(View itemView) {
                super(itemView);
                tab1_recyclerView_item = itemView.findViewById(R.id.tab1_recyclerView_item);
            }
        }
    }




}

 Tab2Fragment

package com.example.javaniceyou.weeek2test.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.javaniceyou.weeek2test.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class Tab2Fragment extends Fragment {


    public Tab2Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Toast.makeText(getActivity(), "toast的内容", Toast.LENGTH_LONG).show();
        return inflater.inflate(R.layout.fragment_tab2, container, false);
    }

}

 Tab3Fragment

package com.example.javaniceyou.weeek2test.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.javaniceyou.weeek2test.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class Tab3Fragment extends Fragment {


    public Tab3Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_tab3, container, false);
    }

}

 Tab4Fragment

package com.example.javaniceyou.weeek2test.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.javaniceyou.weeek2test.R;
import com.example.javaniceyou.weeek2test.section_recyclerview_viewholder.CountBodyItemViewHolder;
import com.example.javaniceyou.weeek2test.section_recyclerview_viewholder.CountFooterViewHolder;
import com.example.javaniceyou.weeek2test.section_recyclerview_viewholder.CountHeaderViewHolder;
import com.truizlop.sectionedrecyclerview.SectionedRecyclerViewAdapter;
import com.truizlop.sectionedrecyclerview.SectionedSpanSizeLookup;

/**
 * A simple {@link Fragment} subclass.
 */
public class Tab4Fragment extends Fragment {


    public Tab4Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
        View view = layoutInflater.inflate(R.layout.fragment_tab4, null);
        //第一步:创建RecyclerView的实例
        RecyclerView recycler = (RecyclerView)view.findViewById(R.id.recycler);
        //创建布局方式
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 2);
        //为RecyclerView设置布局方式,这是表格方式
        recycler.setLayoutManager(layoutManager);

        //第二步:创建RecyclerView的适配器
        SectionRecylerViewAdapter sectionRecylerViewAdapter = new SectionRecylerViewAdapter();




        //控制sectionRecylcerView分块展示
        SectionedSpanSizeLookup lookup = new SectionedSpanSizeLookup(sectionRecylerViewAdapter, layoutManager);
        layoutManager.setSpanSizeLookup(lookup);
        //第三步:为RecyclerView绑定适配器
        recycler.setAdapter(sectionRecylerViewAdapter);



        return view;
    }


    class SectionRecylerViewAdapter extends SectionedRecyclerViewAdapter<CountHeaderViewHolder, CountBodyItemViewHolder, CountFooterViewHolder> {
        //表示一共有几段
        @Override
        protected int getSectionCount() {
            return 5;
        }
        //表示每一段有几个内容
        @Override
        protected int getItemCountForSection(int section) {
            return section + 1;
        }

        @Override
        protected boolean hasFooterInSection(int section) {
            return true;
        }


        @Override
        protected CountHeaderViewHolder onCreateSectionHeaderViewHolder(ViewGroup parent, int viewType) {

            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            View view = layoutInflater.inflate(R.layout.sectionview_count_header, null);
            CountHeaderViewHolder countHeaderViewHolder = new CountHeaderViewHolder(view);
            return countHeaderViewHolder;
        }

        @Override
        protected CountFooterViewHolder onCreateSectionFooterViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            View view = layoutInflater.inflate(R.layout.sectionview_count_footer, null);
            CountFooterViewHolder countFooterViewHolder = new CountFooterViewHolder(view);
            return countFooterViewHolder;
        }

        @Override
        protected CountBodyItemViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            View view = layoutInflater.inflate(R.layout.sectionview_count_body, null);
            CountBodyItemViewHolder countBodyItemViewHolder = new CountBodyItemViewHolder(view);
            return countBodyItemViewHolder;
        }

        @Override
        protected void onBindSectionHeaderViewHolder(CountHeaderViewHolder holder, int section) {

            holder.section_title.setText("heaer"+section);
        }

        @Override
        protected void onBindSectionFooterViewHolder(CountFooterViewHolder holder, int section) {
            holder.footer_title.setText("footer" + section);
        }

        @Override
        protected void onBindItemViewHolder(CountBodyItemViewHolder holder, int section, int position) {
            holder.section_body.setText("第" + position + "个身体的item");

        }
    }


}

 section_recyclerview_viewholder

 CountBodyItemViewHolder

package com.example.javaniceyou.weeek2test.section_recyclerview_viewholder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.javaniceyou.weeek2test.R;

/**
 * Created by javaniceyou on 2018/3/17.
 */

public class CountBodyItemViewHolder extends RecyclerView.ViewHolder {
    public  TextView section_body;
    public CountBodyItemViewHolder(View itemView) {
        super(itemView);
        section_body =(TextView)itemView.findViewById(R.id.section_body);
    }
}

 CountFooterViewHolder

package com.example.javaniceyou.weeek2test.section_recyclerview_viewholder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.javaniceyou.weeek2test.R;

/**
 * Created by javaniceyou on 2018/3/17.
 */

public class CountFooterViewHolder extends RecyclerView.ViewHolder{
    public  TextView footer_title;
    public CountFooterViewHolder(View itemView) {
        super(itemView);
        footer_title = (TextView)itemView.findViewById(R.id.footer_title);
    }
}

 CountHeaderViewHolder

package com.example.javaniceyou.weeek2test.section_recyclerview_viewholder;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.javaniceyou.weeek2test.R;

/**
 * Created by javaniceyou on 2018/3/17.
 */

public class CountHeaderViewHolder  extends RecyclerView.ViewHolder{

    public TextView section_title;
    public CountHeaderViewHolder(View itemView) {
        super(itemView);

        section_title = (TextView)itemView.findViewById(R.id.section_title);
    }
}

 布局

fragment_tab1

<FrameLayout 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"
    tools:context="com.example.javaniceyou.weeek2test.fragment.Tab1Fragment">

  <!--  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="床前明月光,疑是地上霜"
        />-->
    <!-- TODO: Update blank fragment layout -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/tab1_recyclerView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>

</FrameLayout>

  fragment_tab2

<FrameLayout 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"
    tools:context="com.example.javaniceyou.weeek2test.fragment.Tab2Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="饿,饿,饿,曲项向天歌,白毛浮绿水,红掌拨清波" />

</FrameLayout>

 fragment_tab3

<FrameLayout 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"
    tools:context="com.example.javaniceyou.weeek2test.fragment.Tab3Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="轻轻地,我走啦,正如我轻轻的来!" />

</FrameLayout>

 fragment_tab4

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>


</RelativeLayout>

 sectionview_count_body

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="48dp">
    <TextView
        android:id="@+id/section_body"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp" />

</LinearLayout>

 sectionview_count_footer

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="32dp">
    <TextView
        android:id="@+id/footer_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:textColor="@android:color/black"
        android:textSize="10sp"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        />
</LinearLayout>

 sectionview_count_header

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="48dp">
    <TextView
        android:id="@+id/section_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold"
        android:textAllCaps="true"
        android:layout_gravity="center_vertical"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp" />

</LinearLayout>

 tab1_recyclerview_item_layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tab1_recyclerView_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

  XML

 bottom_bar_tabs

<?xml version="1.0" encoding="utf-8"?>
<tabs>
   <tab
       id="@+id/tab1"
       title="tab1"
       icon="@drawable/ic_launcher"
       >

   </tab>

    <tab
        id="@+id/tab2"
        title="tab2"
        icon="@drawable/ic_launcher"
        >

    </tab>

    <tab
        id="@+id/tab3"
        title="tab3"
        icon="@drawable/ic_launcher"
        >

    </tab>

    <tab
        id="@+id/tab4"
        title="tab4"
        icon="@drawable/ic_launcher"
        >

    </tab>

</tabs>

 bottombar_color_activity

<?xml version="1.0" encoding="utf-8"?>
<tabs>
    <tab
        barColorWhenSelected="#5D4037"
        icon="@drawable/ic_favorites"
        id="@+id/tab1"
        title="Favorites"/>
    <tab

        barColorWhenSelected="#f44336"
        icon="@drawable/ic_nearby"
        id="@+id/tab2"
        title="Nearby"/>
    <tab
        barColorWhenSelected="#6A1B9A"
        icon="@drawable/ic_friends"
        id="@+id/tab3"
        title="Friends"/>
    <tab
        barColorWhenSelected="#304FFE"
        icon="@drawable/ic_recents"
        id="@+id/tab4"
        title="Recent"/>
    <tab
        barColorWhenSelected="#009688"
        icon="@drawable/ic_restaurants"
        id="@+id/tab5"
        title="restaurants"/>
</tabs>

  bottombar_sample_activity

<?xml version="1.0" encoding="utf-8"?>
<tabs>
    <tab
        icon="@drawable/ic_favorites"
        id="@+id/tab1"
        title="Favorites"/>
    <tab
        icon="@drawable/ic_nearby"
        id="@+id/tab2"
        title="Nearby"/>
    <tab
        icon="@drawable/ic_friends"
        id="@+id/tab3"
        title="Friends"/>
    <tab
        icon="@drawable/ic_recents"
        id="@+id/tab4"
        title="Recent"/>
    <tab
        icon="@drawable/ic_restaurants"
        id="@+id/tab5"
        title="restaurants"/>
</tabs>

  添加依赖

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.javaniceyou.weeek2test"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:support-v4:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    //bottomBar的依赖
    compile 'com.roughike:bottom-bar:2.0.2'
    implementation project(':library-sectionrecycleView')
}

mipmap-anydpi-v26

ic_launcher

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@drawable/ic_launcher_background" />
    <foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>

 ic_launcher_round

<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@drawable/ic_launcher_background" />
    <foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值