Android ListView 设置隔行隔色

在网上查到设置隔行隔色的如下实现方式。


import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;

public class CustomAdapter extends SimpleAdapter {
private int[] colors = new int[] { 0x30ff00ff, 0x30f6f6f6 };
/*
*以数字方式传入时,需按ARGB格式;若按RGB格式,不生效
*或数组中成员为 android.graphics.Color.rgb(230, 230, 230)
*/

public CustomAdapter(Context context, List<HashMap<String, String>> items, int resource, String[] from, int[] to) {
super(context, items, resource, from, to);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
int colorPos = position % colors.length;
view.setBackgroundColor(colors[colorPos]);
return view;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android中使用ListView设置评论功能,可以按照以下步骤进操作: 1. 在布局文件中添加ListView组件,并设置其属性,如下所示: ``` <ListView android:id="@+id/listView_comments" android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@null" android:dividerHeight="0dp" /> ``` 2. 创建一个适配器类,用于将评论数据与ListView绑定。适配器类需要继承BaseAdapter,并实现以下方法: ``` public class CommentAdapter extends BaseAdapter { private List<Comment> commentList; private LayoutInflater inflater; public CommentAdapter(Context context, List<Comment> commentList) { this.commentList = commentList; inflater = LayoutInflater.from(context); } @Override public int getCount() { return commentList.size(); } @Override public Object getItem(int position) { return commentList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView == null) { viewHolder = new ViewHolder(); convertView = inflater.inflate(R.layout.item_comment, null); viewHolder.tvUsername = convertView.findViewById(R.id.tv_username); viewHolder.tvComment = convertView.findViewById(R.id.tv_comment); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } Comment comment = commentList.get(position); viewHolder.tvUsername.setText(comment.getUsername()); viewHolder.tvComment.setText(comment.getComment()); return convertView; } static class ViewHolder { TextView tvUsername; TextView tvComment; } } ``` 3. 创建一个评论类,用于存储每个评论的用户名和评论内容。 ``` public class Comment { private String username; private String comment; public Comment(String username, String comment) { this.username = username; this.comment = comment; } public String getUsername() { return username; } public String getComment() { return comment; } } ``` 4. 在Activity中初始化ListView和适配器,并为ListView设置适配器。 ``` public class CommentActivity extends AppCompatActivity { private ListView listViewComments; private List<Comment> commentList; private CommentAdapter commentAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_comment); listViewComments = findViewById(R.id.listView_comments); commentList = new ArrayList<>(); commentList.add(new Comment("user1", "comment1")); commentList.add(new Comment("user2", "comment2")); commentList.add(new Comment("user3", "comment3")); commentAdapter = new CommentAdapter(this, commentList); listViewComments.setAdapter(commentAdapter); } } ``` 5. 最后,可以通过添加新评论并调用适配器的notifyDataSetChanged()方法来更新评论列表。 ``` commentList.add(new Comment("user4", "comment4")); commentAdapter.notifyDataSetChanged(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值