我想将按钮的可见性从“ like”更改为“ like”,如果将按钮按下,则将其更改为“ like” api将被调用,但只有一次更改
package com.vipulashishsanket.vadakvadan.Adapter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.picasso.Picasso;
import com.vipulashishsanket.vadakvadan.EndPoints;
import com.vipulashishsanket.vadakvadan.R;
import com.vipulashishsanket.vadakvadan.SharedPrefManager;
import com.vipulashishsanket.vadakvadan.VideoPlayingActivity;
import com.vipulashishsanket.vadakvadan.fetchingClass.LikesId;
import com.vipulashishsanket.vadakvadan.fetchingClass.ThumbnailFetching;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import org.json.JSONException;
import org.json.JSONObject;
import de.hdodenhof.circleimageview.CircleImageView;
public class ThumbnailFetchingAdapter extends RecyclerView.Adapter<ThumbnailFetchingAdapter.ViewHolder>
{
private Context mcontext;
private List<ThumbnailFetching> mThumbnailFetchings;
public ThumbnailFetchingAdapter(Context mcontext, List<ThumbnailFetching> mThumbnailFetchings)
{
this.mcontext=mcontext;
this.mThumbnailFetchings=mThumbnailFetchings;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mcontext).inflate(R.layout.card_feeds,parent,false);
return new ThumbnailFetchingAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position )
{
final ThumbnailFetching mthumbnailFetchings=mThumbnailFetchings.get(position);
final String userid=mthumbnailFetchings.getUserid();
final String postid=mthumbnailFetchings.getPostid();
holder.like.setText(mthumbnailFetchings.getTotallikes());
holder.comments.setText(mthumbnailFetchings.getTotalcomments());
holder.grpname.setText(mthumbnailFetchings.getPartyname());
holder.title.setText(mthumbnailFetchings.getTitle());
if(mthumbnailFetchings.getImgurl().equals(""))
{
holder.thumbnail.setImageResource(R.drawable.grpprofilepic_icon);
}
else
{
Picasso.with(mcontext).load(mthumbnailFetchings.getImgurl()).into(holder.profilepic);
}
Picasso.with(mcontext).load(mthumbnailFetchings.getThumbnailsurl()).into(holder.thumbnail);
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(mcontext, VideoPlayingActivity.class);
intent.putExtra("uid",userid);
intent.putExtra("postid",postid);
intent.putExtra("videourl",mthumbnailFetchings.getVideourl());
mcontext.startActivity(intent);
}
});
final String userId= SharedPrefManager.getInstance(mcontext).getUid();
final String likeid=mthumbnailFetchings.getLikeId();
if(likeid.equals("1"))
{
holder.unlike.setVisibility(View.VISIBLE);
holder.feedlike.setVisibility(View.GONE);
holder.unlike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
holder.feedlike.setVisibility(View.VISIBLE);
holder.unlike.setVisibility(View.GONE);
String count=holder.like.getText().toString();
int c=Integer.parseInt(count)-1;
String check=String.valueOf(c);
holder.like.setText(check);
like(userId, postid,"0");
}
});
}
else if(likeid.equals("0"))
{
holder.unlike.setVisibility(View.GONE);
holder.feedlike.setVisibility(View.VISIBLE);
holder.feedlike.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
holder.unlike.setVisibility(View.VISIBLE);
holder.feedlike.setVisibility(View.GONE);
String count=holder.like.getText().toString();
int c=Integer.parseInt(count)+1;
String check=String.valueOf(c);
holder.like.setText(check);
like(userId, postid,"1");
// notifyItemChanged(holder.getAdapterPosition());
}
});
}
else
{
}
}
public void like(final String userid, final String postid,final String likeid)
{
StringRequest request=new StringRequest(Request.Method.POST,
EndPoints.LikeUnlike,
new Response.Listener<String>() {
@Override
public void onResponse(String response)
{
try
{
JSONObject object=new JSONObject(response);
Toast.makeText(mcontext,object.getString("message"),Toast.LENGTH_SHORT).show();
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map=new HashMap<>();
map.put("liked",likeid);
map.put("post_id",postid);
map.put("user_id",userid);
return map;
}
};
RequestQueue requestQueue= Volley.newRequestQueue(mcontext);
requestQueue.add(request);
}
@Override
public int getItemCount() {
return mThumbnailFetchings.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
//public RelativeLayout std_id;
CircleImageView profilepic;
TextView grpname,loaction,feedtime,like,comments,title;
ImageView thumbnail,feedlike,unlike;
public ViewHolder(View itemView) {
super(itemView);
profilepic=itemView.findViewById(R.id.grpProfilepic);
grpname=itemView.findViewById(R.id.grp_name);
loaction=itemView.findViewById(R.id.grp_loaction);
feedtime=itemView.findViewById(R.id.feed_time);
like=itemView.findViewById(R.id.feeds_like_count);
comments=itemView.findViewById(R.id.feeds_comments_count);
thumbnail=itemView.findViewById(R.id.feeds_thumbnails);
title=itemView.findViewById(R.id.feed_title);
feedlike=itemView.findViewById(R.id.feeds_like);
unlike=itemView.findViewById(R.id.feeds_unlike);
}
}
}
notifyitemchange也不能正常工作,我想将按钮的可见性从“ like”更改为“ likes”和“ withlike”,如果按下按钮 api将被调用,但只有一次更改
答案 0 :(得分:1)
更改自
mThumbnailFetchings.get(position)。 like(userId,postid,“ 0”);
like(userId, postid,"0",getAdapterPosition());
和like()中的适配器位置,并notifyItemChanges
public class ThumbnailFetchingAdapter extends RecyclerView.Adapter<ThumbnailFetchingAdapter.ViewHolder>
{
private Context mcontext;
private List<ThumbnailFetching> mThumbnailFetchings;
public ThumbnailFetchingAdapter(Context mcontext, List<ThumbnailFetching> mThumbnailFetchings)
{
this.mcontext=mcontext;
this.mThumbnailFetchings=mThumbnailFetchings;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mcontext).inflate(R.layout.card_feeds,parent,false);
return new ThumbnailFetchingAdapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position )
{
final ThumbnailFetching mthumbnailFetchings=mThumbnailFetchings.get(position);
final String userid=mthumbnailFetchings.getUserid();
final String postid=mthumbnailFetchings.getPostid();
holder.like.setText(mthumbnailFetchings.getTotallikes());
holder.comments.setText(mthumbnailFetchings.getTotalcomments());
holder.grpname.setText(mthumbnailFetchings.getPartyname());
holder.title.setText(mthumbnailFetchings.getTitle());
if(mthumbnailFetchings.getImgurl().equals(""))
{
holder.thumbnail.setImageResource(R.drawable.grpprofilepic_icon);
}
else
{
Picasso.with(mcontext).load(mthumbnailFetchings.getImgurl()).into(holder.profilepic);
}
Picasso.with(mcontext).load(mthumbnailFetchings.getThumbnailsurl()).into(holder.thumbnail);
holder.thumbnail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(mcontext, VideoPlayingActivity.class);
intent.putExtra("uid",userid);
intent.putExtra("postid",postid);
intent.putExtra("videourl",mthumbnailFetchings.getVideourl());
mcontext.startActivity(intent);
}
});
final String userId= SharedPrefManager.getInstance(mcontext).getUid();
final String likeid=mthumbnailFetchings.getLikeId();
if(likeid.equals("1"))
{
holder.unlike.setVisibility(View.VISIBLE);
holder.feedlike.setVisibility(View.GONE);
holder.unlike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
holder.feedlike.setVisibility(View.VISIBLE);
holder.unlike.setVisibility(View.GONE);
String count=holder.like.getText().toString();
int c=Integer.parseInt(count)-1;
String check=String.valueOf(c);
holder.like.setText(check);
mthumbnailFetchings.setLikeId("0");
like(userId, postid,"0",position);
}
});
}
else if(likeid.equals("0"))
{
holder.unlike.setVisibility(View.GONE);
holder.feedlike.setVisibility(View.VISIBLE);
holder.feedlike.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
holder.unlike.setVisibility(View.VISIBLE);
holder.feedlike.setVisibility(View.GONE);
String count=holder.like.getText().toString();
int c=Integer.parseInt(count)+1;
String check=String.valueOf(c);
holder.like.setText(check);
mthumbnailFetchings.setLikeId("1");
like(userId, postid,"1",position);
// notifyItemChanged(holder.getAdapterPosition());
}
});
}
else
{
}
}
public void like(final String userid, final String postid,final String likeid,int position)
{
StringRequest request=new StringRequest(Request.Method.POST,
EndPoints.LikeUnlike,
new Response.Listener<String>() {
@Override
public void onResponse(String response)
{
try
{ ThumbnailFetchingAdapter.this.notifyItemChange(position);
JSONObject object=new JSONObject(response);
Toast.makeText(mcontext,object.getString("message"),Toast.LENGTH_SHORT).show();
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> map=new HashMap<>();
map.put("liked",likeid);
map.put("post_id",postid);
map.put("user_id",userid);
return map;
}
};
RequestQueue requestQueue= Volley.newRequestQueue(mcontext);
requestQueue.add(request);
}
@Override
public int getItemCount() {
return mThumbnailFetchings.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
//public RelativeLayout std_id;
CircleImageView profilepic;
TextView grpname,loaction,feedtime,like,comments,title;
ImageView thumbnail,feedlike,unlike;
public ViewHolder(View itemView) {
super(itemView);
profilepic=itemView.findViewById(R.id.grpProfilepic);
grpname=itemView.findViewById(R.id.grp_name);
loaction=itemView.findViewById(R.id.grp_loaction);
feedtime=itemView.findViewById(R.id.feed_time);
like=itemView.findViewById(R.id.feeds_like_count);
comments=itemView.findViewById(R.id.feeds_comments_count);
thumbnail=itemView.findViewById(R.id.feeds_thumbnails);
title=itemView.findViewById(R.id.feed_title);
feedlike=itemView.findViewById(R.id.feeds_like);
unlike=itemView.findViewById(R.id.feeds_unlike);
}
}
}
答案 1 :(得分:0)
尝试以这种方式使用点击,可能会解决您的问题。
if(likeid.equals("1")) {
holder.unlike.setVisibility(View.VISIBLE);
holder.feedlike.setVisibility(View.GONE);
}else if(likeid.equals("0")){
holder.unlike.setVisibility(View.GONE);
holder.feedlike.setVisibility(View.VISIBLE);
}
holder.feedlike.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
holder.unlike.setVisibility(View.VISIBLE);
holder.feedlike.setVisibility(View.GONE);
String count=holder.like.getText().toString();
int c=Integer.parseInt(count)+1;
String check=String.valueOf(c);
holder.like.setText(check);
like(userId, postid,"1");
// notifyItemChanged(holder.getAdapterPosition());
}
});
holder.unlike.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){
holder.feedlike.setVisibility(View.VISIBLE);
holder.unlike.setVisibility(View.GONE);
String count=holder.like.getText().toString();
int c=Integer.parseInt(count)-1;
String check=String.valueOf(c);
holder.like.setText(check);
like(userId, postid,"0");
}
});