最近研究了一下fragement的页面切换问题,网上查了许久,发现有滑动切换与点击切换,我按照大神的代码与例子实现了一下,发现两种方式不能共存(我找到一篇据说能共存的博客,但是里面滑动切换是使用的手势切换,我看那个方法已经废弃了~~),所以两种方式只能使用一种,下面是代码:
第一种:点击切换。
效果图:
代码:
MainActivity:
import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.RadioGroup; import com.example.user.twodome.fragment.Fragmentone; import com.example.user.twodome.fragment.Fragmentthree; import com.example.user.twodome.fragment.Fragmenttwo; public class MainActivity extends AppCompatActivity { private RadioGroup rg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rg=(RadioGroup)findViewById(R.id.rg); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); switch (checkedId) { case R.id.b1: Fragmentone fragmentone = new Fragmentone(); fragmentTransaction.replace(R.id.fl_container, fragmentone, "").commit(); break; case R.id.b2: Fragmenttwo fragmenttwo = new Fragmenttwo(); fragmentTransaction.replace(R.id.fl_container, fragmenttwo, "").commit(); break; case R.id.b3: Fragmentthree fragmentthree = new Fragmentthree(); fragmentTransaction.replace(R.id.fl_container, fragmentthree, "").commit(); break; } } }); } }
fragement(因为3个都一样 所以这里发出第一个代码):
Layout_main;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; import com.example.user.twodome.R; /** * Created by user on 16-3-12. */ public class Fragmentone extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view=(View) inflater.inflate(R.layout.fragment_one,null); return view; } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/fl_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > </FrameLayout> <RadioGroup android:id="@+id/rg" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:background="#ddd" android:orientation="horizontal"> <RadioButton android:id="@+id/b1" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_gravity="center" android:layout_weight="1" android:button="@null" android:drawablePadding="6dp" android:gravity="center" android:text="主页" android:textColor="@drawable/zitiyanse"></RadioButton> <RadioButton android:id="@+id/b2" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_gravity="center" android:layout_weight="1" android:button="@null" android:drawablePadding="6dp" android:gravity="center" android:text="我的" android:textColor="@drawable/zitiyanse"></RadioButton> <RadioButton android:id="@+id/b3" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_gravity="center" android:layout_weight="1" android:button="@null" android:drawablePadding="6dp" android:gravity="center" android:text="购物车" android:textColor="@drawable/zitiyanse"></RadioButton> </RadioGroup> </RelativeLayout>
Layout_fragemen(发出一个,另外两个与这个差不多)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="qq"/> </RelativeLayout>
这个效果是用RadioGroup按钮组实现的,并且我没有定义默认显示第一个页面
第二种就是利用ViewPage来做
效果图就不上了 和第一个差不多 但是没有下面的按钮组了
//下面这些代码是借鉴的蓝魔魔大神的
1. Layout_Activity (fragement和第一种方法的布局一样)<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" ><android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="fill_parent" android:layout_height="wrap_content" > </android.support.v4.view.ViewPager></RelativeLayout>
2. 创建三个Fragment(和第一个方法里的一样)
3. 为ViewPager创建Adapter(1) 创建类继承FragmentPagerAdapter(2) 重写getItem(int index) 以及 getCount() 方法注意:自定义的构造函数一定要实现父类的构造函数 super(fm);public class MyPagerAdapter extends FragmentPagerAdapter {private List<Fragment> fragments = new ArrayList<Fragment>();
public MyPagerAdapter(FragmentManager fm){super(fm);}
public MyPagerAdapter(FragmentManager fragmentManager,ArrayList<Fragment> fragments){super(fragmentManager);this.fragments = fragments;}
@Overridepublic Fragment getItem(int index) {return fragments.get(index);}
@Overridepublic int getCount() {return fragments.size();}}
4. 在Activity中将之前创建的3个Fragment加入到ViewPager中ViewPager pager = null;ArrayList<Fragment> fragments = null;PagerAdapter adapter = null;
private void initFragment(){fragments.add(new PageOneFragment());fragments.add(new PageTwoFragment());fragments.add(new PageThreeFragment());}//实现之前写的adapter并赋给ViewPager即可private void initPager(){adapter = new MyPagerAdapter(getSupportFragmentManager(), fragments);pager.setAdapter(adapter);}
5. 至此,就已经实现了左右滑动切换页面的效果。
以上就是两种方法,希望对读者有些帮助。
ps:希望有大神看到我这篇文章时,能够告诉我怎么把这两种方法融合到一起。谢谢了。。。