我正在處理多視圖回收視圖(只有2個視圖)。因此,一個視圖正在使用onClick正常工作,但在另一個視圖中,當我點擊該視圖時,第一個視圖的內容將在下一個活動中加載。onClick不工作在多個視圖recyclerView
下面是我的適配器代碼:
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.MyViewHolder> {
View itemView;
View itemView1;
private Context context;
private List<WorldPopulation> worldpopulationlist = null;
private WorldPopulation movie;
private int x, b;
public FeedAdapter(FragmentActivity feeds, List<WorldPopulation> worldpopulationlist) {
this.context = feeds;
this.worldpopulationlist = worldpopulationlist;
ArrayList<WorldPopulation> arraylist = new ArrayList<>();
arraylist.addAll(worldpopulationlist);
}
@Override
public int getItemViewType(int position) {
movie = worldpopulationlist.get(position);
if (movie.getAds() ==1){
return 1;
}else {
return 0;
}
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView imageContent, userName, author, authorName, pages, pageNo;
TextView brandName, brandDescription, brandDescriptionText;
ImageView feedsImage, brandImage, brandMainImage, userImage;
View mview;
public MyViewHolder(View v) {
super(v);
mview = v;
imageContent = (TextView) v.findViewById(R.id.recycler_text);
feedsImage = (ImageView) v.findViewById(R.id.flag);
userImage = (ImageView) v.findViewById(R.id.userImage);
userName = (TextView) v.findViewById(R.id.userName);
author = (TextView) v.findViewById(R.id.author);
authorName = (TextView) v.findViewById(R.id.author_name);
pages = (TextView) v.findViewById(R.id.pages);
pageNo = (TextView) v.findViewById(R.id.page_no);
brandName = (TextView) v.findViewById(R.id.brand_name);
brandDescription = (TextView) v.findViewById(R.id.author_brand_description);
brandDescriptionText = (TextView) v.findViewById(R.id.author_name_brand_description);
brandImage = (ImageView) v.findViewById(R.id.brand_image);
brandMainImage = (ImageView) v.findViewById(R.id.brand_main_image);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case 1:
Log.i("sand23","this is game");
itemView1 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.feeds_adv_single_row, parent, false);
return new MyViewHolder(itemView1);
case 0:
Log.i("sand24","this is game2");
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.feeds_single_row, parent, false);
return new MyViewHolder(itemView);
}
return null;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
movie = worldpopulationlist.get(holder.getAdapterPosition());
this.b = holder.getAdapterPosition();
x = movie.getAds();
if (x==1){
holder.brandName.setText(movie.getAuthor_Name());
holder.brandDescription.setText(movie.getObjectId()); // when setting brandDescription to the objectId it shows objectId ysZicAeoRU which is correct
holder.brandDescriptionText.setText(movie.getPage_No());
Picasso.with(context)
.load(worldpopulationlist.get(holder.getAdapterPosition()).getBrand_Image())
.into(holder.brandImage);
Picasso.with(context)
.load(worldpopulationlist.get(holder.getAdapterPosition()).getFlag())
.into(holder.brandMainImage);
holder.mview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) // this onClick opens objectId 5y6jCCrxxa
{
Intent intent = new Intent(context, AdsActivity.class);
intent.putExtra("adsNext", worldpopulationlist.get(b).getAdsSecond());
intent.putExtra("objId", worldpopulationlist.get(b).getObjectId());
intent.putExtra("brandImage", worldpopulationlist.get(b).getBrand_Image());
intent.putExtra("brandName", worldpopulationlist.get(b).getAuthor_Name());
intent.putExtra("link", worldpopulationlist.get(b).getLink());
intent.putExtra("mobNo", worldpopulationlist.get(b).getCall());
intent.putExtra("brandDescription", worldpopulationlist.get(b).getPages());
intent.putExtra("brandDescriptionText", worldpopulationlist.get(b).getPage_No());
intent.putExtra("contactUs", worldpopulationlist.get(b).getContactUs());
intent.putExtra("aboutUs", worldpopulationlist.get(b).getAboutUs());
context.startActivity(intent);
}
});
}else {
holder.imageContent.setText(movie.getImagesContent());
holder.userName.setText(movie.getAddress());
holder.author.setText(movie.getAuthor());
holder.authorName.setText(movie.getAuthor_Name());
holder.pages.setText(movie.getPages());
holder.pageNo.setText(String.valueOf(movie.getClicks()));
Picasso.with(context)
.load(worldpopulationlist.get(holder.getAdapterPosition()).getUserImage())
.into(holder.userImage);
Picasso.with(context)
.load(worldpopulationlist.get(holder.getAdapterPosition()).getFlag())
.into(holder.feedsImage);
holder.mview.setOnClickListener(new View.OnClickListener() // this onCLick is working properly
{
@Override
public void onClick(View view) {
Intent intent = new Intent(context, Feeds_Second.class);
intent.putExtra("rank", worldpopulationlist.get(b).getImagesContent());
intent.putExtra("image", worldpopulationlist.get(b).getFlag());
intent.putExtra("ads", worldpopulationlist.get(b).getAds());
intent.putExtra("call", worldpopulationlist.get(b).getCall());
intent.putExtra("objectId", worldpopulationlist.get(b).getObjectId());
intent.putExtra("contactUs", worldpopulationlist.get(b).getContactUs());
intent.putExtra("aboutUs", worldpopulationlist.get(b).getAboutUs());
intent.putExtra("switch", worldpopulationlist.get(b).getSwitches());
intent.putExtra("link", worldpopulationlist.get(b).getLink());
intent.putExtra("clicks", worldpopulationlist.get(b).getClicks());
context.startActivity(intent);
}
});
}
}
@Override
public int getItemCount() {
return worldpopulationlist.size();
}
EDIT
我用 「B」,即 「getAdapterPosition」,而不是 「位置」,並置於我的onClick上onBindViewHolder解決。以下是我的代碼。
public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.MyViewHolder> {
View itemView;
private View itemView1;
private Context context;
private List<WorldPopulation> worldpopulationlist = null;
private WorldPopulation movie;
private int x;
public FeedAdapter(FragmentActivity feeds, List<WorldPopulation> worldpopulationlist) {
this.context = feeds;
this.worldpopulationlist = worldpopulationlist;
ArrayList<WorldPopulation> arraylist = new ArrayList<>();
arraylist.addAll(worldpopulationlist);
}
@Override
public int getItemViewType(int position) {
movie = worldpopulationlist.get(position);
if (movie.getAds() == 1) {
return 1;
} else {
return 0;
}
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView imageContent, userName, author, authorName, pages, pageNo;
TextView brandName, brandDescription, brandDescriptionText;
ImageView feedsImage, brandImage, brandMainImage, userImage;
View mview;
public MyViewHolder(View v) {
super(v);
mview = v;
imageContent = (TextView) v.findViewById(R.id.recycler_text);
feedsImage = (ImageView) v.findViewById(R.id.flag);
userImage = (ImageView) v.findViewById(R.id.userImage);
userName = (TextView) v.findViewById(R.id.userName);
author = (TextView) v.findViewById(R.id.author);
authorName = (TextView) v.findViewById(R.id.author_name);
pages = (TextView) v.findViewById(R.id.pages);
pageNo = (TextView) v.findViewById(R.id.page_no);
brandName = (TextView) v.findViewById(R.id.brand_name);
brandDescription = (TextView) v.findViewById(R.id.author_brand_description);
brandDescriptionText = (TextView) v.findViewById(R.id.author_name_brand_description);
brandImage = (ImageView) v.findViewById(R.id.brand_image);
brandMainImage = (ImageView) v.findViewById(R.id.brand_main_image);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 1:
itemView1 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.feeds_adv_single_row, parent, false);
return new MyViewHolder(itemView1);
case 0:
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.feeds_single_row, parent, false);
return new MyViewHolder(itemView);
}
return null;
}
@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
movie = worldpopulationlist.get(holder.getAdapterPosition());
if (holder.mview == itemView1) {
holder.mview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, AdsActivity.class);
intent.putExtra("adsNext", worldpopulationlist.get(position).getAdsSecond());
intent.putExtra("objId", worldpopulationlist.get(position).getObjectId());
intent.putExtra("brandImage", worldpopulationlist.get(position).getBrand_Image());
intent.putExtra("brandName", worldpopulationlist.get(position).getAuthor_Name());
intent.putExtra("link", worldpopulationlist.get(position).getLink());
intent.putExtra("mobNo", worldpopulationlist.get(position).getCall());
intent.putExtra("brandDescription", worldpopulationlist.get(position).getPages());
intent.putExtra("brandDescriptionText", worldpopulationlist.get(position).getPage_No());
intent.putExtra("contactUs", worldpopulationlist.get(position).getContactUs());
intent.putExtra("aboutUs", worldpopulationlist.get(position).getAboutUs());
context.startActivity(intent);
}
});
} else if (holder.mview == itemView) {
holder.mview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, Feeds_Second.class);
intent.putExtra("rank", worldpopulationlist.get(position).getImagesContent());
intent.putExtra("image", worldpopulationlist.get(position).getFlag());
intent.putExtra("ads", worldpopulationlist.get(position).getAds());
intent.putExtra("call", worldpopulationlist.get(position).getCall());
intent.putExtra("objectId", worldpopulationlist.get(position).getObjectId());
intent.putExtra("contactUs", worldpopulationlist.get(position).getContactUs());
intent.putExtra("aboutUs", worldpopulationlist.get(position).getAboutUs());
intent.putExtra("switch", worldpopulationlist.get(position).getSwitches());
intent.putExtra("link", worldpopulationlist.get(position).getLink());
intent.putExtra("clicks", worldpopulationlist.get(position).getClicks());
context.startActivity(intent);
}
});
}
x = movie.getAds();
if (x == 1) {
holder.brandName.setText(movie.getAuthor_Name());
holder.brandDescription.setText(movie.getPages()); // when setting brandDescription to the objectId it shows objectId ysZicAeoRU
holder.brandDescriptionText.setText(movie.getPage_No());
Picasso.with(context)
.load(worldpopulationlist.get(holder.getAdapterPosition()).getBrand_Image())
.into(holder.brandImage);
Picasso.with(context)
.load(worldpopulationlist.get(holder.getAdapterPosition()).getFlag())
.into(holder.brandMainImage);
} else {
holder.imageContent.setText(movie.getImagesContent());
holder.userName.setText(movie.getAddress());
holder.author.setText(movie.getAuthor());
holder.authorName.setText(movie.getAuthor_Name());
holder.pages.setText(movie.getPages());
holder.pageNo.setText(String.valueOf(movie.getClicks()));
Picasso.with(context)
.load(worldpopulationlist.get(holder.getAdapterPosition()).getUserImage())
.into(holder.userImage);
Picasso.with(context)
.load(worldpopulationlist.get(holder.getAdapterPosition()).getFlag())
.into(holder.feedsImage);
}
}
@Override
public int getItemCount() {
return worldpopulationlist.size();
}
我在代碼中評論了我的疑惑。 – Saurabh