/**
* Created by Prateek on 2/18/2017.
*/
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import newsee.novasyslabs.com.nodefetch.Others.CircularTransform;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
Context mContext;
public static ArrayList <String> embed_html = new ArrayList<>();
public static ArrayList <String> channel_thumbnail = new ArrayList<>();
public static ArrayList <String> channel_title = new ArrayList<>();
public static ArrayList <String> video_title = new ArrayList<>();
public static ArrayList <String> created_time = new ArrayList<>();
public static ArrayList <Integer> android_likes = new ArrayList<>();
public static ArrayList <Integer> pf_likes = new ArrayList<>();
public static ArrayList <Integer> android_views = new ArrayList<>();
public static ArrayList <Integer> pf_views = new ArrayList<>();
public RecyclerAdapter(Context mContext){
this.mContext = mContext;
}
class ViewHolder extends RecyclerView.ViewHolder{
WebView webView;
public ImageView chlogo;
public TextView chtitle;
public TextView vidtitle;
public TextView upld_time;
public TextView show_views;
public TextView show_likes;
public ImageView mLike;
int j = 0;
public ViewHolder(View itemView) {
super(itemView);
this.mLike = (ImageView)itemView.findViewById(R.id.video_like);
this.show_views = (TextView) itemView.findViewById(R.id.video_views_count);
this.show_likes = (TextView) itemView.findViewById(R.id.likes_count);
this.webView = (WebView) itemView.findViewById(R.id.web);
this.chlogo = (ImageView) itemView.findViewById(R.id.ch_logo);
this.chtitle = (TextView) itemView.findViewById(R.id.ch_name);
this.vidtitle = (TextView) itemView.findViewById(R.id.video_title);
this.upld_time = (TextView)itemView.findViewById(R.id.ch_time);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.layout_card, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.mLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.j==0) {
holder.mLike.setImageResource(R.drawable.liked);
CharSequence like = holder.show_likes.getText();
int new_like = Integer.parseInt((String) like) + 1;
Toast.makeText(mContext,"Liked" +new_like ,Toast.LENGTH_SHORT).show();
holder.j = 1;
} else {
holder.mLike.setImageResource(R.drawable.unlike);
CharSequence like = holder.show_likes.getText();
int new_like = Integer.parseInt((String) like) - 1;
Toast.makeText(mContext,"Unliked" +new_like,Toast.LENGTH_SHORT).show();
holder.j = 0;
}
}
});
holder.webView.getSettings().setJavaScriptEnabled(true);
holder.webView.loadData(embed_html.get(position), "text/html", "utf-8");
Picasso.with(mContext).load(channel_thumbnail.get(position)).transform(new CircularTransform()).memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE).into(holder.chlogo);
holder.chtitle.setText(channel_title.get(position));
holder.vidtitle.setText(video_title.get(position));
holder.upld_time.setText(created_time.get(position));
int and_views = android_views.get(position);
int views = pf_views.get(position);
views += and_views;
int and_likes = android_likes.get(position);
int likes = pf_likes.get(position);
likes += and_likes;
holder.show_views.setText("" + views);
holder.show_likes.setText("" + likes);
}
@Override
public int getItemCount() {
return embed_html.size();
}
}
当我点击然后图像更改为第0位置然后4,8,12 ....为什么onclicklistener影响recyclerview中的每4个对象我正在更改图像资源onClick但图像更改为4postion,8position
答案 0 :(得分:1)
我建议这样做,先在你的onBindViewHolder方法中添加这段代码:
holder.mLike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showImage(position);
}
}
});
然后仅在此类中创建一个新方法:
public void showImage(int position){
if (position == 0) {
holder.imageView.setImageResource(R.drawable.medal);
}
...
}