listview实现点赞收藏功能
时间: 2023-06-20 15:06:04 浏览: 165
要实现ListView的点赞和收藏功能,你需要在ListView的每个item中添加两个按钮,一个是点赞按钮,一个是收藏按钮。
1. 首先,在ListView的item布局文件中添加点赞和收藏按钮:
```xml
<LinearLayout
...
android:orientation="horizontal">
<!-- 左侧图标 -->
<ImageView
...
android:id="@+id/icon" />
<!-- 中间布局 -->
<LinearLayout
...
android:orientation="vertical">
<TextView
...
android:id="@+id/title" />
<TextView
...
android:id="@+id/content" />
</LinearLayout>
<!-- 右侧操作按钮 -->
<LinearLayout
...
android:orientation="horizontal">
<!-- 点赞按钮 -->
<Button
...
android:id="@+id/like_btn"
android:text="赞" />
<!-- 收藏按钮 -->
<Button
...
android:id="@+id/favorite_btn"
android:text="收藏" />
</LinearLayout>
</LinearLayout>
```
2. 在ListView的Adapter中,为点赞和收藏按钮添加点击事件,并更新数据源:
```java
public class MyAdapter extends BaseAdapter {
private List<Item> mDataList;
public MyAdapter(List<Item> dataList) {
mDataList = dataList;
}
@Override
public int getCount() {
return mDataList.size();
}
@Override
public Item getItem(int position) {
return mDataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
final Item item = mDataList.get(position);
viewHolder.icon.setImageResource(item.getIcon());
viewHolder.title.setText(item.getTitle());
viewHolder.content.setText(item.getContent());
viewHolder.likeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
item.setLiked(!item.isLiked());
notifyDataSetChanged();
}
});
viewHolder.favoriteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
item.setFavorite(!item.isFavorite());
notifyDataSetChanged();
}
});
return convertView;
}
public static class ViewHolder {
ImageView icon;
TextView title;
TextView content;
Button likeBtn;
Button favoriteBtn;
public ViewHolder(View itemView) {
icon = itemView.findViewById(R.id.icon);
title = itemView.findViewById(R.id.title);
content = itemView.findViewById(R.id.content);
likeBtn = itemView.findViewById(R.id.like_btn);
favoriteBtn = itemView.findViewById(R.id.favorite_btn);
}
}
}
```
3. 在Item类中添加isLiked和isFavorite属性,以及相关的getter和setter方法:
```java
public class Item {
private int icon;
private String title;
private String content;
private boolean isLiked;
private boolean isFavorite;
public Item(int icon, String title, String content) {
this.icon = icon;
this.title = title;
this.content = content;
this.isLiked = false;
this.isFavorite = false;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public boolean isLiked() {
return isLiked;
}
public void setLiked(boolean liked) {
isLiked = liked;
}
public boolean isFavorite() {
return isFavorite;
}
public void setFavorite(boolean favorite) {
isFavorite = favorite;
}
}
```
这样,就可以实现ListView的点赞和收藏功能了。
阅读全文