想到啥写啥第一期:致力于更简单易懂的代码解决我们安卓小白遇到的问题
这次来个稍微难一点的,用fragmen套住ViewPager,ViewPager里面套fragment
听着很绕,但是我们需要把ViewPager看作是一个UI,就是一个工具而已,而fragment看作一个工具箱
这样的话就是把工具放进工具箱,然后打开工具箱就行了–>就是把ViewPager 的代码放进fragmen 的UI界面去显示
话不多说,客官,请看代码:
把activity里面的ViewPager写熟练之后照这个样子写入fragment
fragment1的UI:(和上次一样的,只不过单独拿出来给fragment1)
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPage"
android:background="#fff"
android:layout_width="match_parent"
android:layout_height="400sp"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="50sp">
<RadioButton
android:id="@+id/rb1"
android:button="@null"
android:background="@drawable/bg"
android:layout_width="30sp"
android:layout_height="30sp"
android:layout_marginEnd="20sp"/>
<RadioButton
android:id="@+id/rb2"
android:button="@null"
android:background="@drawable/bg"
android:layout_width="30sp"
android:layout_height="30sp"
android:layout_marginStart="20sp"
android:layout_marginEnd="20sp"/>
<RadioButton
android:id="@+id/rb3"
android:button="@null"
android:background="@drawable/bg"
android:layout_width="30sp"
android:layout_height="30sp"
android:layout_marginStart="20sp"
android:layout_marginEnd="20sp"/>
</RadioGroup>
fragment1.java:
public class Frag1 extends Fragment {
private ViewPager viewPager;
private List<Fragment> fragments = new ArrayList<>();
private ViewPageAdapter adapter;
private RadioGroup radioGroup;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//这里的view这里的inflater....emmmm以后会讲解,暂时就这么写吧,我想到啥就补充啥
//意思就是获取到frag1这个界面
View view = inflater.inflate(R.layout.frag1,container,false);
//绑定的话就是从这个界面去获取组件id
viewPager = view.findViewById(R.id.viewPage);
radioGroup = view.findViewById(R.id.radioGroup);
fragments.add(new MyFrag("测试碎片1", Color.BLUE));
fragments.add(new MyFrag("测试碎片2", Color.RED));
fragments.add(new MyFrag("测试碎片3", Color.YELLOW));
/*getChildFragmentManager 是fragment 包含 fragment ---> getFragmentManager是activity包含fragment */
//一开始我写的是getFragmentManager,产生了ViewPager在切换界面之后有些东西就不显示了,反正,这里应该是用child的吧!(瓦不管)
adapter = new ViewPageAdapter(getChildFragmentManager(),fragments);
viewPager.setAdapter(adapter);
return view;
}
@Override
public void onResume() {
super.onResume();
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.rb1);
break;
case 1:
radioGroup.check(R.id.rb2);
break;
case 2:
radioGroup.check(R.id.rb3);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rb1:
viewPager.setCurrentItem(0);
break;
case R.id.rb2:
viewPager.setCurrentItem(1);
break;
case R.id.rb3:
viewPager.setCurrentItem(2);
break;
}
}
});
radioGroup.check(R.id.rb1);
}
}
fragment2,fragment3都是和这个frag1差不多的意思,照着写就完事儿了
那么,viewPager放入fragment的第一步就做好了
接下来就是把fragment放入activity的frameLayou里面
activity.xml:
//按钮的背景选择器就按照之前的shape,selector去写就行,不多赘述
<RadioGroup
android:id="@+id/group"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="50sp"
android:layout_marginBottom="20sp">
<RadioButton
android:id="@+id/rb_1"
android:button="@null"
android:background="@drawable/bg_rect"
android:layout_width="70sp"
android:text="1"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_height="40sp"
android:layout_marginEnd="20sp"/>
<RadioButton
android:id="@+id/rb_2"
android:button="@null"
android:background="@drawable/bg_rect"
android:layout_width="70sp"
android:layout_height="40sp"
android:text="2"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="20sp"
android:layout_marginEnd="20sp"/>
<RadioButton
android:id="@+id/rb_3"
android:button="@null"
android:background="@drawable/bg_rect"
android:layout_width="70sp"
android:layout_height="40sp"
android:text="3"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginStart="20sp"
android:layout_marginEnd="20sp"/>
</RadioGroup>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="500sp"/>
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private List<Fragment> list = new ArrayList<>();
private RadioGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
group = findViewById(R.id.group);
list.add(new Frag1());
list.add(new Frag2());
list.add(new Frag3());
}
@Override
protected void onResume() {
super.onResume();
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.rb_1:
replaceFrag(list.get(0));
Log.i("temp","00000");
break;
case R.id.rb_2:
replaceFrag(list.get(1));
break;
case R.id.rb_3:
replaceFrag(list.get(2));
break;
}
}
});
group.check(R.id.rb_1);
}
//这里封装一个方法,用于切换碎片fragment
//transaction只能提交一次,所以设成局部变量不容易出错
private void replaceFrag(Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frame,fragment).commit();
}
}
ps:贴上上面ViewPager里面的MyFrag类和它的适配器ViewPageAdapter
MyFrag:
public class MyFrag extends Fragment {
String text ;
int background;
public MyFrag(String text,int background) {
this.text = text;
this.background = background;
}
private TextView textView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myfrag,container,false);
textView = view.findViewById(R.id.text);
textView.setText(text);
view.setBackgroundColor(background);
return view;
}
}
ViewPageAdapter:
public class ViewPageAdapter extends FragmentPagerAdapter {
FragmentManager manager;
List<Fragment> fragmentList;
public ViewPageAdapter(@NonNull FragmentManager fm, List<Fragment> fragmentList) {
super(fm);
this.manager = fm;
this.fragmentList = fragmentList;
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
、、、祝愿每个程序员都有一顶秀发(彩虹屁拍完,完工)