【发布时间】:2017-08-01 07:17:55
【问题描述】:
在 Fragment1 我有 onClick 方法,当我单击按钮时,我得到了 recycleview 项目列表。当我单击 recycleview 中的项目时(使用 getPlaceFromItem 方法),我转到 Fragment2。如果设备是电话然后把 容器 1 中的片段 2,但如果设备是平板电脑(横向),那么我将片段 2 放入容器 1 中片段 1 旁边的容器 2 中。
现在,当我在设备是电话时按返回按钮时,我得到空的回收视图。
此外,我检查屏幕的方向,当设备是手机并且我在 Fragment2 中(即容器 1 中的 Fragment2)时,当屏幕为横向时,我将放入容器 1 片段 1,并放入片段 2 的容器 2。我的问题是如何从 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) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
View v = findViewById(R.id.container2);
v.setVisibility(View.VISIBLE);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container1, new Fragment1())
// The next row is a problematic!!!
.replace(R.id.container2, new Fragment2())
.commit();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
}
}
【问题讨论】:
-
resyclerview 是否在同一个容器片段上?
-
是的,在同一个容器片段上。
-
您能否添加您的 Fragment
onCreateView和onResume方法的代码? -
好的,我稍后会添加代码。谢谢
-
我添加了更多代码,请看。
标签: android android-fragments android-recyclerview