所以我有两个MAIN ACTIVITY(M)
和FRAGMENTS(F1, F2)
一个BUTTON(B)
。F1
现在,当主要活动启动时F1
会自动加载这很好,我想做的是F2
在单击B
which is on时启动F1
。
以下是我的 Fragment1 代码,即 HomeFragment:
package info.androidhive.slidingmenu;
import java.lang.reflect.Field;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class HomeFragment extends Fragment {
public static HomeFragment newInstance()
{
return new HomeFragment();
}
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
ImageButton ib = (ImageButton) rootView.findViewById(R.id.buttonEnq);
ib.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
InfoFragment infoFragment = new InfoFragment();
/*FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.frame_container, infoFragment).commit();
*/
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.abc_slide_out_bottom, 0);
ft.replace(R.id.frame_container,infoFragment);
ft.commit();
}
});
ViewPager viewPager = (ViewPager)rootView.findViewById(R.id.myfivepanelpager);
ImageAdapter adapter1 = new ImageAdapter(this.getActivity());
viewPager.setAdapter(adapter1);
return rootView;
}
@Override
public void onDetach(){
super.onDetach();
try{
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this,null);
}catch(NoSuchFieldException e){
throw new RuntimeException(e);
}catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
frame_container
是 中容器的名称main_activity.xml
。我试过以下帖子:
你能告诉我我做错了什么吗?