简体   繁体   English

单击将片段更改为另一个按钮时,片段不会进行交易

[英]fragment wont transaction when click on the button which change the fragment to another

i have a 2 fragments with Bottom Navigation View when i click on the items to change the fragments nothing happens and its still show the default fragment 当我单击项目以更改片段时,我有2个带有底部导航视图的片段,但没有任何反应,它仍然显示默认片段

Here the code 这里的代码

MainActivity.class MainActivity.class

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.setSelectedItemId(R.id.home);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){

                    case R.id.community:
                        //onClick on Community Button
                        Alert();
                        break;
                    case R.id.home:
                        //OnClick on Home Button
                       goHome();

                        break;

                    case R.id.setting:
                        //OnClick on Setting Button
                       goSetting();
                        break;
                }
                return true;

            }
        });
    }
    //THIS Methods to Transaction between Fragments
    private void  goHome(){
        HomeF home = new HomeF();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragmentlayout,home);
    }
    private void goSetting(){
        SettingF setting = new SettingF();
        FragmentManager fa = getFragmentManager();
        FragmentTransaction fs = fa.beginTransaction();
        fs.replace(R.id.fragmentlayout,setting);
    }
    private void Alert(){
        AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
        alert.setTitle("Attention");
        alert.setMessage("This section is Under Development stay tuned for the Biggest Openning");
        alert.show();

        Toast.makeText(MainActivity.this,"Community",Toast.LENGTH_LONG).show();

    }

}

here is the Frgment Class which is the default View 这是Frgment Class,它是默认视图

HomeFragment.class HomeFragment.class

 public class HomeF extends Fragment {

    private  RecyclerView rc;
    private Context mContext;
    private List<listitems> list = new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
            // Inflate the layout for this fragment
           final View view = inflater.inflate(R.layout.fragment_home, container, false);

        rc = view.findViewById(R.id.recycler);
        rc.setLayoutManager(new GridLayoutManager(this.getActivity(),2));
        rc.setHasFixedSize(true);

        list.add(new listitems(
                1
                ,"Mobile"
                ,R.drawable.phone
        ));

        list.add(new listitems(
                1
                ,"Laptop"
                ,R.drawable.laptop
        ));

        list.add(new listitems(
                1
                ,"CAR"
                ,R.drawable.car
        ));

        list.add(new listitems(
                1
                ," TV"
                ,R.drawable.tv
        ));
        list.add(new listitems(
                1
                ,"House"
                ,R.drawable.house
        ));

        itemsAdapter adapter = new itemsAdapter(this.getActivity(),list);
        rc.setAdapter(adapter);
        rc.setItemAnimator(new DefaultItemAnimator());
        return view;

    }
}

AdapterClass 适配器类

 public class itemsAdapter extends RecyclerView.Adapter<itemsAdapter.ViewHolder>  {

    private Context mContex;
    private List<listitems> myList ;

    public itemsAdapter(Context mContex, List<listitems> myList) {
        this.mContex = mContex;
        this.myList = myList;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_row,parent,false);
        ViewHolder holder = new ViewHolder(row);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

       final listitems listy = myList.get(position);

       holder.txt.setText(listy.getTitle());
        Glide.with(mContex).asBitmap().thumbnail(0.5f)
                .load(listy.getImage())
                .into(holder.img);
    }

    @Override
    public int getItemCount() {
        return myList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView img;
        public TextView txt;

        public ViewHolder(View itemView) {
            super(itemView);

            img =itemView.findViewById(R.id.imgLogo);
            txt = itemView.findViewById(R.id.txtTitle);

        }
    }

}

and Thanks for your time. 感谢您的宝贵时间。

After replace the fragment commit the FragmentTransaction. 替换片段后,提交FragmentTransaction。 add

ft.commit(); ft.commit();

in both the methods 在两种方法中

HomeF home = new HomeF();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragmentlayout,home);
ft.commit();

You need to commit the transaction at the end. 您需要在最后提交事务。 for both method 对于两种方法

private void  goHome(){
    HomeF home = new HomeF();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.fragmentlayout,home);
    ft.commit();
}
private void goSetting(){
    SettingF setting = new SettingF();
    FragmentManager fa = getFragmentManager();
    FragmentTransaction fs = fa.beginTransaction();
    fs.replace(R.id.fragmentlayout,setting);
    ft.commit();
}

Or Probably you should be using common method 或者可能您应该使用通用方法

private void replaceFragment(Fragment f){
        FragmentManager fa = getFragmentManager();
        FragmentTransaction fs = fa.beginTransaction();
        fs.replace(R.id.fragmentlayout,f);
        ft.commit();
    }

And passing the Fragment in the method rather than making different method for each fragment transaction. 并在该方法中传递Fragment,而不是为每个片段事务制作不同的方法。

Try using SupportFragmentManager instead of FragmentManager 尝试使用SupportFragmentManager而不是FragmentManager

 SettingF setting = new SettingF();
 getSupportFragmentManager().beginTransaction()
                                .add(R.id.fragmentlayout, setting, "setting")
                                .commit();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM