先用一张图来显示效果:
从图中可以看到,点击gridview的一个item后出现黄色边框,播放按钮,以及五角星会变。下面看具体代码:
1.在gridview的adapter中设置标识显示的item:
private int clickTemp = -1;
// 标识选择的Item
public void setSeclection(int position) {
clickTemp = position;
}
public void setSeclection(int position) {
clickTemp = position;
}
2.在getview中设置点击操作
// 点击后设置边框颜色
if (clickTemp == position) {
convertView.setBackgroundResource(R.drawable.corner3);
//播放按钮点击显示,不点击隐藏
viewHolder.play.setVisibility(View.VISIBLE);
//收藏按钮的改变
viewHolder.love1.setVisibility(View.INVISIBLE);
viewHolder.love2.setVisibility(View.VISIBLE);
} else {
convertView.setBackgroundColor(Color.TRANSPARENT);
viewHolder.play.setVisibility(View.INVISIBLE);
viewHolder.love1.setVisibility(View.VISIBLE);
viewHolder.love2.setVisibility(View.INVISIBLE);
}
if (clickTemp == position) {
convertView.setBackgroundResource(R.drawable.corner3);
//播放按钮点击显示,不点击隐藏
viewHolder.play.setVisibility(View.VISIBLE);
//收藏按钮的改变
viewHolder.love1.setVisibility(View.INVISIBLE);
viewHolder.love2.setVisibility(View.VISIBLE);
} else {
convertView.setBackgroundColor(Color.TRANSPARENT);
viewHolder.play.setVisibility(View.INVISIBLE);
viewHolder.love1.setVisibility(View.VISIBLE);
viewHolder.love2.setVisibility(View.INVISIBLE);
}
3.在gridview的点击操作中设置事件
gridView.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
// Toast.makeText(MainActivity.this, "pic" + (position), Toast.LENGTH_SHORT).show();
adapter.setSeclection(position);
adapter.notifyDataSetChanged();
}
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
// Toast.makeText(MainActivity.this, "pic" + (position), Toast.LENGTH_SHORT).show();
adapter.setSeclection(position);
adapter.notifyDataSetChanged();
}
});
这样就好了,需要源码的可以在我的资源中下载。