底部菜单fragmentTabHost和viewpager

2016-09-20  本文已影响0人  sun_goden

想要实现底部菜单使用fragmentTabHost,可以点击可滑动


Paste_Image.png

上代码

![Uploading Paste_Image_581832.png . . .]

布局activity_main.xml

<?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="a.demo_cainiao.MainActivity">
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp"></android.support.v4.view.ViewPager>
    <FrameLayout
        android:id="@+id/ll"
        android:visibility="gone"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp">

    </FrameLayout>
<android.support.v4.app.FragmentTabHost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_weight="0"
        android:layout_height="0dp"></FrameLayout>
</android.support.v4.app.FragmentTabHost>

</LinearLayout>

给每一个菜单设置点击效果drawable/下
select_item.xml,select_item1.xml,select_item2.xml,select_item3.xml四个对应四个Tab

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="false" android:drawable="@mipmap/icon_home"></item>
    <item android:state_selected="true" android:drawable="@mipmap/icon_home_press"></item>
</selector>

Mainactivity.class

package a.demo_cainiao;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener,TabHost.OnTabChangeListener{
        private FragmentTabHost fragmentTabHost;
    private ViewPager viewPager;
    private    View view;
    private    View view1;
    private List<Fragment> fragments;
    private  int [] images={R.mipmap.icon_home,R.mipmap.icon_discover};
    private  String [] name={"首页","设置","个人","收藏"};
    private  int []  v={R.drawable.select_item,R.drawable.select_item1,R.drawable.select_item2,R.drawable.select_item3};
    private  Class [] list=new Class[4];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager= (ViewPager) findViewById(R.id.pager);
        viewPager.addOnPageChangeListener((ViewPager.OnPageChangeListener) this);

        fragmentTabHost= (FragmentTabHost) findViewById(android.R.id.tabhost);

        fragmentTabHost.setup(this,getSupportFragmentManager(),R.id.pager);


//定义四个fragement
      Class homeFragment= new HomeFragment().getClass();//每个菜单绑定一个Fragment
        Class shoppingFragment=new ShoppingFragment().getClass();
        Class settingFragment=new SettingFragment().getClass();
        Class persionFragment=new PersionFragment().getClass();
        list[0]=homeFragment;
        list[1]=shoppingFragment;
        list[2]=settingFragment;
        list[3]=persionFragment;

        for (int i=0;i<4;i++) {//添加四个底部菜单
            fragmentTabHost.addTab(fragmentTabHost.newTabSpec(name[i]).setIndicator(getView(i)), list[i], null);
            //给每一个Tab添加点击监听事件,TabOnClickListener是自己定义的OnclickListener
            fragmentTabHost.getTabWidget().getChildTabViewAt(i).setOnClickListener(new TabOnClickListener(fragmentTabHost,viewPager,i));
        }
        init();

    }


    private View getView(int i) {//给每一个菜单设置图片和文字
        view= LayoutInflater.from(this).inflate(R.layout.tab,null);
        TextView textView;
        textView = (TextView) view.findViewById(R.id.text_shouye);
        textView.setText(name[i]);
        ImageView imageView;
        imageView= (ImageView) view.findViewById(R.id.button);
        imageView.setBackgroundResource(v[i]);

        return  view;
    }

    //给viewpager添加fragment,添加适配器
    private void init() {
        fragments=new ArrayList<Fragment>();
        HomeFragment homeFragment=new HomeFragment();
        SettingFragment settingFragment=new SettingFragment();
        PersionFragment persionFragment=new PersionFragment();
        ShoppingFragment shoppingFragment=new ShoppingFragment();
        fragments.add(homeFragment);
        fragments.add(settingFragment);
        fragments.add(shoppingFragment);
        fragments.add(persionFragment);
        viewPager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager(),fragments));
       fragmentTabHost.getTabWidget().setDividerDrawable(null);//取消分割线
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {//position表示当前的选中的位置,这事件是页面跳转完毕的时候调用的
        TabWidget tabWidget=fragmentTabHost.getTabWidget();
        int oldFoucsability= tabWidget.getDescendantFocusability();
        tabWidget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);//设置view覆盖子类控件而获得焦点
        fragmentTabHost.setCurrentTab(position);//根据位置position设置当前的tab
        tabWidget.setDescendantFocusability(oldFoucsability);//设置取消分割线
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }
    @Override
    public void onTabChanged(String tabId) {//tab改变的时候调用
        int position=fragmentTabHost.getCurrentTab();
        viewPager.setCurrentItem(position);//把选中的Tab位置赋给适配器,让他控制页面改变

    }
}

自己定义的OnClickListener

package a.demo_cainiao;

import android.content.DialogInterface;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * Created by 我 on 2016/9/19.
 */
public class TabOnClickListener implements View.OnClickListener {
    private FragmentTabHost fragmentTabHost;
    private  int index;
    private  ViewPager viewPager;
   public  TabOnClickListener(FragmentTabHost fragmentTabHost, ViewPager viewPager, int index){
        this.fragmentTabHost=fragmentTabHost;
       this.index=index;
       this.viewPager=viewPager;
    }
    @Override
    public void onClick(View v) {
        viewPager.setCurrentItem(index);//设置点击事件的跳转,viewpager的子fragment
    }
}
上一篇下一篇

猜你喜欢

热点阅读