我有2个片段与底部导航视图 当我点击项目来改变片段没有任何反应,它仍然显示默认片段
这里是代码
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();
}
}
这里是Frgment Class,它是默认的View
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);
}
}
}
感谢您的时间。
答案 0 :(得分:0)
更换片段后提交FragmentTransaction。添加
两种方法中的ft.commit();
HomeF home = new HomeF();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragmentlayout,home);
ft.commit();
答案 1 :(得分:0)
您需要在最后提交交易。 对于这两种方法
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();
}
或者你可能应该使用常用方法
private void replaceFragment(Fragment f){
FragmentManager fa = getFragmentManager();
FragmentTransaction fs = fa.beginTransaction();
fs.replace(R.id.fragmentlayout,f);
ft.commit();
}
在方法中传递Fragment,而不是为每个片段事务制作不同的方法。
答案 2 :(得分:0)
尝试使用SupportFragmentManager而不是FragmentManager
SettingF setting = new SettingF();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragmentlayout, setting, "setting")
.commit();