fragment的退栈的实现

2020/10/30 15:00
阅读数 21

1.全部代码

3个fragment的代码:

public class BlankFragment extends Fragment {
   
   

    @Override
    public void onCreate(Bundle savedInstanceState) {
   
   
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
   
   
        TextView textView=new TextView(getActivity());
        textView.setText("1111111111111111");
        textView.setTextSize(50);
        textView.setTextColor(Color.parseColor("#FFFFFF"));
        textView.setBackgroundColor(R.color.colorAccent);
        return textView;
    }
}
public class BlankFragment2 extends Fragment {
   
   

    @Override
    public void onCreate(Bundle savedInstanceState) {
   
   
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
   
   
        TextView textView=new TextView(getActivity());
        textView.setText("2222222222222222222222222");
        textView.setBackgroundColor(R.color.colorPrimaryDark);
        return textView;
    }
}
public class BlankFragment3 extends Fragment {
   
   

    @Override
    public void onCreate(Bundle savedInstanceState) {
   
   
        super.onCreate(savedInstanceState);
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
   
   
        TextView textView=new TextView(getActivity());
        textView.setText("3333333333333333333333333333");
        textView.setBackgroundColor(R.color.colorPrimary);
        return textView;
    }
}

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/bt2"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/bt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"
        app:layout_constraintBottom_toBottomOf="@+id/bt_1"
        app:layout_constraintEnd_toStartOf="@+id/bt3"
        app:layout_constraintStart_toEndOf="@+id/bt_1"
        app:layout_constraintTop_toTopOf="@+id/bt_1" />

    <Button
        android:id="@+id/bt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3"
        app:layout_constraintBottom_toBottomOf="@+id/bt2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/bt2"
        app:layout_constraintTop_toTopOf="@+id/bt2" />

    <FrameLayout
        android:id="@+id/fl"
        android:layout_width="409dp"
        android:layout_height="665dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

主界面代码:

public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
   
   
    private static final String TAG = "MainActivity";
    private Button mBt1;
    private Button mBt2;
    private Button mBt3;
    private FrameLayout mFl;
    private BlankFragment blankFragment;
    private BlankFragment2 blankFragment2;
    private BlankFragment3 blankFragment3;
    private FragmentTransaction mFragmentTransaction;
    List<Fragment> list=new LinkedList<>();
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //1.添加全部fragment,默认只显示1
        blankFragment = new BlankFragment();
        blankFragment2 = new BlankFragment2();
        blankFragment3 = new BlankFragment3();
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = supportFragmentManager.beginTransaction();
        mFragmentTransaction
                .add(R.id.fl, blankFragment, "blankFragment")
                .add(R.id.fl, blankFragment2, "blankFragment2")
                .add(R.id.fl, blankFragment3, "blankFragment3")
                .hide(blankFragment2)
                .hide(blankFragment3).commit();



    }



    private void initView() {
   
   
        mBt1 = (Button) findViewById(R.id.bt_1);
        mBt2 = (Button) findViewById(R.id.bt2);
        mBt3 = (Button) findViewById(R.id.bt3);
        mFl = (FrameLayout) findViewById(R.id.fl);

        mBt1.setOnClickListener(this);
        mBt2.setOnClickListener(this);
        mBt3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
   
   
        //隐藏全部fragment
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction
                .hide(blankFragment)
                .hide(blankFragment2)
                .hide(blankFragment3)
        ;
        switch (v.getId()) {
   
   
            case R.id.bt_1:
                fragmentTransaction.show(blankFragment);
                setFragment(blankFragment);//如果有就设置到arrayList中
                break;
            case R.id.bt2:
                fragmentTransaction.show(blankFragment2);
                setFragment(blankFragment2);
                break;
            case R.id.bt3:
                fragmentTransaction.show(blankFragment3);
                setFragment(blankFragment3);
                break;
        }
        //吧当前事务添加到回退栈中
//        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
    private void setFragment(Fragment fragment) {
   
   
        //判断是否有这个fragment
        Log.d(TAG, "setFragment: list:"+list.size());
        if (list.contains(fragment)) {
   
   //有
            //移除,再添加
            list.remove(fragment);
        }
        list.add(fragment);


    }
    @Override
    public void onBackPressed() {
   
   
        Log.d(TAG, "onDestroy: list.size"+list.size());
        if (list.size()>1) {
   
   
            //移除,最顶部那一个
            list.remove(list.size()-1);
            //显示最顶部那一个
            showFragment(list.get(list.size()-1));
        }else {
   
   
            finish();
        }

    }

    private void showFragment(Fragment fragment) {
   
   
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();//得到经理,并开始移动
        fragmentTransaction
                .hide(blankFragment)
                .hide(blankFragment2)
                .hide(blankFragment3)
        ;
        fragmentTransaction.show(fragment);
        fragmentTransaction.commit();
    }
}

2.步骤

1.

//1.添加全部fragment,默认只显示1
        blankFragment = new BlankFragment();
        blankFragment2 = new BlankFragment2();
        blankFragment3 = new BlankFragment3();
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        mFragmentTransaction = supportFragmentManager.beginTransaction();
        mFragmentTransaction
                .add(R.id.fl, blankFragment, "blankFragment")//第三个参数表示标签
                .add(R.id.fl, blankFragment2, "blankFragment2")
                .add(R.id.fl, blankFragment3, "blankFragment3")
                .hide(blankFragment2)
                .hide(blankFragment3).commit();

2.显示那个fragment与添加到linkedList中

@Override
    public void onClick(View v) {
   
   
        //2.隐藏全部fragment
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction
                .hide(blankFragment)
                .hide(blankFragment2)
                .hide(blankFragment3)
        ;
        switch (v.getId()) {
   
   
            case R.id.bt_1:
                fragmentTransaction.show(blankFragment);
                setFragment(blankFragment);//如果有就设置到LInkedList中
                break;
            case R.id.bt2:
                fragmentTransaction.show(blankFragment2);
                setFragment(blankFragment2);
                break;
            case R.id.bt3:
                fragmentTransaction.show(blankFragment3);
                setFragment(blankFragment3);
                break;
        }
        fragmentTransaction.commit();
    }

3.fragment添加到LinkedList方法

private void addFragment(Fragment fragment) {
   
   
        //判断是否有这个fragment
        Log.d(TAG, "setFragment: list:"+list.size());
        if (list.contains(fragment)) {
   
   //有
            //移除,再添加
            list.remove(fragment);
        }
        list.add(fragment);


    }

4.重写返回键,记得删掉super

@Override
    public void onBackPressed() {
   
   
        Log.d(TAG, "onDestroy: list.size"+list.size());
        if (list.size()>1) {
   
   
            //移除,最顶部那一个
            list.remove(list.size()-1);//size已经减一了
            //显示最顶部那一个
            showFragment(list.get(list.size()-1));
        }else {
   
   
            finish();
        }

    }

5.回退后,实现上一个fragment的展示

private void showFragment(Fragment fragment) {
   
   
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();//得到经理,并开始移动
        fragmentTransaction
                .hide(blankFragment)
                .hide(blankFragment2)
                .hide(blankFragment3)
        ;
        fragmentTransaction.show(fragment);
        fragmentTransaction.commit();
    }

3.问题:

  1. linkedList的增删有优势相对于arraylist
  2. list列表移除后,size会直接减一
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部