0

It took me two(!) days to figure out my problem. I have ListView with both onItemClickListener and onScrollListner. If I implement the onScrollListner then no ItemClick actions are fired.

I saw this problem here: setOnScrollListener with setOnItemClickListener and here:Android: cannot set both: setOnItemClickListener setOnScrollListener? with no real solution.

I have also noticed (in the emulator) that when one of the rows (in the ListView) is selected then ItemClick does fire (I assume that it works in not touch mode).

Very frustrating, can anyone think of a solution?

Some code:

Custom ListView

public class CustomHomeListView extends ListView implements android.widget.AdapterView.OnItemClickListener, OnScrollListener, IReceiver {

    public static final String TAG = CustomHomeListView.class.getSimpleName();

    private static Context mContext;
    List<DiscussionModel> mDiscussionsArray = new ArrayList<DiscussionModel>();

    private DiscussionsCustomAdapter mAdapter;
    private boolean mIsLoading;
    private int mCurrentMinDiscussionRowIndex;
    private View mFooterView;
    private int mFirstVisiblePosition;
    private int mSelectedItemPosition;
    private int mDividerHeight;
    private int mSavedPositionInsideListView;

    public MyResultReceiver mReceiver;

    public void onCreate() {

        // for computing scroll position to an item after data fetch
        mDividerHeight = getDividerHeight();
        mSavedPositionInsideListView = 0;

        // prepare ResultReceiver for informing that puller service is done
        mReceiver = new MyResultReceiver(new Handler());
        mReceiver.setReceiver(this);

        mCurrentMinDiscussionRowIndex = -1; // default value for retrieving most
        // recent
        // posts from web

        mAdapter = new DiscussionsCustomAdapter(mContext);
        CrowdietApplication cda = (CrowdietApplication) mContext.getApplicationContext();
        mAdapter.setCurrentUserUid(cda.getUserId());
        setAdapter(mAdapter);

        setOnScrollListener(this);
        setOnItemClickListener(this);

        mFooterView = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.home_list_view_footer, null);

        //setCustomHomeListViewOnScrollListener();

        pullDiscussions();
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        DiscussionModel discussion = mDiscussionsArray.get(position);

        Intent intent = new Intent(mContext, DiscussionActivity.class);
        intent.putExtra(CONSTS_DISCUSSIONS.DISCUSSION_ID_FROM_CALLING_ACTIVITY, discussion.getDiscussionId());
                mContext.startActivity(intent);
    }

    public void refresh() {
        mCurrentMinDiscussionRowIndex = -1;
        pullDiscussions();
    }

    private void pullDiscussions() {
        mIsLoading = true;

        // preparing puller service
        Intent intent = new Intent(mContext, PullDiscussionsListIntentService.class);
        intent.putExtra(CONSTS_APP_SIGNALS.MIN_DISCUSSION_ROW_INDEX_TO_FETCH, mCurrentMinDiscussionRowIndex);
        intent.putExtra(CONSTS_APP_SIGNALS.RECEIVER_TAG, mReceiver);
        mContext.startService(intent);

    }

    private void refreshListFromDatabase() {
        DbDiscussions dbDiscussions = new DbDiscussions(mContext);
        Cursor discussionsCursor;

        discussionsCursor = dbDiscussions.getDiscussions();
        if (discussionsCursor != null && discussionsCursor.getCount() > 0) {
            prepareDiscussionsArray(discussionsCursor);
            mCurrentMinDiscussionRowIndex = dbDiscussions.getMinDiscussionRowIndex();
            mAdapter.setData(mDiscussionsArray);
            mAdapter.notifyDataSetChanged();
            mIsLoading = false;
        }
        discussionsCursor.close();
        dbDiscussions.destroy();
    }

    private void prepareDiscussionsArray(Cursor discussionsCursor) {
        mDiscussionsArray.clear();
        int noOfReturnedDiscussions = discussionsCursor.getCount();

        discussionsCursor.moveToFirst();
        for (int i = 0; i < noOfReturnedDiscussions; i++) {
            DiscussionModel discussion = new DiscussionModel();
            discussion.setDiscussionId(discussionsCursor.getString(discussionsCursor.getColumnIndex(CONSTS_DISCUSSIONS.FIELD_DISCUSSION_ID)));
            discussion.setDiscussionTitle(discussionsCursor.getString(discussionsCursor.getColumnIndex(CONSTS_DISCUSSIONS.FIELD_DISCUSSION_TITLE)));
            discussion.setUsername(discussionsCursor.getString(discussionsCursor.getColumnIndex(CONSTS_DISCUSSIONS.FIELD_USERNAME)));

            mDiscussionsArray.add(discussion);
            discussionsCursor.moveToNext();
        }

    }

    private int calcItemPosition() {
        int posInListView = 0;
        for (int i = 0; i < mSelectedItemPosition - mFirstVisiblePosition - 1; i++) {
            View child = getChildAt(i);
            if (child == null) {
                break;
            }
            posInListView += child.getHeight() + mDividerHeight;
        }
        // Log.d(TAG, "measured height :" + posInListView);
        return posInListView;
    }

    public CustomHomeListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    private void prepareForMySetSelection() {
        if (mSelectedItemPosition > -1) {
            mSavedPositionInsideListView = calcItemPosition();
        }
    }

    private void mySetSelection() {
        if (mSelectedItemPosition > -1) {
            setSelectionFromTop(mSelectedItemPosition, mSavedPositionInsideListView);
        } else {
            setSelection(mFirstVisiblePosition);
        }
    }

    public void onReceiveResult(int resultCode, Bundle resultData) {
        prepareForMySetSelection(); // should be before
                                    // RefreshListFromDatabase() so that
                                    // actual position inside adapter is
                                    // saved
        refreshListFromDatabase();
        setAdapter(mAdapter);
        mySetSelection();
        // }
        removeFooterView(mFooterView);
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        mFirstVisiblePosition = getFirstVisiblePosition();
        mSelectedItemPosition = getSelectedItemPosition();

        if (mAdapter != null && mAdapter.getCount() > 0) {
            int l = visibleItemCount + firstVisibleItem;
            if (l >= totalItemCount && !mIsLoading) {
                addFooterView(mFooterView);
                pullDiscussions();
            }
        }
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub

    }

}
Community
  • 1
  • 1
dsb
  • 2,347
  • 5
  • 26
  • 43
  • 1
    I can't imagine this doesn't work as I've used the combination of listeners in the past. Can you provide some of the code you're working with? – Paul Lammertsma Mar 30 '14 at 13:53
  • both listeners work without any problems, unless you are doing some tricky things... – pskink Mar 30 '14 at 14:00
  • I added some code. nothing tricky there, I think. I'm really clueless. Moreover, when I'm working with data set from local computer it works fine, when I work with (much samller) remote db the problem appears. – dsb Mar 30 '14 at 14:36
  • Well, I removed adding and removing the footer (which only displays progressView) and it works. Still, I can't figure out what's going on here. – dsb Mar 30 '14 at 14:53

0 Answers0