Android_ViewPage_Fragment_模仿微信界面

实现滑动切换碎片,和点击切换碎片并同时更改按钮的图片:

效果图展示:

Android_ViewPage_Fragment_模仿微信界面_第1张图片          Android_ViewPage_Fragment_模仿微信界面_第2张图片

点击按钮时:

Android_ViewPage_Fragment_模仿微信界面_第3张图片

实现思路:

实现编码:

drawable中的选择器(selector.xml):



    
    



cantactsfragment(通讯录)布局:




    

findfragment(发现)布局:




    

myfragment(我)布局:




    

weixinfragment(微信)布局:




    

activity_main(主)布局:





    
    
        
        
        
        
    

FindGragment发现的碎片:

package com.zking.admin.android_6_14_viewpagefragment;

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

/**
 * Created by admin on 2017/6/15.
 */

public class FindFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.findfragment,null);
    }
}

ContactsFragment通讯录的碎片:

package com.zking.admin.android_6_14_viewpagefragment;

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

/**
 * Created by admin on 2017/6/15.
 */

public class ContactsFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.contactsfragment,null);
    }
}

MyFragment我的碎片:

package com.zking.admin.android_6_14_viewpagefragment;

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

/**
 * Created by admin on 2017/6/15.
 */

public class MyFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.myfragment,null);
    }
}

WeixinFragment微信碎片:

package com.zking.admin.android_6_14_viewpagefragment;

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

/**
 * Created by admin on 2017/6/15.
 */

public class WinxinFragment  extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.weixinfragment,null);
    }
}

MainActivity主Avtivity文件:

package com.zking.admin.android_6_14_viewpagefragment;

import android.graphics.Color;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
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.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    private ViewPager vp_main_viewpager;
    private List lists;
    private RadioGroup rg_main_radiogroup;
    private List buttons;
    private ViewPager vp_main_viewpage;
    private List lists1;
    private RadioGroup rg_main_radiogroup1;
    private List views;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp_main_viewpage = (ViewPager) findViewById(R.id.vp_main_viewpage);
        rg_main_radiogroup1 = (RadioGroup) findViewById(R.id.rg_main_radiogroup);
        lists1 = new ArrayList<>();
        lists1.add(new WinxinFragment());
        lists1.add(new ContactsFragment());
        lists1.add(new FindFragment());
        lists1.add(new MyFragment());
        vp_main_viewpage.setAdapter(new Myadapter(getSupportFragmentManager()));
        vp_main_viewpage.setCurrentItem(0);
        views = rg_main_radiogroup1.getTouchables();
        RadioButton button1= (RadioButton) views.get(0);
        button1.setChecked(true);
        vp_main_viewpage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                RadioButton button3= (RadioButton) views.get(position);
                button3.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        rg_main_radiogroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                for (int i = 0; i < views.size(); i++) {
                    RadioButton button2= (RadioButton) views.get(i);
                    if (checkedId==button2.getId()){
                        button2.setChecked(true);
                        vp_main_viewpage.setCurrentItem(i);
                    }
                }
            }
        });
    }

    class Myadapter extends FragmentPagerAdapter{

        public Myadapter(FragmentManager fm) {
            super(fm);
        }

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

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

}


你可能感兴趣的:(ViewPage,Fragment,碎片,切换)