【问题标题】:recyclerview is empty when back pressed from fragment从片段返回时,recyclerview 为空
【发布时间】: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 onCreateViewonResume 方法的代码?
  • 好的,我稍后会添加代码。谢谢
  • 我添加了更多代码,请看。

标签: android android-fragments android-recyclerview


【解决方案1】:

请记住,当片段从返回堆栈中弹出时,它会返回时没有视图。所以你必须再次附加视图。

我建议您在 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;
}

}

【讨论】:

  • 我稍后会检查并告诉你发生了什么。谢谢
  • 一分钟之前我放了更多代码,你能看看吗。
  • 对不起,这是一个全新的问题和代码,我认为您应该先验证最后一个问题,然后添加一个新问题。
  • 我今天会尝试你的想法,然后返回结果。
  • 我检查了代码,我的行与您建议的相同,但没有帮助。
【解决方案2】:

.addToBackStack(null) 从代码中删除它

希望对你有用

【讨论】:

  • 我建议在 onResume() 或 onPause() 中重新填写 recyclerview
  • 什么意思?可以举个例子吗?
  • 我添加了更多代码和解释。你能看看吗?
【解决方案3】:

添加.addToBackStack(fragment.getClass().getCanonicalName()).addToBackStack("anyString") 以保存片段实例。添加新片段时也使用add 方法,而不是replace

【讨论】:

  • 你用过add吗? replace 将删除您的最后一个片段添加新片段。 add 只会添加新的。
  • 我稍后会添加更多代码。在使用replace中,因为container1中还有其他fragment。
  • 我添加了更多代码和解释。你能看看吗?
【解决方案4】:

当你添加你的第一个片段时,你不应该使用 addToBackStack(null)。

对于第一个片段这样写

 getSupportFragmentManager().beginTransaction()
    .add(R.id.container1, FragmentA)
    .commit();

对于您应该使用的其他片段

getSupportFragmentManager().beginTransaction()
    .replace(R.id.container1, FragmentB)
    .addToBackStack(null)
    .commit();

希望对你有帮助!

【讨论】:

  • 在一分钟之前我添加了更多代码和解释。你能看看吗?
  • 没用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多