Answer the question
In order to leave comments, you need to log in
Is it possible to load different data in recycler-view?
I have a fragment with two buttons. Each of them now leads to a fragment with a recycler-view, however, we need to make sure that different data is loaded into the recycler-view depending on the selected button. Can this be done with a single fragment with recycler-view? Or do I need to create a second one for the second button?
ых данных в recycler-view
Задать вопрос
0
У меня есть фрагмент с двумя кнопками. Каждая из них сейчас ведет на фрагмент с recycler-view, однако надо сделать так, чтобы в recycler-view загружались разные данные в зависимости от выбранной кнопки. Можно ли это сделать с один фрагментом с recycler-view? Или нужно создавать второй для второй кнопки?
Код фрагмента с кнопками (для простоты просто поставил клик на картинки)
ImageView imageView = (ImageView) view.findViewById(R.id.menu_image);
Glide.with(this).load(R.drawable.cutting_coupon).into(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TestFragment newFragment = new TestFragment();
((AppCompatActivity) imageView.getContext()).getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, newFragment)
.addToBackStack(null)
.commit();
}
});
ImageView imageView1 = (ImageView) view.findViewById(R.id.menu_image1);
Glide.with(this).load(R.drawable.sale).into(imageView1);
imageView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TestFragment newFragment = new TestFragment();
((AppCompatActivity) imageView.getContext()).getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, newFragment)
.addToBackStack(null)
.commit();
}
});
public class TestFragment extends Fragment {
private ArrayList<GalleryGridObject> galleryList = new ArrayList<GalleryGridObject>();
private TestAdapter mAdapter;
private RecyclerView recyclerView;
public TestFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_test, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
mAdapter = new TestAdapter(this, galleryList);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
recyclerView.setHasFixedSize(true);
prepareGalleryData();
return view;
}
private void prepareGalleryData()
{
GalleryGridObject gallery = new GalleryGridObject(R.drawable.test);
galleryList.add(gallery);
gallery = new GalleryGridObject(R.drawable.test);
galleryList.add(gallery);
gallery = new GalleryGridObject(R.drawable.test);
galleryList.add(gallery);
gallery = new GalleryGridObject(R.drawable.test);
galleryList.add(gallery);
gallery = new GalleryGridObject(R.drawable.test);
galleryList.add(gallery);
gallery = new GalleryGridObject(R.drawable.test);
galleryList.add(gallery);
gallery = new GalleryGridObject(R.drawable.test);
galleryList.add(gallery);
gallery = new GalleryGridObject(R.drawable.test);
galleryList.add(gallery);
mAdapter.notifyDataSetChanged();
}
Answer the question
In order to leave comments, you need to log in
Can. Google towards the adapter, there is an overridden method
public int getItemViewType (int position)
in which you can determine the type of the returned item. For each type, you need to describe your own ViewHolder and then in onBindViewHolder (final ViewHolder holder, final int position) bind the desired one (as you can see, both the type and the binding are tied to the position)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question