使用自定义适配器绕过 ListView 的 onListItemClick()

发布于 2024-11-29 09:27:29 字数 5601 浏览 3 评论 0原文

我意识到关于这个问题有很多问题,但是将 row.xml 中的每个视图设置为 focusable="false" 和 focusableInTouchMode="false" 的解决方案不起作用,从 ListActivity 获取 ListView 并设置也不起作用setItemsCanFocus(假)。

奇怪的是,当注册上下文菜单时,长按有效。普通的水龙头呢?没有。我尝试设置像 OnItemClickListener 这样的监听器,但没有成功。

我在某处读到,我可以通过重写适配器中的 getView() 来解决这个问题?但我不太确定这是如何运作的。注意,我不想知道用户点击了哪个视图;我只关心单击列表行以在 onListItemClick() 中启动相应的代码。

也许我的 row.xml 中有一些东西是错误的?或者它是否受到我设置 ListView 适配器的方式的影响(放置在 onResume() 而不是 onCreate() 中来更新信息)?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:focusable="false"
  android:focusableInTouchMode="false">
<TextView style="?android:attr/listSeparatorTextViewStyle"
  android:id="@+id/listSectionHeader"
  android:layout_width = "fill_parent"
  android:layout_height="wrap_content"
  android:paddingTop="2dp"
  android:paddingBottom="2dp"
  android:paddingLeft="5dp"
  android:textColor="@android:color/white"
  android:visibility="gone"
  android:focusable="false"
  android:focusableInTouchMode="false"/>
<RelativeLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="70dp"
  android:background="@drawable/list_button"
  android:focusable="false"
  android:focusableInTouchMode="false"
  android:clickable="true"
  android:longClickable="true"
  >
<TextView android:id="@+id/itemID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:textStyle="bold"
    android:layout_alignParentLeft="true"
    android:focusable="false"
    android:focusableInTouchMode="false"/>
<CheckBox 
    android:id="@+id/returnedCheckbox"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_margin="10dp"
    android:checked="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    />
<LinearLayout
    android:layout_toRightOf="@id/returnedCheckbox"
    android:layout_margin="5dp"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false">
    <TextView 
        android:id="@+id/stuffName"
        android:layout_width="fill_parent"
        android:lines="1"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:scrollHorizontally="true"
        android:text="Hey there"
        android:textSize="25sp"
        android:textColor="#FFF"
        android:shadowColor="#000"
        android:focusable="false"
        android:focusableInTouchMode="false"
    />
    <RelativeLayout
        android:id="@+id/detailsLayout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false">
        <TextView 
            android:id="@+id/dueListItem"
            android:layout_width="wrap_content"
            android:lines="1"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="@string/dueListing"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
        <TextView
            android:id="@+id/dueDate"
            android:layout_toRightOf="@id/dueListItem"
            android:layout_marginLeft="2dp"
            android:layout_width="90dp"
            android:lines="1"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="HEHEHE"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
        <ImageView
            android:id="@+id/starMark"
            android:layout_alignRight="@id/detailsLayout"
            android:layout_toRightOf="@id/dueDate"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="2dp"
            android:layout_height="15dp"
            android:layout_width="15dp"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            android:src="@drawable/list_starred"
            android:visibility="invisible"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
    </RelativeLayout>
</LinearLayout>
<ImageView 
        android:id="@+id/contactPic"
        android:layout_alignParentRight="true"
        android:layout_height="48dp"
        android:layout_width="48dp"
        android:background="#FFF"
        android:layout_margin="10dp"
        android:layout_centerVertical="true"
        android:scaleType="fitCenter"
        android:padding="3dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
    />
<ImageView 
        android:id="@+id/lentArrow"
        android:visibility="invisible"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/indicator_lent"
        android:layout_marginTop="42dp"
        android:layout_marginRight="1dp"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
</RelativeLayout>
</LinearLayout>

I realize there are a ton of questions regarding this issue, but the solution of setting every view in the row.xml to focusable="false" and focusableInTouchMode="false" do not work, nor does getting the ListView from the ListActivity and setting setItemsCanFocus(false).

Weirdly, when registered for a context menu, the long tap works. The regular tap though? Nope. I tried setting listeners like OnItemClickListener to no avail.

