我已經搜索了一個解決方案,並且找不到任何可以指向解決方案的內容。ListView onItemClick不能使用自定義適配器
我在使用一個ListView,其中每個項目都有一個TextView和一個HorizontalScrollView。 HorizontalScrollView在運行時充滿了幾個TextView。當用戶點擊其中一個TextView時,我在視圖上切換背景,爲此我擴展了TextView類。
問題是ListView onItemClick不會觸發。我玩了一下代碼,出於某種原因,我不明白,點擊事件有時會觸發,但只有當我點擊每個列表項之間的權利。 我認爲這是因爲我的TextView處理單擊事件或因爲其中一個佈局阻止事件。
編輯:
我想要做的是:
當TextView的點擊,撥動它的一些視覺效果。
將其值(文本)添加到數據結構。
我的自定義TextView處理單擊事件罰款,但我不能抓住該適配器中的單擊事件。我需要它,因爲我需要知道ListView中的位置。
public class KeywordListAdapter extends ArrayAdapter<String> implements OnClickListener {
private final Context context;
private final String[] values;
private AspectManager aspectManager;
static class ViewHolder {
protected TextView aspectName;
protected HorizontalScrollView horizContainer;
protected LinearLayout keyWrapper;
protected KeywordTextView[] keyword;
}
public KeywordListAdapter(Context context, String[] values) {
super(context, R.layout.aspect_list_answer, values);
this.context = context;
this.values = values;
aspectManager = AspectManager.getInstance();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Aspect aspect = aspectManager.getAspectByName(values[position]);
View row = convertView;
// reuse views
if (row == null) {
Log.w(Constants.TAG, "row == null");
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.aspect_list_answer, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.horizContainer = (HorizontalScrollView) row.findViewById(R.id.horizontalRow);
viewHolder.horizContainer.setHorizontalScrollBarEnabled(false);
viewHolder.keyWrapper = new LinearLayout(context);
viewHolder.keyWrapper.setOrientation(LinearLayout.HORIZONTAL);
viewHolder.keyWrapper.setId(0);
viewHolder.keyword = new KeywordTextView[aspect.getKeywordCount()];
viewHolder.aspectName = (TextView) row.findViewById(R.id.lblAspect);
String[] keywords = aspect.getAspectKeys();
for (int i=0; i< keywords.length; i++){
viewHolder.keyword[i] = new KeywordTextView(context);
viewHolder.keyword[i].setOnClickListener(this);
viewHolder.keyWrapper.addView(viewHolder.keyword[i]);
}
viewHolder.horizContainer.addView(viewHolder.keyWrapper);
row.setTag(viewHolder);
}
//Set values
ViewHolder holder = (ViewHolder) row.getTag();
//Fill aspect name
holder.aspectName.setText(aspect.getAspectName());
holder.aspectName.setTextColor(Color.WHITE);
//Fill keywords
String[] keywords = aspect.getAspectKeys();
for (int i=0; i< keywords.length; i++){
holder.keyword[i].setKeyword(keywords[i]);
holder.keyword[i].setColor(baseColor);
//set keyword layout
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.gravity = Gravity.CENTER_VERTICAL;
llp.setMargins(20, 15, 20, 20);
holder.keyword[i].setLayoutParams(llp);
}
return row;
}
@Override
public void onClick(View v) {
((KeywordTextView)v).toggle();
}
public class KeywordTextView extends TextView {
String keyword;
String color;
boolean selected;
private GradientDrawable gd;
public KeywordTextView(Context context) {
super(context);
gd = new GradientDrawable();
selected = false;
}
public void setKeyword(String keyword){
this.keyword = keyword;
setText(keyword);
}
public String getKeyword(){
return keyword;
}
public void setColor(String color){
this.color = color;
setText(keyword);
}
public boolean isKeywordSelected(){
return this.selected;
}
protected void onDraw (Canvas canvas) {
super.onDraw(canvas);
//Set style
...
}
protected void toggle(){
selected = !selected;
}
}
}
活動培訓相關部分:
...
aspectList = (ListView)findViewById(R.id.lvAspectList);
aspectList.setAdapter(new KeywordListAdapter(this, aspects.split(", ")));
aspectList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.w(TAG, "aspectList onItemClick");
}
});
...
我認爲它的工作原理是因爲你的列表項目有一個onClick監聽器,它將拉焦。可能是錯誤的。 – zgc7009 2014-10-10 17:35:52
顯示了aspect_list_answer.xml的代碼 – 2014-10-10 17:37:15