我使用DragSortListView库。它工作正常 - 从我的列表视图中拖动和删除项目,但我也想单击ListView项目。当我设置OnClickListener它不起作用。可能是什么问题呢?抱歉我的英文不好
我的片段:
private DragSortListView listView;
private MakeSingleBetAdapter adapter;
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MyLog.d(TAG, "onCreateView");
BugSenseHandler.initAndStartSession(getActivity(), APP_Parameters.BugSense);
view = inflater.inflate(R.layout.fragment_make_single_bet,
container, false);
listView = (DragSortListView) view.findViewById(R.id.makeBetList);
listView.setRemoveListener(this);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), "Click: " + position, Toast.LENGTH_SHORT).show();
}
});
cursor = dbHelper.getAllBets();
adapter = new MakeSingleBetAdapter(getActivity(), cursor, 3, dbHelper);
listView.setAdapter(adapter);
return view;
}
@Override
public void remove(int which) {
Cursor cursorId = (Cursor) (listView.getItemAtPosition(which));
String id = cursorId.getString(cursor
.getColumnIndex(DBHelper.COL_BET_ID));
dbHelper.deleteBetById(id);
adapter.swapCursor(dbHelper.getAllBets());
}
我的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/ch.bettings"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical" >
....
<com.mobeta.android.dslv.DragSortListView
android:id="@+id/makeBetList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/View1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:cacheColorHint="@color/slidingMenu"
android:divider="#b5b5b5"
app:drag_enabled="true"
app:drag_start_mode="onMove"
app:remove_enabled="true"
app:remove_mode="flingRemove"
app:sort_enabled="false" />
....
</RelativeLayout>
如果我在我的适配器中添加ClickListener - 它工作正常,但我无法拖动列表项
答案 0 :(得分:2)
尝试将这些添加到列表视图行:
android:focusable="false"
android:focusableInTouchMode="false"
答案 1 :(得分:0)
这对我有用,我一直在单击列表中的项目及其触摸事件,因此我的代码如下。我只想执行Onclick而不是Ontouch只是重写该方法,什么也不做。
private DragSortListView list;
list = view.findViewById(android.R.id.list);
list.addHeaderView(footerView);
list.setFocusable(false);
list.setHeaderDividersEnabled(false);
list.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(getActivity(),"its a touch" , Toast.LENGTH_LONG).show();
Log.d("stack", "TOUCH EVENT");
return false;
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),"Position :"+position+" ListItem : " +id , Toast.LENGTH_LONG).show();
Log.i("edit recipe click", "clicked");
EditableIngredient ingredient = (EditableIngredient) parent.getItemAtPosition(position);
host.handleIngredientSelected(ingredient);
}
});
return view;
}
我的Xml看起来像
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mobeta.android.dslv.DragSortListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
app:drag_handle_id="@id/drag_handle"
app:float_background_color="@android:color/white"
/>
</FrameLayout>