Issue
How can I get view pager data in my activity android java
i create image slider using viewpager i want to get current viewpager details in my activity
this is my activity i access view pager and set data
ViewPager2 locationViewPager = findViewById(R.id.locationViewPager);
ImageView next = findViewById(R.id.next);
ImageView back = findViewById(R.id.back);
genres = findViewById(R.id.textView1);
source = findViewById(R.id.textView2);
List<TravelLocation> travelLocations = new ArrayList<>();
for (int i=0;i<=1000;i++){
TravelLocation travelLocationMV = new TravelLocation();
travelLocationMV.imageUrl = R.drawable.cardimage;
travelLocationMV.genres = "Thriller";
travelLocationMV.source = "Netflix";
travelLocations.add(travelLocationMV);
}
TravelLocationAdapter travelLocationAdapter = new TravelLocationAdapter(travelLocations);
locationViewPager.setAdapter(travelLocationAdapter);
locationViewPager.setClipToPadding(false);
locationViewPager.setClipChildren(false);
locationViewPager.setOffscreenPageLimit(3);
locationViewPager.getChildAt(0).setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);
CompositePageTransformer compositePageTransformer = new CompositePageTransformer();
compositePageTransformer.addTransformer(new MarginPageTransformer(40));
compositePageTransformer.addTransformer((page, position) -> {
float r = 1 - Math.abs(position);
page.setScaleY(0.90f + r * 0.04f);
});
locationViewPager.setPageTransformer(compositePageTransformer);
this is my adapter
class TravelLocationAdapter extends RecyclerView.Adapter<TravelLocationAdapter.TravelLocationViewHolder> {
private List<TravelLocation> travelLocations;
public TravelLocationAdapter(List<TravelLocation> travelLocations) {
this.travelLocations = travelLocations;
}
@NonNull
@Override
public TravelLocationViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new TravelLocationViewHolder(
LayoutInflater.from(parent.getContext()).inflate(
R.layout.cardlayout,
parent, false
)
);
}
@Override
public void onBindViewHolder(@NonNull TravelLocationViewHolder holder, int position) {
holder.setLocationData(travelLocations.get(position));
holder.kbvLocation.setOnClickListener(view -> {
});
}
@Override
public int getItemCount() {
return travelLocations.size();
}
static class TravelLocationViewHolder extends RecyclerView.ViewHolder {
private ImageView kbvLocation;
TravelLocationViewHolder(@NonNull View itemView) {
super(itemView);
kbvLocation = itemView.findViewById(R.id.kbvLocation);
}
void setLocationData(TravelLocation travelLocation) {
Picasso.get().load(travelLocation.imageUrl).into(kbvLocation);
}
}
this is my data class
class TravelLocation {
public String imageUrl;
public String genres,source; }
i want to access other 2 field in my activity
i wan to set this filed in activity
Solution
use interface to access recyclerView data.check this answer how to create interface : How to create a interface in main activity and pass the data from adapter to main activity through the interface?
Answered By - Muhammad Asad
Answer Checked By - Cary Denson (JavaFixing Admin)