2013-01-01 80 views
2

我有一个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; 
} 

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

+0

你说你有一个包含'BaseFragments'一个'ViewPager'但你的活动的内容是一个简单的'带有'BaseFragment'的LinearLayout'。你可以解释吗? – Luksprog

+0

是的,正如我所说的这是示例应用程序只是1个BaseFragment和1个嵌套片段,其中只有1个视图。如果解决了这个问题,我会转向我原来的任务。但至于现在请帮助我纠正此示例应用程序,它遵循此层次结构 MainActivity - > BaseFragment - > NestedFragment - > ImageView –

回答

0

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