嵌套片段中的自定义适配器

时间:2013-01-01 10:54:05

标签: android android-fragments

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

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

以下是示例应用程序的代码

main.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;

}


}

nested_fragment.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>

NestedFragment.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;
}

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

1 个答案:

答案 0 :(得分:0)

从上面的代码中,您没有在base_fragment.xml .So add the frame layout for nested fragment in base_fragment.xml

中使用片段容器布局,例如FrameLayout