我正在制作一个与社交网络相关的应用程序,像facebook一样喜欢作为帖子选项我们的应用程序已经拍手。我正在使用listview显示所有帖子,并且在列表视图中有一个相对布局作为imageview和reactno作为反应的数量反应,但问题是当用户点击时图标的drawable改变了以及数字也是如此,但当用户向下滚动并返回时,反应图标会在加载时更改回页面,任何人都知道如何解决这个问题?
这是我的代码
public class postsAdapter extends ArrayAdapter{
private final Context context;
private final ArrayList<posts>posts;
public postsAdapter(Context context,ArrayList<posts> posts) {
super(context, -1,posts);
this.context = context;
this.posts=posts;
}
static class ViewHolder {
ImageView pic,reactions,post_image,thoughts,share;
RelativeTimeTextView time;
TextView reactionsno,thoughtsno,shareno,name,rpost,profileUrl;
RelativeLayout lay,buttonpanel;
}
ArrayList<posts>postlist=new ArrayList<posts>();
ArrayList<TextView>reactionslist=new ArrayList<TextView>();
posts post;
@Override
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder vie;
vie = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.posts, null);
post=posts.get(position);
postlist.add(post);
vie.pic=(ImageView)view.findViewById(R.id.posts_pic);
vie.name= (TextView) view.findViewById(R.id.posts_name);
vie.lay=(RelativeLayout)view.findViewById(R.id.mainlay);
vie.time=(RelativeTimeTextView)view.findViewById(R.id.time);
vie.rpost=(TextView)view.findViewById(R.id.posts_text);
vie.reactionsno=(TextView)view.findViewById(R.id.reactionsno);
vie.thoughtsno=(TextView)view.findViewById(R.id.thoughtsno);
vie.shareno=(TextView)view.findViewById(R.id.shareno);
vie.post_image=(ImageView)view.findViewById(R.id.post_image);
vie.reactions=(ImageView)view.findViewById(R.id.reactions);
vie.thoughts=(ImageView)view.findViewById(R.id.thoughts);
vie.share=(ImageView)view.findViewById(R.id.share);
vie.buttonpanel=(RelativeLayout)view.findViewById(R.id.bottompanel);
vie.name.setText(post.getName());
vie.rpost.setText(post.getPost());
vie.reactionsno.setText(""+post.getReactions());
vie.thoughtsno.setText(""+post.getThoughts());
vie.shareno.setText(""+post.getSeen());
RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams)vie.buttonpanel.getLayoutParams();
RequestOptions req = new RequestOptions();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
final String atime=dateFormat.format(date);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date mDate = sdf.parse(post.getTime());
long timeInMilliseconds = mDate.getTime();
vie.time.setReferenceTime(timeInMilliseconds);
} catch (ParseException e) {
e.printStackTrace();
}
if(post.getHasPic()==1){
params.addRule(RelativeLayout.BELOW,R.id.posts_text);
vie. lay.removeView( vie.post_image);
}
else{
Glide.with(context)
.load(Social.URL + "posts/" + post.getPicUrl() + ".png")
.apply(req.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC))
.into( vie.post_image);
}
if(post.liked==1){
vie.reactions.setImageResource(R.drawable.happy);
vie.reactions.setTag("clap");
}
else if(post.liked==2){
vie.reactions.setImageResource(R.drawable.clap1);
vie.reactions.setTag("clap1");
}
Glide
.with(context)
.load(Social.URL + "profilepics/" + post.getToken() + ".png")
.apply(req.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC))
.apply(RequestOptions.circleCropTransform())
.into( vie.pic);
final postDb db=new postDb(context,"posts");
vie.reactions.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View iew) {
new animationHelper().Blink(iew);
ImageView view=(ImageView)iew;
if(view.getTag().equals("clap")){
view.setTag("clap1");
postlist.get(position).setReactions(postlist.get(position).getReactions()+ 1);
db.update(postlist.get(position).getReactions(),2,postlist.get(position).getPicUrl());
vie.reactionsno.setText(""+postlist.get(position).getReactions());
view.setImageResource(R.drawable.clap1);
}
else{
postlist.get(position).setReactions(postlist.get(position).getReactions()- 1);
view.setImageResource(R.drawable.happy);
db.update(postlist.get(position).getReactions(),1,postlist.get(position).getPicUrl());
vie.reactionsno.setText(""+postlist.get(position).getReactions());
view.setTag("clap");
}
}
});
return view;
}}
Onclick方法工作正常但是当页面滚动时,drawable被设置为原始drawable
答案 0 :(得分:0)
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout. posts, null);
}
//todo
}