2016-12-31 2 views
1

Ich arbeite an Multiple View Recycler Ansicht (mit nur 2 Ansichten). So funktioniert eine Ansicht ordnungsgemäß mit onClick, aber in einer anderen Ansicht, wenn ich auf diese Ansicht klicke, wird der Inhalt der ersten Ansicht in die nächste Aktivität geladen.onClick funktioniert nicht bei Mehrfachansicht RecyclerView

Unten ist mein Adapter Code:

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
GELöST
I "b", das heißt "getAdapterPosition" wurde unter Verwendung anstelle von "Position" und legte meinen onClick auf onBindViewHolder. Und unten ist mein Code.

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

Ich habe meine Zweifel im Code kommentiert. – Saurabh

Antwort

0

In Sie ViewHolder Klasse heißt MyViewHolder, fügen Sie den Klick-Listener:

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

Und es aus onBindViewHolder entfernen.

+0

movie.getAds, ist immer 1, also Feeds_Second-Klasse. Aber in meiner Datenbank ist der Wert 1 und 0. – Saurabh

+0

es funktioniert nicht. – Saurabh