点击单选按钮进入不同界面
此处设置为两个按钮:按钮1 按钮2
主页面布局
<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="13dp"
android:text="方式" />
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/rb1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:button="@null"
android:textSize="15dp"
android:drawableRight="@android:drawable/btn_radio"
android:text="上滑"/>
<RadioButton
android:id="@+id/rb2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:button="@null"
android:textSize="15dp"
android:drawableRight="@android:drawable/btn_radio"
android:text="虚拟"/>
</RadioGroup>
<FrameLayout
android:id="@+id/mFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
另创建两个layout1.xml、layout2.xml用来放置两个fragment布局,并创建相应的java文件:
package com.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.airbnb.lottie.LottieAnimationView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view= inflater.inflate(R.layout.layout1,container,false);
return view;
}
public Fragment1(){
}
public static Fragment1 newInstance(){
Fragment1 fragment1=new Fragment1();
return fragment1;
}
}
在该文件中加载布局。
最后在主函数中添加
package yangziyu.com.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
private RadioGroup mRadioGroup;
private List<Fragment> fragments =new ArrayList<>();
private Fragment fragment;
private FragmentManager fm;
private FragmentTransaction transaction;
private RadioButton rb1,rb2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
mRadioGroup.setOnCheckedChangeListener(this);
fragments=getFragments();
normalFragment();
}
private void normalFragment(){
fm=getSupportFragmentManager();
transaction=fm.beginTransaction();
fragment=fragments.get(0);
transaction.replace(R.id.mFragment,fragment);
transaction.commit();
}
private void initView() {
mRadioGroup = (RadioGroup) findViewById(R.id.rg);
rb1= (RadioButton) findViewById(R.id.rb1);
rb2= (RadioButton) findViewById(R.id.rb2);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
fm=getSupportFragmentManager();
transaction=fm.beginTransaction();
switch (checkedId){
case R.id.rb1:
fragment=fragments.get(0);
transaction.replace(R.id.mFragment,fragment);
break;
case R.id.rb2:
fragment=fragments.get(1);
transaction.replace(R.id.mFragment,fragment);
break;
}
transaction.commit();
}
public List<Fragment> getFragments() {
fragments.add(new Fragment1());
fragments.add(new Fragment2());
return fragments;
}
}
问题1:onCreateView要重写,如果拼写错误,可能不需要重写,那么就加载不出来页面。Fragment2相同操作。
问题2:
可控制按钮在右边
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:drawableLeft="@mipmap/ic_launcher"
控制按钮的图标显示位置及资源添加
问题3:
在Fragement中加载view时不能直接finViewById,使用方法为先创建View,再去寻找控件
View view= inflater.inflate(R.layout.layout1,container,false);
LottieAnimationView lottieLike = (LottieAnimationView)view.findViewById(R.id.lottie_likeanim);