package com.example.mootaodemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import com.example.mootaodemo.fragment.FindFragment;
import com.example.mootaodemo.fragment.IndexFragment;
import com.example.mootaodemo.fragment.MeFragment;
public class MainActivity extends AppCompatActivity {
private Fragment fragment1;
private Fragment fragment2;
private Fragment fragment3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragment1=new FindFragment();
fragment2=new IndexFragment();
fragment3=new MeFragment();
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container,fragment2);
transaction.commit();
}
public void myclick(View v){
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
switch (v.getId()){
case R.id.index:
transaction.replace(R.id.container,fragment2);
break;
case R.id.find:
transaction.replace(R.id.container,fragment1);
break;
case R.id.me:
transaction.replace(R.id.container,fragment3);
break;
}
transaction.commit();
}
}
就是这种提前的实例化并切换,在不断点击下方的导航栏不断来回切换fragment时,会出现Gridview不断重载,直到崩溃
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星