Android自定义底部导航栏-Tabbar

一、添加依赖

 //view,事件绑定
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

二、开始写布局

1、Tabbar布局页面




    
    
    

        
        

            

                
                

            

        

        
        

            

                
                
                
            

        

    


2、fragment_test.xml布局页面

这个页面就是FrameLayout 中要显示的页面




    

三、开始写代码

1、ShowTabbarActivity.Activity

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bh.pql.Fragment.TestFragment1;
import com.bh.pql.Fragment.TestFragment2;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShowtabbarActivity extends AppCompatActivity {

    @BindView(R.id.content)
    FrameLayout flayout;
    @BindView(R.id.tabbar_group)
    ImageView tabbarGroup;
    @BindView(R.id.tabbar_group_text)
    TextView tabbarGroupText;
    @BindView(R.id.tabbar_mine)
    ImageView tabbarMine;
    @BindView(R.id.tabbar_mine_text)
    TextView tabbarMineText;
    @BindView(R.id.tabbar_group_layout)
    LinearLayout tabbarGroupLayout;
    @BindView(R.id.tabbar_mine_layout)
    LinearLayout tabbarMineLayout;

    private TestFragment1 testFragment1;
    private TestFragment2 testFragment2;

    FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_showtabbar);
        ButterKnife.bind(this);

        fragmentManager = getSupportFragmentManager();          // 开启一个事物
        // 默认加载某一个tabbarItem(第一个按钮)
        testFragment1 = new TestFragment1();
        // 启动Activity时使第一个按钮的图标为选中状态(投机取巧)
        tabbarGroup.setImageResource(R.drawable.tabbar_group_select);
        tabbarGroupText.setTextColor(getResources().getColor(R.color.colorAccent));
        getSupportFragmentManager().beginTransaction().add(R.id.content, testFragment1).commit();
    }

    @OnClick({R.id.tabbar_group_layout, R.id.tabbar_mine_layout})
    public void onViewClicked(View view) {
        clearChioce();
        // 重置选项+隐藏所有的Fragment
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (view.getId()) {
            case R.id.tabbar_group_layout:
                hideFragments(transaction);
                tabbarGroup.setImageResource(R.drawable.tabbar_group_select);
                tabbarGroupText.setTextColor(getResources().getColor(R.color.colorAccent));
                if (testFragment1 == null) {
                    testFragment1 = new TestFragment1();
                    transaction.add(R.id.content, testFragment1);
                } else {
                    transaction.show(testFragment1);
                }
                break;
            case R.id.tabbar_mine_layout:
                hideFragments(transaction);
                tabbarMine.setImageResource(R.drawable.tabbar_mine_select);
                tabbarMineText.setTextColor(getResources().getColor(R.color.colorAccent));
                if (testFragment2 == null) {
                    testFragment2 = new TestFragment2();
                    transaction.add(R.id.content, testFragment2);
                } else {
                    transaction.show(testFragment2);
                }
                break;
        }
        transaction.commit();
    }

    /**
     * 将所有的Fragment都设置为隐藏状态
     *
     * @param transaction 事物
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (testFragment1 != null) {
            transaction.hide(testFragment1);
        }
        if (testFragment2 != null) {
            transaction.hide(testFragment2);
        }

    }

    /**
     * 定义一个重置所有选项的方法
     */
    public void clearChioce() {
        tabbarGroup.setImageResource(R.drawable.tabbar_group_normal);
        tabbarGroupText.setTextColor(getResources().getColor(R.color.colorTextGray));

        tabbarMine.setImageResource(R.drawable.tabbar_mine_normal);
        tabbarMineText.setTextColor(getResources().getColor(R.color.colorTextGray));

    }
}

2、TestFragment1

必须是继承Fragment的类

public class TestFragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_pt_listview_heade, container,false);
        return view;
    }
}

你可能感兴趣的:(Android自定义底部导航栏-Tabbar)