Android 更改片段视图

Android 更改片段视图,android,android-fragments,android-fragmentactivity,Android,Android Fragments,Android Fragmentactivity,我有一个带有碎片的应用程序。它很好用 其中一个片段有一个按钮,按下时我想更改视图 public class BikeRecap extends Fragment { public static Activity activity; public static Context context; /** Called when the activity is first created. */ public View onCreateView(LayoutInflater inflater

我有一个带有碎片的应用程序。它很好用

其中一个片段有一个按钮,按下时我想更改视图

public class BikeRecap extends Fragment {

public static Activity activity;
public static Context context;


/** Called when the activity is first created. */


public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    activity = getActivity();
    context = activity.getApplicationContext();

    View view = inflater.inflate(R.layout.bike_recap, container, false);        

    ImageButton details = (ImageButton) item.findViewById(R.id.details);


    details.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              OpenNewView();
          }
    });

    return view;
}
OpenNewView()应该更改视图,因此,用户位于同一选项卡中,但内容不同


有可能吗?

片段中不能有片段

请尝试以下代码:

FragmentManager fragmentManager2 = getFragmentManager();
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
DetailFragment fragment2 = new DetailFragment();
fragmentTransaction2.addToBackStack("abc");
fragmentTransaction2.hide(BikeRecap.this);
fragmentTransaction2.add(android.R.id.content, fragment2);
fragmentTransaction2.commit();

我认为你可以很容易地做你想做的事情。如果OpenNewView由活动而不是片段实现:

getActivity().openNewView(…)

。。。然后,活动可以使用标准事务机制轻松地将当前片段替换为新片段

布莱克·梅克 马拉卡纳

Android 2ed的编程功能现已上市:

我不想在片段中包含片段,我想更改片段显示的内容。实际上,从Android 4.2开始,您可以拥有子片段,并且这也可以通过Android支持库进行后端口。@cocoahero,真的吗?我不知道,你知道API演示中是否有示例吗?@Marckaraujo不确定是否有示例代码,但你所要做的就是从片段内部调用getChildFragmentManager();什么是碎片?我找不到它,它是一些包裹吗?很抱歉,但我不明白你的意思,我问你怎么做,你告诉我,“使用标准交易”?