我花了两(())天来弄清楚我的问题。
我ListView
和onItemClickListener
都有onScrollListner
。如果我实施onScrollListner
,则不会触发任何ItemClick
操作。
我在这里看到了这个问题:setOnScrollListener with setOnItemClickListener而这里:Android: cannot set both: setOnItemClickListener setOnScrollListener?没有真正的解决方案。
我还注意到(在模拟器中)当选择其中一行(在ListView
中)时,ItemClick
会触发(我假设它在非触摸模式下工作)。< / p>
非常令人沮丧,有人能想到解决方案吗?
一些代码:
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
}
}