Fragment滑动切换简单案例
Fragment的产生与介绍Android运行在各种各样的设备中,有小屏幕的手机,超大屏的平板甚至电视。针对屏幕尺寸的差距,很多情况下,都是先针对手机开发一套App,然后拷贝一份,修改布局以适应平板神马超级大屏的。难道无法做到一个App可以同时适应手机和平板么,当然了,必须有啊。Fragment的出现就是为了解决这样的问题。你可以把Fragment当成Activity的一个界面的一个组成部分,甚至Activity的界面可以完全有不同的Fragment组成,更帅气的是Fragment拥有自己的生命周期和接收、处理用户的事件,这样就不必在Activity写一堆控件的事件处理的代码了。更为重要的是,你可以动态的添加、替换和移除某个Fragment
实例图片:
MainActivity
package com.shaoxin.myfragment; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.app.FragmentTransaction; 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 implements ViewPager.OnPageChangeListener { private FragmentManager fragment; private RadioGroup radioGroup; private FragmentTransaction fragmentTransaction; private FirstFragment firstFragment; private SecondFragment secondFragment; private ThirdFragment thirdFragment; private ViewPager viewpager; MyAdpter myAdpter; private List<Fragment> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取管理工具 fragment = getSupportFragmentManager(); radioGroup = (RadioGroup) findViewById(R.id.radio_group); viewpager = (ViewPager) findViewById(R.id.viewpager); //实例化fragment firstFragment = new FirstFragment(); secondFragment = new SecondFragment(); thirdFragment = new ThirdFragment(); list = new ArrayList<>(); list.add(firstFragment); list.add(secondFragment); list.add(thirdFragment); myAdpter = new MyAdpter(fragment); radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //开启事务 // fragmentTransaction = fragment.beginTransaction(); switch (checkedId) { case R.id.radio_masg: //替换填充view // fragmentTransaction.replace(R.id.viewpager, firstFragment); viewpager.setCurrentItem(0); break; case R.id.radio_imag: viewpager.setCurrentItem(1); // fragmentTransaction.replace(R.id.viewpager, secondFragment); break; case R.id.radio_setting: viewpager.setCurrentItem(2); // fragmentTransaction.replace(R.id.viewpager, thirdFragment); break; } //提交数据 // fragmentTransaction.commit(); } }); viewpager.setAdapter(myAdpter); viewpager.addOnPageChangeListener(this); } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { switch (position) { case 0: radioGroup.check(R.id.radio_masg); break; case 1: radioGroup.check(R.id.radio_imag); break; case 2: radioGroup.check(R.id.radio_setting); break; } } @Override public void onPageScrollStateChanged(int state) { } public class MyAdpter extends FragmentPagerAdapter { public MyAdpter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { return list.get(position); } @Override public int getCount() { return list.size(); } } }
FirstFragment
package com.shaoxin.myfragment; 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 shaoxin on 2016/12/11. */ public class FirstFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { container = (ViewGroup) inflater.inflate(R.layout.first_fragment, null); return container; } }
SecondFragment
package com.shaoxin.myfragment; 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 shaoxin on 2016/12/11. */ public class SecondFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { container = (ViewGroup) inflater.inflate(R.layout.sec_fragment, null); return container; } }
ThirdFragment
package com.shaoxin.myfragment; 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 shaoxin on 2016/12/11. */ public class ThirdFragment extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { container = (ViewGroup) inflater.inflate(R.layout.third_fragment, null); return container; } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.shaoxin.myfragment.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> <RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:orientation="horizontal"> <RadioButton android:id="@+id/radio_masg" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/button_background" android:gravity="center_horizontal" android:text="消息" /> <RadioButton android:id="@+id/radio_imag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/button_background" android:gravity="center_horizontal" android:text="图片" /> <RadioButton android:id="@+id/radio_setting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:button="@null" android:drawableTop="@drawable/button_background" android:gravity="center_horizontal" android:text="设置" /> </RadioGroup> </FrameLayout>
first_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"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/a"/> </LinearLayout>
sec_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"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/b"/> </LinearLayout>
third_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"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/b"/> </LinearLayout>
然后再drawable文件夹中创建文件button_background.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@android:drawable/star_on" android:state_checked="true" /> <item android:drawable="@android:drawable/star_off" android:state_checked="false" /> </selector>
另外的图片自行插入