簡體   English   中英

從片段反向按下時,recyclerview為空

[英]recyclerview is empty when back pressed from fragment

在Fragment1上我有onClick方法,當我點擊按鈕時,我得到了recycleview項目列表。 當我單擊recycleview中的項目(使用getPlaceFromItem方法)時,我轉到Fragment2。 如果設備是手機然后將Fragment2放入container1,但如果設備是平板電腦(橫向),那么我將Fragment2放入container1中Fragment1旁邊的container2。

現在,當我在設備是電話時按下后退按鈕,我得到空的recycleview。

此外,我檢查屏幕的方向,當設備是電話,我在Fragment2(即容器1中的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){
 }      
    }

請記住,當一個片段從后面的堆棧中彈出時,它會返回而沒有任何視圖。 所以你必須再次附加視圖。

我建議您跟蹤片段中的主視圖,以避免重新創建視圖。 這樣一旦你回到使用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;
}

}

.addToBackStack(null)從代碼中刪除它

我希望它對你有用

添加.addToBackStack(fragment.getClass().getCanonicalName()).addToBackStack("anyString")來保存片段實例。 還可以使用add方法,而不是在添加新片段時replace

當您添加第一個片段時,不應使用addToBackStack(null)。

對於第一個片段寫這樣的

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

對於你應該使用的其他片段

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

希望能幫助到你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM