android:如何同时为Viewgroup及其子视图设置监听器?

时间:2013-03-21 09:58:03

标签: android android-listview android-adapter

我正在为listview使用自定义适配器。这是代码:

    public class ContentAdapter extends ArrayAdapter<Content>{

    private ArrayList<Content> items;

    public ContentAdapter(Context context, int textViewResourceId, ArrayList<Content> objects) {
        super(context, textViewResourceId, objects);
        this.items=objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v=convertView;
        if (v==null){
            LayoutInflater inflater=(LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v=inflater.inflate(R.layout.list_items_layout, null);
        }
        Content content=items.get(position);
        if (content!=null){
            TextView nm=(TextView)v.findViewById(R.id.itemname);
            TextView sm=(TextView)v.findViewById(R.id.itemsubname);
            nm.setTextColor(Color.RED);
            nm.setText(content.getName());
            sm.setTextColor(Color.GRAY);
            sm.setText(content.getSubname());
        }
        return v;
    }

R.layout.list_items_layout:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">

<TextView android:id="@+id/itemname"
    android:textIsSelectable="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="20sp"/>

<TextView android:id="@+id/itemsubname"
    android:textIsSelectable="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:textSize="15sp"/>

当我使用setOnItemClick(监听器)为我的listview创建监听器时,虽然设置了监听器,但它只能在我单击视图组的空白部分时识别。如果我点击两个TextView中的任何一个,它就无法响应。

有没有办法设置一个可以同时监视viewgroup及其子视图的OnItemClickListener?

0 个答案:

没有答案