[英]How can I get viewpager data in my activity android java
How can I get viewpager data in my activity android java如何在我的活动中获取 viewpager 数据 android java
I want the viewpager's current page data in my activity我想在我的活动中查看 viewpager 的当前页面数据
my activity我的活动
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);
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);
}
}
} }
my data class我的数据 class
class TravelLocation {
public int imageUrl;
public String genres,source; }
I want to access the other two text view in main activity我想访问主要活动中的其他两个文本视图
how can I set TextView
's text in my activity如何在我的活动中设置TextView
的文本
how can the other two fields get in activity其他两个领域如何参与活动
I want to set this field in activity我想在活动中设置这个字段
You can create another function inside of TravelLocationAdapter which accepts a value that you want the textview to have.您可以在 TravelLocationAdapter 内创建另一个 function ,它接受您希望 textview 具有的值。 This way code remains in adapter and you can call it and pass value to it from main activity.这种方式代码保留在适配器中,您可以调用它并将值从主要活动传递给它。 The function can even return the values. function 甚至可以返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.