在Fragment1上,我具有onClick方法,当我单击按钮时,我得到了项目的recycleview列表。当我单击recycleview中的项目时(使用getPlaceFromItem方法),我转到Fragment2。如果设备是电话,则将Fragment2放在container1中,但是如果设备是tablet(横向),则将Fragment2放入container2中,在container1中Fragment1旁边。
现在,当设备为手机时,当我按返回按钮时,我会得到空的recycleview。
另外,我检查屏幕的方向,以及当设备为手机且我在Fragment2中(即container1中的Fragment2)时,然后在屏幕为横向时,我将放入container1 Fragment1中,并放入Fragment2中的container2中。我的问题是如何从Container1剪切Fragment2并将其放入container2。
主要活动 :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
View v = findViewById(R.id.container2);
v.setVisibility(View.GONE);
getSupportFragmentManager().beginTransaction()
.add(R.id.container1, new FragmentA())
.commit();
}
@Override
public void getPlaceFromItem(Place place) {
Bundle bundle = new Bundle();
bundle.putDouble("lat", place.getLat());
bundle.putDouble("lng", place.getLng());
Fragment2 fragment2 = new Fragment2();
fragment2.setArguments(bundle);
if(getResources().getBoolean(R.bool.isTab)) {
View v = findViewById(R.id.container2);
v.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container2,fragment2)
.addToBackStack(null)
.commit();
}
else {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container1, fragment2)
.addToBackStack(null)
.commit();
}
}
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) …Run Code Online (Sandbox Code Playgroud) android android-fragments recycler-adapter android-recyclerview