onClick无法处理多个视图recyclerView

时间:2016-12-31 12:07:52

标签: android android-recyclerview

我正在处理多视图回收器视图(只有2个视图)。所以一个视图使用onClick正常工作,但在另一个视图中,当我点击该视图时,第一个视图的内容被加载到下一个Activity上。

以下是我的适配器代码:

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();
}

修改
解决
我使用的是“b”,即“getAdapterPosition”,而不是“position”,并将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();
}

1 个答案:

答案 0 :(得分:0)

在ViewHolder类中,即MyViewHolder,添加点击监听器:

    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);

            v.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                int pos = getAdapterPosition();
                WorldPopulation movie = worldpopulationlist.get(holder.getAdapterPosition());
                if(movie.getAds()==1){
                 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{
                  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);
                }
             }
         });
        }
    }

并从onBindViewHolder中删除它。