Android: 在 ExpandableListView 中接收 id onItemLongClickListener 时出错

Android: Error receiving id onItemLongClickListener in ExpandableListView

由于我的必需品发生变化,我不得不将我的 ListFragment 更改为带有 ExpandableListViewFragment,我很容易从前者切换到后者,除了OnItemLongClickListener.

对不同行的实际点击工作正常,但是当我长按任何行时,id 值总是超出我的数据库范围。我正在使用 CursorTreeAdapter 来填充 ExpandableListView 并且它工作正常,所以我不知道我在长按过程中做错了什么。

我会post相关代码

//片段

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.v(TAG, "onCreateView");
    View view = inflater.inflate(R.layout.activity_list_layout, container,
            false);
    mAdapter = new ExpandableActivityListCursorAdapter(getActivity(), null);
    mExpListView = (ExpandableListView) view.findViewById(R.id.exp_list);
    mExpListView.setAdapter(mAdapter);

//...
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    Log.v(TAG, "onLoadFinished");
        if (cursor != null) {
        mAdapter.changeCursor(cursor);
       }
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    Log.v(TAG, "onLoaderReset");
    mAdapter.changeCursor(null);
}

if (mExpListView != null) {
        mExpListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                int res = DBActivity.changeHighlightedActivity(getActivity().getContentResolver(), id); // <-- Not working because id has values like 12323487364763 or -9843627183764 (not valid as IDs in my DB)

                updateHighlightedActivityDB(id, res);

                Toast.makeText(getActivity(), (res == 0 ? getString(R.string.activity_no_longer_highlighted) :
                        getString(R.string.activity_now_highlighted)), Toast.LENGTH_LONG).show();
                return true;
            }
        });


  mExpListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                Intent i = new Intent(v.getContext(), ActivityPagerActivity.class);

                Uri chatUri = ContentUris.withAppendedId(
                        EPCTrackerContentProvider.CONTENT_URI_MESSAGE_PER_ACTIVITY, id); //<-- id is having the right values (1 to 500 more or less)

                //....
                startActivity(i);
                return true;
            }
        });

}

//适配器

@Override
public long getGroupId(int groupPosition)
{
    if(getCursor() == null)
        return -1;

    Cursor group_cur = getCursor();

    if(!group_cur.moveToPosition(groupPosition))
    {
        throw new IllegalStateException("couldn't move group cursor to position " + groupPosition);
    }

    return group_cur.getLong(group_cur.getColumnIndexOrThrow(DBActivity.COLUMN_ACTIVITY_ID));

}

@Override
public long getChildId(int groupPosition, int childPosition)
{
    if(getCursor() == null)
        return -1;

    Cursor group_cur = getCursor();

    if(!group_cur.moveToPosition(groupPosition))
    {
        throw new IllegalStateException("couldn't move group cursor to position " + groupPosition);
    }

    Cursor child_cur = getChildrenCursor(group_cur);

    if(child_cur == null)
        return -1;

    if(!child_cur.moveToPosition(childPosition))
    {
        throw new IllegalStateException("couldn't move child cursor to position " + childPosition);
    }

    return child_cur.getLong(group_cur.getColumnIndexOrThrow(DBActivity.COLUMN_ACTIVITY_ID));
}

正如我之前提到的,当我点击组项目或子项目时,返回的 ID 是正确的。我错过了什么?

提前致谢。

编辑

我将添加 XML 定义

<ExpandableListView
        android:id="@+id/exp_list"
        android:layout_below="@id/layout_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:focusable="false" />

好吧,经过一些研究我没有发现这种行为的原因,但是我找到了解决问题的方法。我将从给定位置获取 ExpandableListView 的行位置,然后从中获取元素 id。这是代码:

mExpListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
                    final long packedPosition = ((ExpandableListView) parent).getExpandableListPosition(position);
                    int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
                    int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);

                    long id = mAdapter.getChildId(groupPosition,childPosition));

                    //...
                    // Return true as we are handling the event.
                    return true;
                }
                else if(ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_GROUP)
                {
                    final long packedPosition = ((ExpandableListView) parent).getExpandableListPosition(position);
                    int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);

                    long id = mAdapter.getGroupId(groupPosition));

                    //...
                    return true;
            }
        });

希望对处于我这个位置的人有所帮助

干杯