[英]How can I get viewpager data in my activity android java
如何在我的活动中获取 viewpager 数据 android java
我想在我的活动中查看 viewpager 的当前页面数据
我的活动
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);
我的适配器
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);
}
}
}
我的数据 class
class TravelLocation {
public int imageUrl;
public String genres,source; }
我想访问主要活动中的其他两个文本视图
如何在我的活动中设置TextView
的文本
其他两个领域如何参与活动
我想在活动中设置这个字段
您可以在 TravelLocationAdapter 内创建另一个 function ,它接受您希望 textview 具有的值。 这种方式代码保留在适配器中,您可以调用它并将值从主要活动传递给它。 function 甚至可以返回值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.