I read somewhere that I might be able to remedy this by overriding getView() in my Adapter? I'm not too sure how that works though. Note, I don't want to know what view the user has clicked; I just care about the list row being clicked to initiate the corresponding code in onListItemClick().

Maybe there's something in my row.xml that's all wrong? Or is it affected by the way I set my ListView's adapter (placed in onResume() instead of onCreate() to update information)?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:focusable="false"
  android:focusableInTouchMode="false">
<TextView style="?android:attr/listSeparatorTextViewStyle"
  android:id="@+id/listSectionHeader"
  android:layout_width = "fill_parent"
  android:layout_height="wrap_content"
  android:paddingTop="2dp"
  android:paddingBottom="2dp"
  android:paddingLeft="5dp"
  android:textColor="@android:color/white"
  android:visibility="gone"
  android:focusable="false"
  android:focusableInTouchMode="false"/>
<RelativeLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="70dp"
  android:background="@drawable/list_button"
  android:focusable="false"
  android:focusableInTouchMode="false"
  android:clickable="true"
  android:longClickable="true"
  >
<TextView android:id="@+id/itemID"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:textStyle="bold"
    android:layout_alignParentLeft="true"
    android:focusable="false"
    android:focusableInTouchMode="false"/>
<CheckBox 
    android:id="@+id/returnedCheckbox"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_margin="10dp"
    android:checked="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false"
    />
<LinearLayout
    android:layout_toRightOf="@id/returnedCheckbox"
    android:layout_margin="5dp"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:layout_width="180dp"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false">
    <TextView 
        android:id="@+id/stuffName"
        android:layout_width="fill_parent"
        android:lines="1"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:scrollHorizontally="true"
        android:text="Hey there"
        android:textSize="25sp"
        android:textColor="#FFF"
        android:shadowColor="#000"
        android:focusable="false"
        android:focusableInTouchMode="false"
    />
    <RelativeLayout
        android:id="@+id/detailsLayout"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false">
        <TextView 
            android:id="@+id/dueListItem"
            android:layout_width="wrap_content"
            android:lines="1"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="@string/dueListing"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
        <TextView
            android:id="@+id/dueDate"
            android:layout_toRightOf="@id/dueListItem"
            android:layout_marginLeft="2dp"
            android:layout_width="90dp"
            android:lines="1"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:text="HEHEHE"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
        <ImageView
            android:id="@+id/starMark"
            android:layout_alignRight="@id/detailsLayout"
            android:layout_toRightOf="@id/dueDate"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="2dp"
            android:layout_height="15dp"
            android:layout_width="15dp"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            android:src="@drawable/list_starred"
            android:visibility="invisible"
            android:focusable="false"
            android:focusableInTouchMode="false"/>
    </RelativeLayout>
</LinearLayout>
<ImageView 
        android:id="@+id/contactPic"
        android:layout_alignParentRight="true"
        android:layout_height="48dp"
        android:layout_width="48dp"
        android:background="#FFF"
        android:layout_margin="10dp"
        android:layout_centerVertical="true"
        android:scaleType="fitCenter"
        android:padding="3dp"
        android:focusable="false"
        android:focusableInTouchMode="false"
    />
<ImageView 
        android:id="@+id/lentArrow"
        android:visibility="invisible"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/indicator_lent"
        android:layout_marginTop="42dp"
        android:layout_marginRight="1dp"
        android:focusable="false"
        android:focusableInTouchMode="false"/>
</RelativeLayout>
</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

远昼 2024-12-06 09:27:29

Onlistitemclick 永远不会被调用,因为列表项中有一个可单击的视图。删除复选框并查看是否能够获得点击次数。

还有一种替代方法可以直接在项目布局中使用复选框。如果适合您的需要,请使用 android:choiceMode="multipleChoice" 作为列表视图。

The Onlistitemclick will never be called because there is a clickable view in your list item. Remove the checkbox and see if you are able to get the clicks.

There is an alternative to using using a checkbox directly in your item layout. Use android:choiceMode="multipleChoice" for listview if it suits your needs.

等待我真够勒 2024-12-06 09:27:29

查看此链接,了解如何创建自定义适配器:

http://android-er.blogspot.com/2010/06/using-convertview-in-getview-to-make.html

