我在FragmentA类中创建了一个GridView菜单,并且我创建了一个单独的Common_Fragment类,它包含两个ImageView。现在我想在FragmentA类和其他Fragment中重用那些Common_Fragment视图。我怎么能这样做?
My FragmentA class
public class FragmentA extends Fragment {
public FragmentA() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_a, container, false);
CommonFragment fragment = new CommonFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.dynamicFragmentLayout, fragment);
transaction.commit();
getFragmentManager().beginTransaction().replace(R.id.dynamicFragmentLayout, new Fragment()).commit();
return view;
}
}

Common_Fragment Class
public class CommonFragment extends Fragment {
ImageView imageView1,imageview2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.common_fragment,
container, false);
imageView1 = (ImageView) view.findViewById(R.id.imageView1);
imageView2 = (ImageView) view.findViewById(R.id.imageView2);
return view;
}
}

fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dynamicFragmentLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
android:layout_marginTop="20dp" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
</LinearLayout>
&#13;
common_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical"
android:id="@+id/dynamicFragmentLayout" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
>
<ImageButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
&#13;
我在FragmentB中使用了这段代码
This is my FragmentB Class
public class FragmentB extends Fragment {
public FragmentB() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_b, container, false);
CommonFragment fragment = new CommonFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.dynamicFragmentLayout, fragment);
transaction.commit();
getFragmentManager().beginTransaction().replace(R.id.container,new CommonFragment()).commit();
}
}
&#13;
答案 0 :(得分:0)
在FragmentA
的布局xml中创建一个容器,并将common_fragment
加载到FragmentA
。
在fragment_a.xml中
<FrameLayout
android:id="@+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></FrameLayout>
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
.........
/>
在FragmentA.java
中@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_a, container, false);
//Add the fragment
getChildFragmentManager().beginTransaction().add(new CommonFragment(),R.id.container).commit();
//Your code.
}