仿微信底部切换页签

layout:下面

主界面:activity_main.xml

<?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"
android:orientation="vertical">

    <android.support.v4.view.ViewPager
android:id="@+id/main_ViewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
    </android.support.v4.view.ViewPager>

    <RadioGroup
android:id="@+id/tab_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/tab_menu_bg"
android:orientation="horizontal">

        <RadioButton
android:id="@+id/tab_square"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:background="@drawable/tab_selector_checked_bg"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/tab_selector_square"
android:gravity="center_horizontal|bottom"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/square_name"
android:textColor="@drawable/tab_selector_txt_color"
android:textSize="20dp"/>

        <RadioButton
android:id="@+id/tab_chat"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:background="@drawable/tab_selector_checked_bg"
android:button="@null"
android:drawableTop="@drawable/tab_selector_message"
android:gravity="center_horizontal|bottom"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/chat_name"
android:textColor="@drawable/tab_selector_txt_color"
android:textSize="20dp"/>

        <RadioButton
android:id="@+id/tab_mine"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:background="@drawable/tab_selector_checked_bg"
android:button="@null"
android:drawableTop="@drawable/tab_selector_mine"
android:gravity="center_horizontal|bottom"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="@string/mine_name"
android:textColor="@drawable/tab_selector_txt_color"
android:textSize="20dp"/>
    </RadioGroup>

</LinearLayout>
fragment_chat.xml:
<?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"
android:orientation="vertical">

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是聊天界面"/>
</LinearLayout>
fragment_mine.xml:
<?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"
android:orientation="vertical">

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是我的界面"/>
</LinearLayout>
fragment_square.xml:
<?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"
android:orientation="vertical">

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是我的界面"/>
</LinearLayout>

 fragment_chat.xml

<?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"
              android:orientation="vertical">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是聊天界面"/>
</LinearLayout>

 fragment_mine.xml

<?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"
              android:orientation="vertical">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是聊天界面"/>
</LinearLayout>

 fragment_square.xml

<?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"
              android:orientation="vertical">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="这是广场的界面"/>
</LinearLayout>

MainActivity.java:

package com.xba.answerpro;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.xba.answerpro.fragment.ChatFragment;
import com.xba.answerpro.fragment.MineFragment;
import com.xba.answerpro.fragment.SquareFragment;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener, ViewPager.OnPageChangeListener {
    private ViewPager mViewPager;
    private RadioGroup mTabMenu;
    private RadioButton mTabSquare;
    private RadioButton mTabChat;
    private RadioButton mTabMine;
    private Fragment squareFragment;
    private Fragment chatFragment;
    private Fragment mineFragment;
    private ArrayList<Fragment> mTabMenus;

    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        squareFragment = new Fragment();
        chatFragment = new Fragment();
        mineFragment = new Fragment();

        mTabMenu = (RadioGroup) findViewById(R.id.tab_menu);
        mTabSquare = (RadioButton) findViewById(R.id.tab_square);
        mTabChat = (RadioButton) findViewById(R.id.tab_chat);
        mTabMine = (RadioButton) findViewById(R.id.tab_mine);
        mTabMenu.setOnCheckedChangeListener(this);
        initViewPager();
    }

    public void initViewPager() {
        mViewPager = (ViewPager) findViewById(R.id.main_ViewPager);
        squareFragment = new SquareFragment();
        chatFragment = new ChatFragment();
        mineFragment = new MineFragment();
        mTabMenus = new ArrayList<Fragment>();
        mTabMenus.add(squareFragment);
        mTabMenus.add(chatFragment);
        mTabMenus.add(mineFragment);

        FragmentManager fm = getSupportFragmentManager();
        mViewPager.setAdapter(new MyAdapter(fm, mTabMenus));
        mViewPager.setCurrentItem(0);
        mViewPager.addOnPageChangeListener(this);
    }


    @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
        int current = 0;
        switch (checkedId) {
            case R.id.tab_square:
                current = 0;
                break;
            case R.id.tab_chat:
                current = 1;
                break;
            case R.id.tab_mine:
                current = 2;
                break;
        }
        if (mViewPager.getCurrentItem() != current) {
            mViewPager.setCurrentItem(current);
        }
    }

    @Override
public void onPageSelected(int position) {
        switch (mViewPager.getCurrentItem()) {
            case 0:
                mTabMenu.check(R.id.tab_square);
                break;
            case 1:
                mTabMenu.check(R.id.tab_chat);
                break;
            case 2:
                mTabMenu.check(R.id.tab_mine);
                break;
        }

    }

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

    }

    @Override
public void onPageScrollStateChanged(int state) {
    }

    public class MyAdapter extends FragmentPagerAdapter {
        ArrayList<Fragment> list;

        public MyAdapter(FragmentManager fm, ArrayList<Fragment> arrayList) {
            super(fm);
            this.list = arrayList;
        }

        @Override
public int getCount() {
            return list.size();
        }

        @Override
public Fragment getItem(int arg0) {
            return list.get(arg0);
        }
    }
}

 ChatFragment.java:

package com.xba.answerpro.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.xba.answerpro.R;

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

    @Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

 MineFragment.java:

扫描二维码关注公众号,回复: 317144 查看本文章
package com.xba.answerpro.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.xba.answerpro.R;

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

    @Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

 SquareFragment.java:

package com.xba.answerpro.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.xba.answerpro.R;

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

    @Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

 drawable资源文件已上传

猜你喜欢

转载自ch-kexin.iteye.com/blog/2286715