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

问题描述 投票:1回答:3

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

很奇怪,当注册上下文菜单时,长按有效。虽然经常水龙头?不。我尝试将像OnItemClickListener这样的侦听器设置为无效。

我读到某个地方可以通过重写Adapter中的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>
android android-listview listactivity android-cursoradapter onclicklistener
3个回答
1
投票

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

除了在项目布局中直接使用复选框之外,还有一种选择。如果需要,请使用android:choiceMode="multipleChoice"进行列表查看。


0
投票

请参阅此链接,了解如何创建自定义适配器:

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;
    }
}

0
投票

我不必对适配器执行任何操作; onListItemClick()出于某种原因奇迹般地独自工作。我现在更要挠头。

顺便说一句,我扩展了CursorAdapter,而不是SimpleCursorAdapter,据我了解有些不同。据我了解,CursorAdapter扩展名只需要覆盖newView()bindView(),而不是基于此http://www.workreloaded.com/2011/02/android-extending-cursoradapter-for-custom-listview/的常规getView()。

© www.soinside.com 2019 - 2024. All rights reserved.