gpt4 book ai didi

android - 从 fragment 返回时,recyclerview 为空

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:29:34 24 4
gpt4 key购买 nike

在 Fragment1 上我有 onClick 方法,当我点击按钮时,我得到了项目的回收 View 列表。当我单击 recycleview 中的项目(使用 getPlaceFromItem 方法)时,我转到 Fragment2。如果设备是电话然后把container1 中的 Fragment2,但如果设备是平板电脑(横向),那么我将 Fragment2 放入 container1 中 Fragment1 旁边的 container2。

现在,当我在设备是电话的情况下按下后退按钮时,我得到空的回收 View 。

此外,我检查屏幕的方向,当设备是手机并且我在 Fragment2 中(即 container1 中的 Fragment2)时,当屏幕为横向时,我将放入 container1 Fragment1,并放入 container2 中的 Fragment2。我的问题是如何从 container1 中剪切 Fragment2 并将其放入 container2。

主要 Activity :

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){
}
}

最佳答案

请记住,当 fragment 从返回堆栈中弹出时,它返回时没有 View 。所以你必须再次附加 View 。

我建议您跟踪 Fragment 中的主视图,以避免重新创建 View 。这样,一旦您返回带有 RecyclerView 的 fragment ,它就会在那里。代码如下所示。

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;
}

关于android - 从 fragment 返回时,recyclerview 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45431311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com