如何解决共享相同布局的多个片段
所以我正在编写一个优惠券应用程序,我想将优惠券分为几类(服装、家庭..)我创建了一个优惠券片段,并在其中使用 tablayout 和 viewpager 添加了多个片段,这些片段假设代表类别。所以这里是我想对所有这些都使用相同的 recyclerview 并且一些与特定类别无关的产品不会出现我想通过使用 setvisibilty 来做到这一点fragment 和 recyclerview 本身以及第二个以便使用 viewpager 添加片段。 如何在所有这些视图中使用相同的 recyclerview 并使用适配器管理它们中的每一个?
这是我的代码,如果有人能提供帮助,我会很高兴
public class CouponsFragment extends Fragment {
RecyclerView recyclerView;
private DatabaseReference myRef;
private List<Coupons> couponsList;
private CouponsAdapter couponsAdapter;
TextView grooming;
TextView kids;
TextView nature;
TextView food;
TextView kitchenware;
TextView homedecor;
TextView cloth;
Toolbar toolbar;
ViewPager viewPager;
TabLayout tabLayout;
public CouponsFragment() {
}
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_coupons,container,false);
recyclerView=view.findViewById(R.id.coupons_recyclerview);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
couponsList=new ArrayList<>();
// recyclerView.setHasFixedSize(true);
//
// return inflater.inflate(R.layout.coupons_menu_bar,false);
// View rootView = inflater.inflate(R.layout.coupons_menu_bar,false);
toolbar=view.findViewById(R.id.toolbar_coupons);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
grooming = view.findViewById(R.id.Grooming);
kids = view.findViewById(R.id.Kids);
nature = view.findViewById(R.id.Nature);
food=view.findViewById(R.id.Food);
kitchenware=view.findViewById(R.id.Kitchenware);
homedecor=view.findViewById(R.id.Homedecore);
cloth=view.findViewById(R.id.Clothing);
tabLayout=view.findViewById(R.id.tablayout_coupon);
viewPager=view.findViewById(R.id.viewpager_coupon);
addFragment();
couponsAdapter=new CouponsAdapter(couponsList,getContext());
recyclerView.setAdapter(couponsAdapter);
recyclerView.setLayoutManager(linearLayoutManager);
return view;
}
private void addFragment(){
CouponsViewPagerAdapter couponsViewPagerAdapter =new CouponsViewPagerAdapter(getParentFragmentManager());
couponsViewPagerAdapter.addFragment(new GroomingFragment(),getString(R.string.Grooming));
couponsViewPagerAdapter.addFragment(new KidsFragment(),getString(R.string.Kids));
couponsViewPagerAdapter.addFragment(new NatureFragment(),getString(R.string.Nature));
couponsViewPagerAdapter.addFragment(new FoodFragment(),getString(R.string.Food));
couponsViewPagerAdapter.addFragment(new KitchenwareFragment(),getString(R.string.Kitchenware));
couponsViewPagerAdapter.addFragment(new HomedesignFragment(),getString(R.string.Home_decor));
couponsViewPagerAdapter.addFragment(new ClothingFragment(),getString(R.string.Womens_clothing));
viewPager.setAdapter(couponsViewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}
public class CouponsViewPagerAdapter extends FragmentPagerAdapter {
private List<Fragment>fragmentList=new ArrayList<>();
private List<String>titleList=new ArrayList<>();
public CouponsViewPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
public void addFragment(Fragment fragment,String title){
fragmentList.add(fragment);
titleList.add(title);
}
}
public class CouponsAdapter extends RecyclerView.Adapter<CouponsAdapter.MusicviewHolder> {
private List<Coupons>coupons;
public Resources res;
Context context;
Uri imageUri;
public CouponsAdapter(List<Coupons> coupons,Context context){
this.coupons=coupons;
this.context=context;
}
interface CouponsListener {
void onContactClicked(int position,View view);
void onDeleteClicked(int position);
}
CouponsListener listener;
public void setListener(CouponsListener listener) {
this.listener = listener;
}
@Override
public MusicviewHolder onCreateViewHolder(ViewGroup parent,int viewType) {//function design the cell
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.coupons_layout,parent,false);
MusicviewHolder musicviewholder=new MusicviewHolder(view);
return musicviewholder;
}
@Override
public void onBindViewHolder(MusicviewHolder holder,int position) {//this function takes the object and put it in the cell
//Coupons coupons=coupo.get(position);
// holder.songImg.setImageBitmap(music.getPhoto());
holder.description.setText(coupons.get(position).getDescription());
holder.brand_name.setText(coupons.get(position).getBrand_name());
holder.price.setText(coupons.get(position).getPrice());
// Glide.with(holder.brand_img.getContext()).load(C);
Glide.with(context).load(coupons.get(position).getBrand_img()).into(holder.brand_img);
Glide.with(context).load(coupons.get(position).getProduct_img()).into(holder.product_img);
}
@Override
public int getItemCount() {
return coupons.size();
}
public class MusicviewHolder extends RecyclerView.ViewHolder{
TextView description;
TextView brand_name;
TextView price;
ImageView brand_img;
ImageView product_img;
public MusicviewHolder(@NonNull View itemView) {
super(itemView);
description = (TextView) itemView.findViewById(R.id.description);
brand_name = (TextView) itemView.findViewById(R.id.brand_name);
price = (TextView) itemView.findViewById(R.id.prise);
brand_img = (ImageView) itemView.findViewById(R.id.brand_img);
product_img = (ImageView) itemView.findViewById(R.id.product_img);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener!=null)
listener.onContactClicked(getAdapterPosition(),v);
}
}
);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。