所以..我想创建一个列表视图,其视图将由自定义数组适配器处理。此列表视图的每个视图都有一个子文本视图。我想要做的是我想处理(启动一个intent)此文本视图的click事件,该事件位于列表视图中的视图中。我怎么做?我建议的代码会运行吗?
建议代码:
@Override
public view getView(int position, View convertView, View parent)
{
View v = convertView;
/*code…*/
TextView t = (TextView) v.findViewById(R.id.text_field);
t.setOnClickListener(new OnClickListener({
@Override
public void onClick(View v){
MediaPlayer mp = MediaPlayer.create(thisActivity.this, R.raw.sound);
mp.start();
}));
}
答案 0 :(得分:0)
请参阅以下示例。
@Override
public View getView(final int position, View view, final ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (view != null) {
holder = (ViewHolder) view.getTag();
} else {
view = inflater.inflate(R.layout.store_list_item, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
}
holder.storeName.setText(mShoppingLists.get(position).getName());
holder.editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final FragmentManager manager = ((Activity) mContext).getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
ShoppingListEditFragment shoppingListEditFragment =
ShoppingListEditFragment.newInstance(mShoppingLists.get(position).getId());
transaction.replace(R.id.container, shoppingListEditFragment, "shoppinglistedit");
transaction.addToBackStack(null);
transaction.commit();
}
});
holder.handle.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
((DynamicListView) parent).onHandleLongClickListener(new DragEvent());
return true;
}
});
return view;
}
答案 1 :(得分:0)
如果您想处理listview项目的click事件,请输入以下代码:
/*Inside the onCreate method of your activity class which displays the list
......*/
ListView l = (ListView)findViewById(R.id.list);
CustomAdapter adapter = new CustomAdapter(this, 0, list, R.color.category_numbers);
l.setAdapter(adapter);
l.setOnItemClickListener(new AdapterView.OnItemClickListener({
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long
id) {
/*your code*/
}
});
答案 2 :(得分:0)
要捕获listview项目中子元素的click事件,还需要在子元素上设置focusable="false"
,在Listview本身设置focusable="true"
。
除此之外,代码看起来正确。因此,请尝试设置focusable
和focusableInTouchMode
参数。