2

我有一个ViewPager包含多个基本片段,每个基本片段还有四个嵌套片段,每个嵌套片段是 5imageViewstextView. (这是我打算做的)

我已经创建了一个示例应用程序,但我无法正确实现它。我在嵌套片段或基本片段中看不到任何视图。

这是示例应用程序的代码

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
    >

<fragment android:name="com.example.nestedfragments.BaseFragment"
          android:id="@+id/left_fragment"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="match_parent"/>
</LinearLayout>

MainActivity.java

package com.example.nestedfragments;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}

base_fragment.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">

<Button
        android:id="@+id/button1"
        android:text="Launch Nested Fragment"
        android:gravity="center"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:paddingBottom="10dp"/>

</RelativeLayout>

BaseFragment.java

public class BaseFragment extends Fragment {

Button doNestingButton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // set the view
    View root = inflater.inflate(R.layout.base_fragment, container, false);



    doNestingButton = (Button) root.findViewById(R.id.button1);

    doNestingButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment videoFragment = new NestedFragment();
            // we get the 'childFragmentManager' for our transaction
            FragmentTransaction transaction =  getChildFragmentManager().beginTransaction();
            // make the back button return to the main screen
            // and supply the tag 'left' to the backstack
            transaction.addToBackStack("left");
            // add our new nested fragment
            transaction.add(getId(), videoFragment, "left");
            // commit the transaction
            transaction.commit();
        }
    });
    return root;

}


}

嵌套片段.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">


<ImageView
        android:layout_width="154dp"
        android:layout_height="154dp"
        android:id="@+id/imageView" android:layout_gravity="left|center_vertical"
        android:src="@drawable/ic_splash"/>
</LinearLayout>

嵌套片段.java

public class NestedFragment extends Fragment {

public NestedFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.nested_fragment, container, false);
    ImageView doNestingButton = (ImageView) root.findViewById(R.id.imageView);

    return root;
}

这又是一个示例应用程序,请指导我。

4

1 回答 1

0

从您上面的代码中,您还没有使用base_fragment.xml .So add the frame layout for nested fragment inbase_fragment.xml中的 FrameLayout 等片段容器布局

于 2013-07-31T13:24:39.013 回答