在 Override getView() 方法中,您可以设置这row.setOnClickListener() 来执行单击该列表项时需要执行的操作。

理想情况下,您可以使用 ConvertView 来填充 ViewHolder 类,这样您就不会在创建列表项时重新创建列表项,但这是一个不同的问题。

编辑

下面是扩展 SimpleCursorAdapter 的精简自定义实现:

public class CatchCursorAdapter extends SimpleCursorAdapter {

    private final Context mContext;
    private final Cursor mCursor;
    private final int layout;
    private final LayoutInflater inflater;

    private final class ViewHolder {
        public ImageView catchImage;
        public ImageView catchStar;
        public TextView catchSpecies;
    }

    public CatchCursorAdapter(Context context, int layout, Cursor cursor) {
        super(context, layout, cursor, new String[] { }, new int[] { });

        this.mContext = context;
        this.mCursor = cursor;
        this.inflater = LayoutInflater.from(context);
        this.layout = layout;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(layout, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.catchImage = (ImageView)convertView.findViewById(R.id.catch_image);
            viewHolder.catchSpecies = (TextView)convertView.findViewById(R.id.catch_species);
            viewHolder.catchStar = (ImageView)convertView.findViewById(R.id.catch_starfavorite);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        convertView.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Perform an action when this list item is clicked
            }
        });
        viewHolder.catchStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //You can also have buttons within your list item that can be clicked
                //independently of the parent list item.
            }
        });

        //Set the rest of your views

        return convertView;
    }
}

Take a look at this link in how to create a custom adapter:

http://android-er.blogspot.com/2010/06/using-convertview-in-getview-to-make.html

In the Override getView() method, you could just set the row.setOnClickListener() to do what you needed to do when that list item is clicked.

Ideally, you would use convertView to fill a ViewHolder class so that you aren't recreating list items when they have already been created, but that's a different question.

Edit

Here's a slimmed down custom implementation of extending a SimpleCursorAdapter:

public class CatchCursorAdapter extends SimpleCursorAdapter {

    private final Context mContext;
    private final Cursor mCursor;
    private final int layout;
    private final LayoutInflater inflater;

    private final class ViewHolder {
        public ImageView catchImage;
        public ImageView catchStar;
        public TextView catchSpecies;
    }

    public CatchCursorAdapter(Context context, int layout, Cursor cursor) {
        super(context, layout, cursor, new String[] { }, new int[] { });

        this.mContext = context;
        this.mCursor = cursor;
        this.inflater = LayoutInflater.from(context);
        this.layout = layout;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(layout, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.catchImage = (ImageView)convertView.findViewById(R.id.catch_image);
            viewHolder.catchSpecies = (TextView)convertView.findViewById(R.id.catch_species);
            viewHolder.catchStar = (ImageView)convertView.findViewById(R.id.catch_starfavorite);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder)convertView.getTag();
        }
        convertView.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Perform an action when this list item is clicked
            }
        });
        viewHolder.catchStar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //You can also have buttons within your list item that can be clicked
                //independently of the parent list item.
            }
        });

        //Set the rest of your views

        return convertView;
    }
}
梦中楼上月下 2024-12-06 09:27:29

我不需要对我的适配器做任何事情;由于某种原因,onListItemClick() 奇迹般地自行工作了。我现在更摸不着头脑了。

顺便说一句,我扩展了 CursorAdapter,而不是 SimpleCursorAdapter,我知道这是有点不同的。据我所知,CursorAdapter扩展只需要重写newView()和bindView()而不是基于此的通常的getView() a href="http://www.workreloaded.com/2011/02/android-extending-cursoradapter-for-custom-listview/" rel="nofollow noreferrer">http://www.workreloaded.com/2011/02/android-extending-cursoradapter-for-custom-listview/。

I didn't have to do anything to my adapter; onListItemClick() just miraculously worked on its own for some reason. I'm scratching my head even more right now.

Incidentally, I extended CursorAdapter, not SimpleCursorAdapter, which I understand is sort of different. From what I read, CursorAdapter extension requires only overriding the newView() and bindView() instead of the usual getView() based on this http://www.workreloaded.com/2011/02/android-extending-cursoradapter-for-custom-listview/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文