如何使用游标适配器将SwipeListener添加到列表项

时间:2017-09-06 13:16:17

标签: android swipe-gesture

我尝试向listview添加左右滑动以创建调用意图和消息传递意图,如联系人应用: 向右滑动 - >呼叫 向左滑动 - >消息

OnSwipe,它应该调用一个意图

我尝试了很多方法,但我无法获得解决方案

以下是我的代码,用于生成列表查看项目(不包括滑动相关代码,因为我到目前为止使用库):

这是我的adpater课程

class ContactCursorAdapter extends CursorAdapter{

public ContactCursorAdapter(Context context, Cursor c) {
    super(context, c, 0);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {

    return LayoutInflater.from(context).inflate(R.layout.contacts_list_item, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {

    TextView nameTextView = (TextView) view.findViewById(R.id.text_view_name);
    ImageView profilePictureView = (ImageView) view.findViewById(R.id.image_person);

    int columnNameIndex = cursor.getColumnIndex(ContactEntry.COLUMN_PERSON_NAME);
    int columnNumberIndex = cursor.getColumnIndex(ContactEntry.COLUMN_CONTACT_NUMBER);
    int profilePictureIndex = cursor.getColumnIndex(ContactEntry.COLUMN_PROFILE_PICTURE);

    String contactName = cursor.getString(columnNameIndex);
    final String contactNumber = cursor.getString(columnNumberIndex);
    byte[] profilePicture = cursor.getBlob(profilePictureIndex);

    if (contactName.length() == 0) {
        nameTextView.setText(contactNumber);
    } else {
        nameTextView.setText(contactName);
    }
    if (profilePicture != null) {
        if (profilePicture.length > 3) {
            profilePictureView.setImageBitmap(BitmapFactory.decodeByteArray(profilePicture, 0, profilePicture.length));
        } else {
            profilePictureView.setImageResource(R.drawable.ic_person);
        }
    }
}

这是我的联系人列表项目文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="?android:listPreferredItemHeight"
    android:background="@drawable/selector_color">

    <ImageView
        android:id="@+id/image_person"
        android:layout_width="?android:attr/listPreferredItemHeight"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:contentDescription="@string/profile_picture"
        android:scaleType="centerCrop"
        android:src="@drawable/ic_person" />

    <TextView
        android:id="@+id/text_view_name"
        android:layout_width="match_parent"
        android:layout_height="?android:listPreferredItemHeight"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@id/image_person"
        android:layout_toRightOf="@id/image_person"
        android:ellipsize="marquee"
        android:fontFamily="sans-serif-light"
        android:gravity="center_vertical"
        android:maxLines="1"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white"
        tools:targetApi="jelly_bean" />
</RelativeLayout>

我只想使用CursorAdapter

0 个答案:

没有答案