2016-12-22 2 views
0

フラグメントにListView/RecyclerViewが含まれる2つのフラグメントでネストされたフラグメントビューを作成しようとしていますが、2番目のリストビューデータは最初のリストビューアイテムに依存します。アンドロイドで入れ子になったフラグメントビューを作成する方法は?

これは、電子商取引アプリケーションの商品フィルタ画面に似ています。

以下のコードを見てください:

public class TestActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test); 
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
    FilterFragment filterFragment = new FilterFragment(); 
    ft.replace(R.id.filter_fragment_container, filterFragment); 
    ft.commit(); 
} 

}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_test" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.splice.TestActivity"> 
<FrameLayout 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</FrameLayout> 

activity_test.xml活動のための

レイアウトファイル:親の断片を保持している

TestActivity.javaを

親フラグメントクラスFilterFragment.java

public class FilterFragment extends Fragment{ 


public FilterFragment() { 
    // Required empty public constructor 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View v = inflater.inflate(R.layout.fragment_filter, container, false); 
    return v; 
} 
@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    insertNestedFragment(); 
} 

// Embeds the child fragment dynamically 
private void insertNestedFragment() { 
    Fragment childFragment = new TestFragment1(); 
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction(); 
    transaction.replace(R.id.fragment1_container, childFragment).commit(); 
} 

}

親フラグメントレイアウトファイルfragment_filter.xml

<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="vertical" 
android:layout_margin="@dimen/activity_horizontal_margin" 
tools:context="com.splice.views.FilterFragment"> 

<!-- TODO: Update blank fragment layout --> 
<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center_horizontal" 
    android:text="@string/filter_purchase_title" /> 

<FrameLayout 
android:layout_width="match_parent" 
android:id="@+id/fragment1_container" 
android:layout_height="wrap_content"> 
</FrameLayout> 

子フラグメントクラスTestFragment1.java

public class TestFragment1 extends Fragment { 


public TestFragment1() { 
    // 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_test_fragment1, container, false); 

} 

}

結果では、子フラグメントの内容ではなく、親フラグメントの内容を表示しています。

は私が enter image description here

のように気にいらを実装したい私はこれを実装するために間違ったapprochためつもりなら、私を修正してください。

ありがとうございます。

答えて

1

私はあなたが提示したコードにいくつかの問題を発見:

  1. ft.replace(R.id.filter_fragment_container、filterFragment)。 のactivity_testレイアウトはfragment_containerと呼ばれます。私は確信していません これはタイプミスでしたが、ちょうど
  2. がfragment_filter.xmlにあり、LinearLayoutの向きが垂直で、TextViewにアンドロイド:layout_height = "match_parent"がある場合は、 次の要素、画面の外に出ます。だから、おそらく、 あなたはその断片を見ません。分割されたレイアウトを持つために

を左右に、あなたはそれが好きで作ることができます。

<LinearLayout ... 
android:orientation="horizontal" 
android:weightSum="2"> 

<View1 
... 
android:layout_width="0dp" 
android:layout_weight="1" 
/> 
<View2 
... 
android:layout_width="0dp" 
android:layout_weight="1" 
/> 
</LinearLayout> 
関連する問題