Fragment感觉有点像VUE的组件,今天研究了一下。成果如下
新建两个 Fragment 文件 并设置对应的布局
public class BlankFragment1 extends Fragment {
private String TAG = BlankFragment1.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_blank1, container, false);
TextView f1_id = view.findViewById(R.id.f1_id);
Bundle bundle = getArguments();
String Id = "";
if(bundle!=null)
{
Id = bundle.getString("Id");
}
f1_id.setText(Id);
return view;
}
}
布局文件
<LinearLayout 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"
android:orientation="horizontal"
tools:context="com.yysoft.Frames.BlankFragment1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="18sp"
android:id="@+id/f1_id"
android:text="farm1" />
</LinearLayout>
新建一个Activity文件,做为启动界面
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/top1"
android:orientation="horizontal"
>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:text="第一个页面11"
android:gravity="center"
android:id="@+id/tv1"
>
</TextView>
<View android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/black"
></View>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:text="第二个页面22"
android:gravity="center"
android:id="@+id/tv2"
>
</TextView>
</LinearLayout>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:id="@+id/vv1"
android:layout_below="@id/top1"
android:background="@color/black"
></View>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fv1"
android:layout_below="@id/vv1"></FrameLayout>
</RelativeLayout>
主文件
private TextView tv1;
private TextView tv2;
private Fragment curFrageMent;
private String TAG = MainActivity2.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv1 = findViewById(R.id.tv1);
tv2 = findViewById(R.id.tv2);
tv1.setOnClickListener(this);
tv2.setOnClickListener(this);
showFm(1);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.tv1:
showFm(1);
break;
case R.id.tv2:
showFm(2);
break;
default:break;
}
}
private void showFm(int i){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
//传值用的
Bundle bundle = new Bundle();
switch (i)
{
case 1:
curFrageMent = new BlankFragment1();
bundle.putString("Id","123456");
break;
case 2:
curFrageMent = new BlankFragment2();
bundle.putString("Id","654321");
break;
}
Log.i(TAG, "showFm: "+curFrageMent.getClass().getSimpleName());
if(curFrageMent!=null) {
curFrageMent.setArguments(bundle); //传值
transaction.replace(R.id.fv1, curFrageMent);
transaction.commit();
}
}
另外,组件之间传值时,可以通过activity传,比如在第二个碎片化里面写上方法,在第一个中调用时可以使用。
private void setContent(String title, String content) {
ContentFragment contentFragment = (ContentFragment)getActivity().getSupportFragmentManager().findFragmentById(R.id.fra_content);
contentFragment.setData(title,content);
}