底栏片段重新加载

时间:2017-06-29 09:51:06

标签: android android-fragments android-viewpager reload bottombar

这些是我的主要活动和我的片段。每次我在底栏视图的选项卡上返回时,我都不想重新加载片段。事实上现在每次我点击该选项卡时图像都会重新加载。 有解决方案吗

主要活动

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    BottomNavigationView bottomNavigationView = (BottomNavigationView)
            findViewById(R.id.navigation);

    bottomNavigationView.setOnNavigationItemSelectedListener
            (new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    Fragment selectedFragment = null;

                    switch (item.getItemId()) {
                        case R.id.action_item1:

                                selectedFragment = Tab1.newInstance();

                            break;
                        case R.id.action_item2:
                            selectedFragment = Tab2.newInstance();
                            break;
                        case R.id.action_item3:
                            selectedFragment = Tab3.newInstance();
                            break;
                    }
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                    transaction.replace(R.id.frame_layout, selectedFragment);
                    transaction.commit();
                    return true;
                }
            });

    //Manually displaying the first fragment - one time only
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.frame_layout, Tab1.newInstance());
    transaction.commit();

    //Used to select an item programmatically
    //bottomNavigationView.getMenu().getItem(2).setChecked(true);
}
}

片段

public class Tab1 extends Fragment {
ImageView imageview;
ProgressDialog progressDialog;
public static Tab1 newInstance() {
    Tab1 fragment = new Tab1();
    return fragment;
}

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

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView =  inflater.inflate(R.layout.activity_tab1, container, false);

    imageview =(ImageView)  rootView.findViewById(R.id.immagine);
    FirebaseStorage storage = FirebaseStorage.getInstance();
    StorageReference storageRef = storage.getReferenceFromUrl("gs://test-x3b2c.appspot.com").child("lc.jpg");

    //download file as a byte array

    progressDialog = ProgressDialog.show(getActivity(), "Attendi",
            "Sto caricando il luogo comune", true);
    try {
        final File localFile = File.createTempFile("images", "jpg");
        storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                Bitmap bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
                imageview.setImageBitmap(bitmap);
                progressDialog.dismiss();

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
            }
        });
    }
    catch (IOException e) {
    }
    return rootView;
}
}

2 个答案:

答案 0 :(得分:1)

像这样修改你的代码:

    switch (item.getItemId()) {
                    case R.id.action_item1:
                        selectedFragment = Tab1.newInstance();
                        break;
                    case R.id.action_item2:
                        selectedFragment = Tab2.newInstance();
                        break;
                    case R.id.action_item3:
                        selectedFragment = Tab3.newInstance();
                        break;
                }

                if(selectedFragment ! =null) {
                 FragmentTransaction transaction = 
                      getSupportFragmentManager().beginTransaction();
                 transaction.replace(R.id.frame_layout, selectedFragment, selectedFragment.getTag());
                 transaction.commit();
                }

                return true;

    }

Fragment标签可以避免再次重新创建片段(如果它已经存在)。

答案 1 :(得分:1)

replace将导致片段的生命周期重新启动。您可以使用addshowhide的组合。

请在此处查看我的答案以获取示例:https://stackoverflow.com/a/45595029/1102687