我在事件处理方面遇到小问题,我有List视图自定义适配器数据,每行都有日期,标题,价格。当我点击行时我需要显示详细信息页面,但是当我长按价格,日期或标题时,我需要对列表视图进行排序。我需要在Long Press上使用手势。请参考以下代码我尝试过的内容。
自定义适配器视图
public EventAdapterView(Context context, List<EventUtil> eventList) {
this.mContext = context;
this.mEventUtil = eventList;
mLayoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new DrawableManager();
}
@SuppressLint("DefaultLocale")
@SuppressWarnings("deprecation")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mView = convertView;
EventUtil eventUtil = mEventUtil.get(position);
mView = mLayoutInflater.inflate(R.layout.row_event_adapter, null);
TextView eventTitleView = (TextView) mView
.findViewById(R.id.list_view_event_title);
TextView eventDescView = (TextView) mView
.findViewById(R.id.list_view_event_location);
TextView eventDateView = (TextView) mView
.findViewById(R.id.list_view_event_price);
// final MyGestureDetector myGestureDetector= new MyGestureDetector();
// new ImageFeach().execute(mEventUtil.getEvent_Image_Url());
eventTitleView.setText(eventUtil.getEvent_Title());
// event title sorting
eventTitleView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
//if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
@Override
public int compare(EventUtil obje1,
EventUtil obje2) {
return obje1
.getEvent_Title()
.compareTo(
obje2.getEvent_Title());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
// event location sorting
eventDescView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
//if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
@Override
public int compare(EventUtil event1,
EventUtil event2) {
return event1
.getEvent_location()
.compareTo(
event2.getEvent_location());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
// event price sorting
eventDateView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
// if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
@Override
public int compare(EventUtil event1,
EventUtil event2) {
return event1
.getEvent_Price()
.compareTo(
event2.getEvent_Price());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
// event date sorting
ImageView dateImageView = (ImageView) mView
.findViewById(R.id.list_view_event_date);
dateImageView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
new MyGestureDetector() {
public void onLongPress(MotionEvent event1) {
//if (event1.getAction() == MotionEvent.ACTION_DOWN) {
Collections.sort(mEventUtil,
new Comparator<EventUtil>() {
@Override
public int compare(EventUtil event1,
EventUtil event2) {
return event2
.getEvent_Date()
.compareTo(
event1.getEvent_Date());
}
});
notifyDataSetChanged();
//}
};
}.onLongPress(event);
return true;
}
});
MyGestureDetector Class
public class MyGestureDetector extends SimpleOnGestureListener {
@Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
}
}
帮助我......谢谢
答案 0 :(得分:3)
我认为,没有必要使用SimpleOnGestureListener
。
在自定义适配器视图中:
首先,
eventTitleView.setFocusable(false);
eventTitleView.setFocusableInTouchMode(false);
eventTitleView.setLongClickable(true);
eventDescView.setFocusable(false);
eventDescView.setFocusableInTouchMode(false);
eventDescView.setLongClickable(true);
eventDateView.setFocusable(false);
eventDateView.setFocusableInTouchMode(false);
eventDateView.setLongClickable(true);
然后,
只需在setOnLongClickListener (View.OnLongClickListener l)
,eventTitleView
和eventDescView
上设置eventDateView
即可,
eventTitleView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//do your sorting stuff here
}
});
eventDescView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//do your sorting stuff here
}
});
eventDateView.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//do your sorting stuff here
}
});
希望这会对你有所帮助。
答案 1 :(得分:0)
为什么你需要覆盖onlongpress?
你知道这个功能默认可用吗?
只需将价格,日期或标题放在同一个布局上,并为每个OnLongPressListener提供您想要的操作..:
实施例: http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/
答案 2 :(得分:0)
ListView.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// YOUR SORTING CODE HERE
return false;
}
});
答案 3 :(得分:0)
我已经完成了这个onLongClickListener
。使用这个界面很容易。你需要做
Class YourClass implements onLongClickListener{
public boolean onLongClick(View arg0){
// Event generated when user have long pressed the screen
return false; // If you do not want the event to keep occuring again and again
}