首先,我初始化了我的listview
并添加了registerForContextMenu(my_list)
chatLists = (ListView)findViewById(R.id.chatlist);
chatLists.setAdapter(new ContactListAdapter(this));
registerForContextMenu(chatLists);
然后我添加了OnCreateContextMenu
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.chatlist) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.list_context, menu);
}
}
但是在运行应用程序之后..当我长按任何列表项时,它没有显示任何上下文菜单..
答案 0 :(得分:0)
您应该在上下文菜单中注册LISTVIEW。
这是来源。
代表registerForContextMenu(lv);
:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
if (v.getId() == R.id.lv) {
ListView lv = (ListView) v;
AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
YourObject obj = (YourObject) lv.getItemAtPosition(acmi.position);
menu.add("One");
menu.add("Two");
menu.add("Three");
menu.add(obj.name);
}
}
要在长按期间访问所选项目:
NM_CUSTOMDRAW
答案 1 :(得分:0)
这里有一些你可以使用的示例代码:
`
public class NotificationListAdapter extends BaseAdapter {
private MainActivity context;
private Vector<MessageInboxBean> messagesList;
private DisplayImageOptions /* options, */ options1;
private ImageLoader imageLoader;
private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
private NotiItemHolder holder;
private boolean isHomeworkTab;
private LineColorPicker colorPicker;
private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>();
int msgTypeId;
public NotificationListAdapter(MainActivity context, int msgTypeId, Vector<MessageInboxBean> messagesList, DisplayImageOptions options, boolean isHomeworkTab) {
this.context = context;
this.messagesList = messagesList;
// this.options = options;
options1 = new DisplayImageOptions.Builder()//
.showImageOnLoading(R.drawable.default_teacher)//
.showImageForEmptyUri(R.drawable.default_teacher)//
.showImageOnFail(R.drawable.default_teacher)//
.cacheInMemory(true)//
.cacheOnDisk(true)//
.considerExifParams(true)//
.displayer(new RoundedBitmapDisplayer(0))//
.build();
this.isHomeworkTab = isHomeworkTab;
this.msgTypeId = msgTypeId;
imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(context));
if (this.messagesList == null) {
messagesList = getMessagesList(this.msgTypeId);
}
// if (this.messagesList.size() == 0) {
// messagesList = getMessagesList(this.msgTypeId);
// }
}
public void setNewSelection(int position, boolean value) {
mSelection.put(position, value);
notifyDataSetChanged();
}
public boolean isPositionChecked(int position) {
Boolean result = mSelection.get(position);
return result == null ? false : result;
}
public Set<Integer> getCurrentCheckedPosition() {
return mSelection.keySet();
}
public void removeSelection(int position) {
mSelection.remove(position);
notifyDataSetChanged();
}
public void clearSelection() {
mSelection = new HashMap<Integer, Boolean>();
notifyDataSetChanged();
}
public void selectAllItems() {
mSelection = new HashMap<Integer, Boolean>();
for (int i = 0; i < messagesList.size(); i++) {
mSelection.put(i, true);
}
notifyDataSetChanged();
}
public boolean isAllSelected() {
if (mSelection.size() == messagesList.size()) {
return true;
} else {
return false;
}
}
public void deleteSelectedItems() {
ArrayList<Integer> notiIdAray = new ArrayList<Integer>();
for (int i = 0; i < messagesList.size(); i++) {
Boolean result = mSelection.get(i);
boolean result1 = result == null ? false : result;
if (result1) {
notiIdAray.add(messagesList.get(i).getNotificationId());
}
}
((MessageInboxTable) AppDb.getInstance(context).getTableObject(MessageInboxTable.TABLE_NAME)).deleteData(notiIdAray, context);
updateMessagesList(msgTypeId);
notifyDataSetChanged();
}
@Override
public int getCount() {
return messagesList.size();
}
@Override
public Object getItem(int position) {
return messagesList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("deprecation")
@SuppressLint({ "NewApi", "InflateParams" })
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.notification_list_item, null);
holder = new NotiItemHolder();
holder.dueDate = (TextView) convertView.findViewById(R.id.dueDateTxt);
holder.date = (TextView) convertView.findViewById(R.id.date);
holder.day = (TextView) convertView.findViewById(R.id.day);
holder.title = (TextView) convertView.findViewById(R.id.title);
holder.message = (TextView) convertView.findViewById(R.id.message);
holder.subject = (TextView) convertView.findViewById(R.id.subject);
holder.doneStatus = (TextView) convertView.findViewById(R.id.doneStatus);
holder.sendersName = (TextView) convertView.findViewById(R.id.sendersName);
holder.notiDate = (TextView) convertView.findViewById(R.id.notiDate);
holder.sendersImg = (ImageView) convertView.findViewById(R.id.sendersImg);
holder.statusImg = (ImageView) convertView.findViewById(R.id.statusImg);
holder.star = (CheckBox) convertView.findViewById(R.id.star);
holder.doneLayout = (LinearLayout) convertView.findViewById(R.id.done_layout);
holder.endView = convertView.findViewById(R.id.endView);
convertView.setTag(holder);
} else {
holder = (NotiItemHolder) convertView.getTag();
}
MessageInboxBean bean = messagesList.get(position);
if (mSelection.get(position) != null) {
convertView.findViewById(R.id.cal_img_layout).setBackgroundResource(R.drawable.calender_check);
holder.dueDate.setVisibility(View.INVISIBLE);
holder.date.setVisibility(View.INVISIBLE);
holder.day.setVisibility(View.INVISIBLE);
} else {
if (bean.getDueDate().trim().length() > 0) {
convertView.findViewById(R.id.cal_img_layout).setBackgroundResource(R.drawable.calender);
holder.dueDate.setVisibility(View.VISIBLE);
holder.date.setVisibility(View.VISIBLE);
holder.day.setVisibility(View.VISIBLE);
holder.date.setText(getDueDate(bean.getDueDate()));
holder.day.setText(getDueDay(bean.getDueDate()));
} else {
convertView.findViewById(R.id.cal_img_layout).setBackgroundResource(R.drawable.calender_2);
holder.dueDate.setVisibility(View.INVISIBLE);
holder.date.setVisibility(View.INVISIBLE);
holder.day.setVisibility(View.INVISIBLE);
}
if (isHomeworkTab) {
holder.subject.setVisibility(View.VISIBLE);
holder.doneLayout.setVisibility(View.VISIBLE);
} else {
holder.subject.setVisibility(View.GONE);
holder.doneLayout.setVisibility(View.GONE);
}
}
holder.title.setText(bean.getTitle());
holder.message.setText(bean.getMessage());
if (isHomeworkTab) {
holder.subject.setText(getSubjectFromId(context, bean.getSubjectId()) == null ? "" : getSubjectFromId(context, bean.getSubjectId()));
String colr = getSubjectColorFromId(context, bean.getSubjectId());
if (colr != null) {
setColorToImage(holder.subject, 1, colr, false);
}
if (bean.getStatus().equals("0")) {
holder.doneStatus.setText("Not done");
holder.doneStatus.setTextColor(context.getResources().getColor(R.color.text_light_grey));
holder.statusImg.setImageResource(R.drawable.not_done);
} else {
holder.doneStatus.setText("Done");
holder.doneStatus.setTextColor(context.getResources().getColor(R.color.app_theme_color));
holder.statusImg.setImageResource(R.drawable.done);
}
}
holder.notiDate.setText(getMessageDate(bean.getMessageDate()));
if (bean.getStarred().equals("0")) {
holder.star.setChecked(false);
} else {
holder.star.setChecked(true);
}
holder.sendersName.setText("~" + bean.getSender());
// holder.notiDate
imageLoader.displayImage(bean.getSenderPicURL(), holder.sendersImg, options1, animateFirstListener);
holder.subject.setTag(position);
holder.star.setTag(position);
holder.doneLayout.setTag(position);
holder.subject.setOnClickListener(subjectClick);
holder.star.setOnCheckedChangeListener(starClick);
holder.doneLayout.setOnClickListener(doneClick);
if (position == getCount() - 1) {
holder.endView.setVisibility(View.VISIBLE);
} else {
holder.endView.setVisibility(View.GONE);
}
return convertView;
}
class NotiItemHolder {
TextView dueDate, date, day, title, message, subject, doneStatus, sendersName, notiDate;
ImageView sendersImg, statusImg;
CheckBox star;
LinearLayout doneLayout;
View endView;
}
`
这里是适配器:
{{1}}
}