【问题标题】:How to get the position of the list item in onGesturePerformed method?如何在 onGesturePerformed 方法中获取列表项的位置?
【发布时间】:2012-05-04 19:45:52
【问题描述】:

Here 是 Android 文档中的一个很好的教程,其中包含解释 GestureListview 的源代码。但我找不到如何在 onGesturePerformed(GestureOverlayView overlay, Gesture gesture) 方法中获取列表项位置?

请查看代码中带有 ? 的注释标记以了解我的查询。谢谢。

public class GesturesListActivity extends ListActivity implements OnGesturePerformedListener {
private GestureLibrary mLibrary;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Populate the activity with the names of our contacts
    Cursor query = managedQuery(Contacts.People.CONTENT_URI,
            new String[] { Contacts.People._ID, Contacts.People.DISPLAY_NAME },
            null, null, Contacts.People.DEFAULT_SORT_ORDER);

    ListAdapter adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1, query,
            new String[] { Contacts.People.DISPLAY_NAME },
            new int[] { android.R.id.text1 });

    setListAdapter(adapter);

    mLibrary = GestureLibraries.fromRawResource(this, R.raw.actions);
    if (!mLibrary.load()) {
        finish();
    }

    GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
    gestures.addOnGesturePerformedListener(this);
}

public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
    ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
    if (predictions.size() > 0) {
        if (predictions.get(0).score > 1.0) {
            String action = predictions.get(0).name;
            if ("action_add".equals(action)) {
                Toast.makeText(this, "Adding a contact", Toast.LENGTH_SHORT).show();                
            } else if ("action_delete".equals(action)) {
                Toast.makeText(this, "Removing a contact", Toast.LENGTH_SHORT).show();




                //How to get the specific position in the list to remove the contact on which the gesture event took place?




            } else if ("action_refresh".equals(action)) {
                Toast.makeText(this, "Reloading contacts", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

}

【问题讨论】:

    标签: android android-listview android-gesture


    【解决方案1】:

    我们可以覆盖 protected void onListItemClick(ListView l, View v, int position, long id)

    这样这将获得您单击的列表项的视图

    【讨论】:

    • 但是当点击项目时我不需要它,而是当用户以预定义的方向在项目上滑动手指时的位置。 onListItemClick 将具有不同的功能-例如它将显示详细的联系人。向左滑动手指将删除联系人。
    猜你喜欢
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多