Android-Fragment(模微信界面)

2019-04-13 17:29发布

 代码实现fragment左右滑动改变按钮样式,点击按钮切换fragment


在android Studio 
四个布局文件 和actity
1.weixin_Fragment.xml -------------------------------------------------------------------- xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/weixin" android:textSize="30dp" /> LinearLayout> activity------------------------------------------public class Fragment_WeiXin extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.weixin_fragment,null ); } }
2.contacts_fragment.xml ------------------------------------------ xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/contacts" /> LinearLayout>
activity --------------------------------------- public class Fragment_Contacts extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.contacts_fragment,null); } }
3.find_fragment.xml ------------------------------------------------------------------ xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/find"/> LinearLayout> activity ---------------------------------------- public class Fragment_Find extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.find_fragment,null); } }
4.me_fragment -------------------------------------- xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/me"/> LinearLayout> activity----------------------------------------public class Fragment_Me extends Fragment{ @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.me_fragment,null); } }

5.主界面(activity) 和布局文件 5.1 activity_main.xml xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.g160628_android_10_1_fragment.MainActivity" android:weightSum="1"> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="0dp" android:id="@+id/vp_ViewPager_page" android:layout_weight="1"> android.support.v4.view.ViewPager> <RadioGroup android:id="@+id/rg_radiogroup_radio" android:gravity="center" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <RadioButton android:button="@null" android:drawableTop="@drawable/button_one" android:padding="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_RadioButton_one" android:text="微信"/> <RadioButton android:button="@null" android:drawableTop="@drawable/button_tow" android:padding="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_RadioButton_tow" android:text="通讯录"/> <RadioButton android:button="@null" android:drawableTop="@drawable/button_three" android:padding="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_RadioButton_three" android:text="发现"/> <RadioButton android:drawableTop="@drawable/button_four" android:button="@null" android:padding="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rb_RadioButton_four" android:text="我"/> RadioGroup> LinearLayout> 5.2 MainActivity---------------------------------------- package com.example.g160628_android_10_1_fragment; 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.widget.RadioGroup; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ViewPager vp; private List list=new ArrayList<>(); private RadioGroup rg; int data[]={R.id.rb_RadioButton_one,R.id.rb_RadioButton_tow,R.id.rb_RadioButton_three,R.id.rb_RadioButton_four}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = (ViewPager) findViewById(R.id.vp_ViewPager_page); Fragment_WeiXin fragment_weiXin=new Fragment_WeiXin(); Fragment_Contacts fragment_contacts=new Fragment_Contacts(); Fragment_Find fragment_find=new Fragment_Find(); Fragment_Me fragment_me=new Fragment_Me(); list.add(fragment_weiXin); list.add(fragment_contacts); list.add(fragment_find); list.add(fragment_me); vp.setAdapter(new MyAdapter(getSupportFragmentManager())); rg = (RadioGroup) findViewById(R.id.rg_radiogroup_radio); //第一次进来 vp.setCurrentItem(0); rg.check(data[0]); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) { for (int i = 0; i < data.length; i++) { if(checkedId==data[i]){ vp.setCurrentItem(i); break; } } } }); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { // state==1; // state==2;滑动完毕 // state==0; if(state==2){ rg.check(data[vp.getCurrentItem()]); } } }); } class MyAdapter extends FragmentPagerAdapter{ public MyAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } } }




在drawable里新建一个选择器(因为代码相同就只写一遍) xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/demon_start">item> <item android:state_checked="false" android:drawable="@drawable/demon_end">item> selector>