Ira*_*Ira 5 android android-fragments recycler-adapter android-recyclerview
在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) {
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){
}
}
Run Code Online (Sandbox Code Playgroud)
小智 6
记住,当片段从后堆栈弹出时,它不会返回而没有视图。因此,您必须再次附加视图。
我建议您跟踪Fragment中的主视图,以免重新创建视图。这样,一旦您使用RecyclerView返回片段,它就会在那里。代码看起来像这样。
public class BlankFragment2 extends Fragment {
public View myRoot;
public BlankFragment2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if (myRoot == null) {
myRoot = inflater.inflate(R.layout.fragment_blank_fragment2, container, false);
}
return myRoot;
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
2710 次 |
| 最近记录: |