ViewPager+RadioGroup 实现高仿微信底部栏

一、布局


  1. activity_main:



    

    

        

        

        

        
    

  1. view_chat:



    

  1. view_contacts:



    

  1. view_discovery:



    

  1. view_me:



    


二、代码


package com.example.ttt.wechattest;

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

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

public class MainActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private List fragmentList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewPager=(ViewPager)findViewById(R.id.view_pager);
        radioGroup=(RadioGroup)findViewById(R.id.radio_group);

        ChatFragment chatFragment=new ChatFragment();
        ContactsFragment contactsFragment=new ContactsFragment();
        DiscoveryFragment discoveryFragment=new DiscoveryFragment();
        MeFragment meFragment=new MeFragment();

        fragmentList.add(chatFragment);
        fragmentList.add(contactsFragment);
        fragmentList.add(discoveryFragment);
        fragmentList.add(meFragment);

        viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),fragmentList));

        // ViewPager页面切换监听
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        radioGroup.check(R.id.chat);
                        break;
                    case 1:
                        radioGroup.check(R.id.contacts);
                        break;
                    case 2:
                        radioGroup.check(R.id.discovery);
                        break;
                    case 3:
                        radioGroup.check(R.id.me);
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        // RadioGroup选中状态改变监听
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.chat:
                        /**
                         * setCurrentItem第二个参数控制页面切换动画
                         * true:打开/false:关闭
                         */
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.contacts:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.discovery:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.me:
                        viewPager.setCurrentItem(3);
                        break;
                    default:
                        break;
                }
            }
        });
    }
}

MyFragmentPagerAdapter:

package com.example.ttt.wechattest;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    private List list;

    public MyFragmentPagerAdapter(FragmentManager fm, List list){
        super(fm);
        this.list=list;
    }

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

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

ChatFragment:(其他三个类似)

package com.example.ttt.wechattest;

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

public class ChatFragment extends Fragment{

    public ChatFragment(){

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.view_chat,container,false);
    }
}

三、drawable 文件夹


  1. chat_selector


    
    

  1. contacts_selector


    
    

  1. discovery_selector


    
    

  1. me_selector


    
    

  1. color_selector


    
    


四、效果


  • 可拖动也可点击图标翻页。
ViewPager+RadioGroup 实现高仿微信底部栏_第1张图片

你可能感兴趣的:(ViewPager+RadioGroup 实现高仿微信底部栏)