简体   繁体   English

如何重用其他片段类中的常见片段视图?

[英]How to reuse common fragment views in other fragment class?

I created a GridView menu in my FragmentA class and also I created a separate Common_Fragment class which is contain two ImageView. 我在FragmentA类中创建了GridView菜单,还创建了一个单独的Common_Fragment类,其中包含两个ImageView。 Now I want to reuse those Common_Fragment views in my FragmentA class and other Fragment. 现在,我想重用我的FragmentA类和其他Fragment中的Common_Fragment视图。 How can I do this? 我怎样才能做到这一点?

 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> 

 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> 

I used this code in FragmentB 我在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(); } } 

Create a container in the layout xml of FragmentA and load the common_fragment into that FragmentA . FragmentA的布局xml中创建一个容器,并将common_fragment加载到该FragmentA

In fragment_a.xml 在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"
        .........
 />

In the FragmentA.java 在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.  
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM