关于ListView和LinearLayout在列表行中的可聚焦性

时间:2012-03-25 15:27:52

标签: android focus android-listview android-linearlayout

问题是:如何设法将监听器放在LinearLayout内的元素上,ListView LinearLayout

具体来说,我有一个ListView,其中包含LinearLayout列表,每个列表都包含ButtonTextView。我只想知道用户是否点击了ButtonTextView以及在哪一行。

我不是特别想找代码,但到目前为止,我的OnItemListener上有一个ListView,当我点击它时,它会返回LinearLayout,而不是

2 个答案:

答案 0 :(得分:2)

您必须实现自定义适配器并在Button方法中为这些视图(TextViewgetView())手动设置侦听器。要查找单击ButtonTextView的行,您可以先将位置(您在getView()方法中获得的参数)设置为标记,然后在侦听器中查找根据视图的标记与您要处理的行。方法onItemClick只会告诉您列表中单击的

以下是一些使用自定义适配器的示例代码:

class CustomCursorAdapter extends CursorAdapter {

    private LayoutInflater mInflater;
    private Context ctx;

    public CustomCursorAdapter(Context context, Cursor c,
            boolean autoRequery) {
        super(context, c, autoRequery);
        mInflater = LayoutInflater.from(context);
        ctx = context;
    }

    @Override
    public void bindView(View view, final Context context, Cursor cursor) {
        ViewHolder holder = (ViewHolder) view.getTag();
        String txt = cursor.getString(cursor.getColumnIndex("name"));
        holder.text.setText(txt);
        holder.button.setText(txt);
        int currentPosition = cursor.getPosition();
        holder.text.setTag(new Integer(currentPosition));
        holder.button.setTag(new Integer(currentPosition));

        OnClickListener listener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                int thePosition = (Integer) v.getTag();
                if (v.getId() == R.id.button1) {
                    Toast.makeText(ctx,
                            "You clicked the Button from the row "
                                    + thePosition + "!", Toast.LENGTH_SHORT).show();
                } else if (v.getId() == R.id.textView1) {
                    Toast.makeText(ctx,
                            "You clicked the TextView from the row "
                                    + thePosition + "!", Toast.LENGTH_SHORT).show();
                }               
            }
        };
        holder.text.setOnClickListener(listener);
        holder.button.setOnClickListener(listener);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View v = mInflater.inflate(R.layout.adapters_cursor_adapter_clicks,
                null);
        ViewHolder holder = new ViewHolder();
        holder.text = (TextView) v.findViewById(R.id.textView1);
        holder.button = (Button) v.findViewById(R.id.button1);
        v.setTag(holder);
        return v;
    }

    class ViewHolder {
        TextView text;
        Button button;
    }

}

我对两个视图使用了一个OnClickListener,你可以使用两个监听器,每个视图一个跟随相同的原则。完整的类+布局文件可以在这里找到:http://pastebin.com/uM0mb4mq(这里有 gist :git://gist.github.com/2625797.git)

答案 1 :(得分:2)

您必须声明自定义适配器才能执行此操作。

本教程帮助了我很多:      http://www.vogella.de/articles/AndroidListView/article.html#listview_adaptermodel

以下是我的某个项目的摘录。单击时,它在ImageView内的两个图像之间交替显示。

 private class EventsAdapter extends ArrayAdapter<Eveniment> {

        private ArrayList<Eveniment> items;

        ...

        public OnClickListener goingClickListener2= new OnClickListener() {
            public void onClick(View v) {
                ((ImageView)v).setImageResource(R.drawable.going1);
                v.setOnClickListener(goingClickListener);
            }
        };
        public OnClickListener goingClickListener = new OnClickListener() {

            public void onClick(View v) {
                ((ImageView)v).setImageResource(R.drawable.going2);
                v.setOnClickListener(goingClickListener2);
            }
        };

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.listitemlayout, null);

                }
                if (items==null) return v;

                Eveniment ev = items.get(position);
                if (ev != null) { //init for each listItem
                        TextView tv_Titlu = (TextView) v.findViewById(R.id.tv_Titlu);
                        TextView tv_eventBody = (TextView)v.findViewById(R.id.tv_eventBody);
                        ImageView iv_EventPic = (ImageView)v.findViewById(R.id.iv_eventPic);
                        ImageView iv_going = (ImageView)v.findViewById(R.id.iv_going);

                        iv_going.setOnClickListener(goingClickListener);//setting the onClickListener

                        tv_Titlu.setText(ev.getEvent());

                        ... //other initialization for each view in my layout
                }
                return v;
        }

在上面的示例中,为每个listItem调用getView()。我通过在视图v上调用findViewById()(每个listItem中的RelativeLayout)来访问我的ImageVeiw