【发布时间】:2015-06-05 08:10:39
【问题描述】:
我有扩展 ArrayAdapter 的自定义适配器。我对不同的项目有不同的布局。我喜欢制作来自特定类型unclickable 的物品。这是我的代码:
package android.adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Switch;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import android.items.ItemProperties;
public class CustomAdapter extends ArrayAdapter<ItemProperties> {
private static final int TYPE_ITEM_AMOUNTS = 0;
private static final int TYPE_ITEM_LIMITS = 1;
private static final int TYPE_MAX_COUNT = 2;
private LayoutInflater mInflater;
private ArrayList<Integer> positions = new ArrayList<Integer>();
private List<ItemProperties> items = new ArrayList<ItemProperties>();
public CustomAdapter(Context context, int resource, List<ItemProperties> itemCards, ArrayList positions) {
super(context, resource, items);
this.positions = positions;
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public ItemProperties getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflaterRow = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = null;
int type = getItemViewType(position);
switch (type) {
case TYPE_ITEM_AMOUNTS:
rowView = inflaterRow.inflate(R.layout.item_details, parent, false);
rowView.setEnabled(false);
rowView.setOnClickListener(null);
TextView txtName = (TextView) rowView.findViewById(R.id.text_name);
txtName.setText(itemCards.get(position).getCardLabelDesc());
TextView txtValue = (TextView) rowView.findViewById(R.id.text_value);
txtValue.setText(itemCards.get(position).getCardLabelValue());
break;
case TYPE_ITEM_LIMITS:
rowView = inflaterRow.inflate(R.layout.item_limit_settings, parent, false);
rowView.setEnabled(true);
TextView txtLimitDesc = (TextView) rowView.findViewById(R.id.text_item_description);
txtLimitDesc.setText(itemCards.get(position).getCardLabelDesc());
Switch limitEnable = (Switch) rowView.findViewById(R.id.enable_limit);
limitEnable.setText("");
TextView txtLimit = (TextView) rowView.findViewById(R.id.txt_limit);
txtLimit.setText(itemCards.get(position).getCardLabelValue());
break;
}
return rowView;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public int getItemViewType(int position) {
if (positions.contains(position))
return TYPE_ITEM_LIMITS;
else
return TYPE_ITEM_AMOUNTS;
}
}
但是,这对我不起作用,什么都不能点击。
在我填充列表的活动中,我有这个:
propertiesAdapter=new
CustomAdapter(this,R.layout.item_details,itemPropertiesList,positions);
listDetails.setAdapter(propertiesAdapter);
listDetails.setOnItemClickListener(this);
我有 onItemClick 事件处理程序。
有人知道是什么问题吗?
【问题讨论】:
-
如果删除 rowView.setEnabled(false); 会发生什么rowView.setOnClickListener(null);来自 TYPE_ITEM_AMOUNTS 案例。那么这两种类型的项目都可以点击了吗?
-
不行,第一种可点击,第二种不可点击,我要归档相反的
-
listDetails 是 ListView UI 吗? listDetails 是否引用了 ListView 布局中的 ID?
-
listDetails = (ListView)findViewById(R.id.list_details);我有这个,所以是 ListView UI
-
发布 R.layout.item_details 的布局。我认为这个ID可能会混淆。
标签: android android-listview onitemclick