public class SimpleAdapterForClickChangeColor extends SimpleAdapter {
public SimpleAdapterForClickChangeColor(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) {
convertView = null; convertView = super.getView(position, convertView, parent);
convertView.setBackgroundColor(Color.TRANSPARENT);
//点击变色
convertView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//此处怎么写
}
});
//改变item的高度
ViewGroup.LayoutParams params=convertView.getLayoutParams();
params.height=50;
convertView.setLayoutParams(params); return convertView;
}
}
请问:onClick方法里怎么写?谢谢
public SimpleAdapterForClickChangeColor(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) {
convertView = null; convertView = super.getView(position, convertView, parent);
convertView.setBackgroundColor(Color.TRANSPARENT);
//点击变色
convertView.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//此处怎么写
}
});
//改变item的高度
ViewGroup.LayoutParams params=convertView.getLayoutParams();
params.height=50;
convertView.setLayoutParams(params); return convertView;
}
}
请问:onClick方法里怎么写?谢谢
android的listView有滚动条时(即实际Item数比显示出来的Item数多),其点击item变色代码入下:
for(int i=0;i<parent.getCount();i++){
View v=parent.getChildAt(i);
if (position == i) {
v.setBackgroundColor(0xFFB679D4);
} else {
v.setBackgroundColor(Color.TRANSPARENT);//此行空指针错
}
}
点击时v.setBackgroundColor(Color.TRANSPARENT);报空指针错。
错误分析:因为实际Item数比显示出来的Item数多,即parent.getCount()>parent.getChildCount(),所以View v=parent.getChildAt(i);当v不在显示的view内时,得到的v为null。所以报错
尝试方法1:parent.getCount()改成parent.getChildCount(),此时出现的新问题是往下滑动时,没有点击的Item也会变色。
尝试方法2:parent.getCount()改成一个整数,该整数=parent.getChildCount(),此时没有往下滑动时正常,但是往下滑动时并不能点击变色。
尝试方法3:做分页,代价太大。所以放弃
麻烦各位给出解决方案~~困扰很久了~
package com.renrenwei.adapter;import com.renrenwei.activity.R;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;public class ActivityGroupAdapter extends BaseAdapter {
private int bottomnum;
private LayoutInflater layoutinflater;
private View myview;
private int index = 0; public ActivityGroupAdapter(Context c, int bottomnum) {
this.bottomnum = bottomnum;
this.layoutinflater = LayoutInflater.from(c);
} public int getCount() {
return bottomnum;
} public void SetFocus(int index) {
this.index = index;
// this.notifyDataSetChanged();
this.notifyDataSetInvalidated();// 刷新界面
} public Object getItem(int position) {
return position;
} public long getItemId(int position) {
return position;
} public View getView(int position, View convertView, ViewGroup parent) {
myview = layoutinflater.inflate(R.layout.group_bottom_item, null);
ImageView imageview = (ImageView) myview
.findViewById(R.id.imageview_bottom);
TextView textview = (TextView) myview
.findViewById(R.id.textview_bottom);
if (position == 0) {
imageview.setBackgroundResource(android.R.drawable.ic_menu_mapmode);
textview.setText(R.string.Group_Item_Main);
}
if (position == 1) {
imageview
.setBackgroundResource(android.R.drawable.ic_menu_myplaces);
textview.setText(R.string.Group_Item_My);
}
if (position == 2) {
imageview.setBackgroundResource(android.R.drawable.ic_menu_share);
textview.setText(R.string.Group_Item_square);
}
if (position == 3) {
imageview.setBackgroundResource(android.R.drawable.ic_menu_more);
textview.setText(R.string.Group_Item_More);
}
if (position == index) {
myview.setBackgroundResource(R.drawable.css_biaoqian);
} else {
myview.setBackgroundResource(0);
}
return myview;
}
}
在你的点击事件中把position传递过来
class BottomListener implements OnItemClickListener { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
SwitchActivity(arg2);
}
}
方法就是这样。改变颜色等你自己在getview里面搞
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
View tag=(View)parent.getTag();
if (tag != null)
{
if (tag.getId()==id) return;
tag.setBackgroundColor(color1);
}
view.setBackgroundColor(color2);
parent.setTag(view);
}
});
public View getView(int position, View convertView, ViewGroup parent) {
View convertView可以直接拿来用的
public View getView(int position, View convertView, ViewGroup parent) {
convertView = null;
convertView = super.getView(position, convertView, parent);
convertView.setBackgroundColor(Color.TRANSPARENT);
convertView.setTag(new Integer(position));
Log.i("convertView.getTag()",String.valueOf(convertView.getTag()));
Log.i("selectedPosition",String.valueOf(selectedPosition));
if(convertView.getTag().equals(selectedPosition)){
convertView.setBackgroundColor(0xFFB679D4);
Log.i("equals",String.valueOf(selectedPosition)+"xxxx");
}
convertView.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
// TODO Auto-generated method stub
selectedPosition = (Integer)v.getTag();
Log.i("selectedPosition",String.valueOf(selectedPosition)+",,,,,,");
}
});
//改变item的高度
ViewGroup.LayoutParams params=convertView.getLayoutParams();
params.height=50;
convertView.setLayoutParams(params); return convertView;
}
中的convertView.setOnClickListener()事件屏蔽了
//板块列表点击事件
plateList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView< ?> parent, View view,
int position, long id) {
Log.i("selectedPlateName","selectedPlateName"); View tag=(View)parent.getTag();
if (tag != null)
{
if (tag.getId()==id) return;
tag.setBackgroundColor(Color.TRANSPARENT);
}
view.setBackgroundColor(0xFFB679D4);
parent.setTag(view); ListView listView = (ListView) parent;
HashMap<String, String> itemData = (HashMap< String, String>) listView.getItemAtPosition(position);
selectedPlateName = itemData.get("name");
Log.i("selectedPlateName",selectedPlateName);
}
});
事件,求解决方案~
mListView.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id)
{
adapter.setmSelectItem(position);
adapter.notifyDataSetInvalidated();