下面列出了怎么用android.widget.AdapterView.OnItemLongClickListener的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(R.string.history_of_spidey_scans);
mDb = DatabaseHelper.getInstance(this);
List<Scan> scans = mDb.getAllScans();
mListView = getListView();
//add on long click listener to start action mode
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
onListItemCheck(position);
return true;
}
});
// use the SimpleCursorAdapter to show the
// elements in a ListView
ScanAdapter adapter = new ScanAdapter(this,scans);
setListAdapter(adapter);
}
/**
* Callback to listener when date is valid (not disable, not outside of
* min/max date)
*
* @return
*/
private OnItemLongClickListener getDateItemLongClickListener() {
dateItemLongClickListener = new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
DateTime dateTime = dateInMonthsList.get(position);
if (caldroidListener != null) {
if ((minDateTime != null && dateTime.lt(minDateTime))
|| (maxDateTime != null && dateTime.gt(maxDateTime))
|| (disableDates != null && disableDates
.indexOf(dateTime) != -1)) {
return false;
}
Date date = CalendarHelper.convertDateTimeToDate(dateTime);
caldroidListener.onLongClickDate(date, view);
}
return true;
}
};
return dateItemLongClickListener;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ListView lv = getListView();
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
ClipboardManager clipboard = (ClipboardManager)
getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Log Entry", ((TextView) view).getText());
clipboard.setPrimaryClip(clip);
Toast.makeText(getActivity(), R.string.copied_entry, Toast.LENGTH_SHORT).show();
return true;
}
});
}
private void initOrganizations()
{
listOrganizations = informaCam.installedOrganizations.listOrganizations();
organizationsHolder.setOnItemLongClickListener(new OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int viewId, long l)
{
IOrganization org = listOrganizations.get((int) l);
((HomeActivityListener) a).getContextualMenuFor(org);
return true;
}
});
listOrganizationsAdapter = new OrganizationsListAdapter(listOrganizations);
organizationsHolder.setAdapter(listOrganizationsAdapter);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// View management
mDualPane = getResources().getBoolean(R.bool.use_dual_panes);
// Modify list view
ListView lv = getListView();
lv.setVerticalFadingEdgeEnabled(true);
// lv.setCacheColorHint(android.R.color.transparent);
if (mDualPane) {
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setItemsCanFocus(false);
} else {
lv.setChoiceMode(ListView.CHOICE_MODE_NONE);
lv.setItemsCanFocus(true);
}
// Map long press
lv.setLongClickable(true);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> ad, View v, int pos, long id) {
turnOnActionMode();
getListView().setItemChecked(pos, true);
mMode.invalidate();
return true;
}
});
}
/**
* Creates and returns a listener, which allows to observe when the items of a bottom sheet have
* been long-clicked.
*
* @return The listener, which has been created, as an instance of the type {qlink
* OnItemLongClickListener}
*/
private OnItemLongClickListener createItemLongClickListener() {
return new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> parent, final View view,
final int position, final long id) {
if (!rootView.isDragging() && !rootView.isAnimationRunning() &&
itemLongClickListener != null) {
int index = position;
if (adapter.containsDividers()) {
for (int i = position; i >= 0; i--) {
if (adapter.getItem(i) == null ||
(adapter.getItem(i) instanceof Divider &&
i % adapter.getColumnCount() > 0)) {
index--;
}
}
}
return itemLongClickListener
.onItemLongClick(parent, view, index, getId(position));
}
return false;
}
};
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
noteCursorAdapter = new NoteCursorAdapter(this);
setListAdapter(noteCursorAdapter);
loaderCallback = new NoteLoaderCallback(this, noteCursorAdapter, NOTE_LOADER_ID);
getLoaderManager().initLoader(NOTE_LOADER_ID, null, loaderCallback);
getListView().setFastScrollEnabled(true);
getListView().setLongClickable(true);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> adapterView, final View view, final int postition, final long id) {
Cursor cursor = (Cursor) noteCursorAdapter.getItem(postition);
int noteId = cursor.getInt(DB.NoteTable._ID.columnIndex());
if (noteService.delete(noteId)) {
getLoaderManager().getLoader(NOTE_LOADER_ID).forceLoad();
Toast.makeText(NotesActivity.this, R.string.delete_success, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(NotesActivity.this, R.string.delete_fail, Toast.LENGTH_LONG).show();
}
return true;
}
});
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Current position should survive screen rotations.
if (savedInstanceState != null) {
mCategory = savedInstanceState.getInt("category");
mCurPosition = savedInstanceState.getInt("listPosition");
}
populateTitles(mCategory);
ListView lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setCacheColorHint(Color.TRANSPARENT);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@SuppressLint("DefaultLocale")
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
final String title = (String) ((TextView) v).getText();
// Set up clip data with the category||entry_id format.
final String textData = String.format("%d||%d", mCategory, pos);
ClipData data = ClipData.newPlainText(title, textData);
v.startDrag(data, new MyDragShadowBuilder(v), null, 0);
return true;
}
});
selectPosition(mCurPosition);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Current position should survive screen rotations.
if (savedInstanceState != null) {
mCategory = savedInstanceState.getInt("category");
mCurPosition = savedInstanceState.getInt("listPosition");
}
populateTitles(mCategory);
ListView lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setCacheColorHint(Color.TRANSPARENT);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
final String title = (String) ((TextView) v).getText();
// Set up clip data with the category||entry_id format.
final String textData = String.format("%d||%d", mCategory, pos);
ClipData data = ClipData.newPlainText(title, textData);
v.startDrag(data, new MyDragShadowBuilder(v), null, 0);
return true;
}
});
selectPosition(mCurPosition);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//Current position should survive screen rotations.
if (savedInstanceState != null) {
mCategory = savedInstanceState.getInt("category");
mCurPosition = savedInstanceState.getInt("listPosition");
}
populateTitles(mCategory);
ListView lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setCacheColorHint(Color.TRANSPARENT);
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
final String title = (String) ((TextView) v).getText();
// Set up clip data with the category||entry_id format.
final String textData = String.format("%d||%d", mCategory, pos);
ClipData data = ClipData.newPlainText(title, textData);
v.startDrag(data, new MyDragShadowBuilder(v), null, 0);
return true;
}
});
selectPosition(mCurPosition);
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
setListAdapter(adapter);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
if (mActionMode != null) {
return false;
}
selectedItem = position;
// TODO Start the CAB using the ActionMode.Callback defined
// above
view.setSelected(true);
return true;
}
});
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
setListAdapter(adapter);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
if (mActionMode != null) {
return false;
}
selectedItem = position;
// Start the CAB using the ActionMode.Callback defined above
mActionMode = MyListActivityActionbar.this
.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android",
"iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu",
"Windows7", "Max OS X", "Linux", "OS/2",
"Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu",
"Windows7", "Max OS X",
"Linux", "OS/2" };
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
setListAdapter(adapter);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (mActionMode != null) {
return false;
}
selectedItem = position;
// Start the CAB using the ActionMode.Callback defined above
MyListActivityActionbar.this.startActionMode(MyListActivityActionbar.this);
view.setSelected(true);
return true;
}
});
}
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux",
"OS/2", "Ubuntu", "Windows7", "Max OS X", "Linux", "OS/2" };
MySimpleArrayAdapter adapter = new MySimpleArrayAdapter(this, values);
setListAdapter(adapter);
getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
if (mActionMode != null) {
return false;
}
selectedItem = position;
// Start the CAB using the ActionMode.Callback defined above
mActionMode = MyListActivityActionbar.this
.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
}
private void initNotifications() {
listNotifications = informaCam.notificationsManifest.sortBy(Models.INotificationManifest.Sort.COMPLETED);
notificationsHolder.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int viewId, long l)
{
INotification notification = informaCam.notificationsManifest.notifications.get((int) l);
((HomeActivityListener) a).getContextualMenuFor(notification);
return true;
}
});
listNotificationsAdapter = new NotificationsListAdapter(listNotifications);
notificationsHolder.setAdapter(listNotificationsAdapter);
if (listNotifications != null && listNotifications.size() > 0)
{
notificationsNoNotifications.setVisibility(View.GONE);
return;
}
notificationsNoNotifications.setVisibility(View.VISIBLE);
}
public void setOnItemLongClickListener(OnItemLongClickListener listener) {
mList.setOnItemLongClickListener(listener);
}
public void setOnItemLongClickListener(OnItemLongClickListener listener) {
mList.setOnItemLongClickListener(listener);
}
@Override
public void setOnItemLongClickListener(
OnItemLongClickListener listener) {
this.mOnItemLongClickListener = listener;
super.setOnItemLongClickListener(this);
}
public void setOnItemLongClickListener(OnItemLongClickListener listener) {
mList.setOnItemLongClickListener(listener);
}
public OnItemLongClickListener getOnItemLongClickListener() {
return onItemLongClickListener;
}
public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
this.onItemLongClickListener = onItemLongClickListener;
}
public void setOnItemLongClickListener(OnItemLongClickListener listener) {
mOnItemLongClickListener = listener;
}
@Override
public void setOnItemLongClickListener(
OnItemLongClickListener listener) {
this.mOnItemLongClickListener = listener;
super.setOnItemLongClickListener(this);
}
public void setOnItemLongClickListener(OnItemLongClickListener listener) {
mOnItemLongClickListener = listener;
}
@Override
public void setOnItemLongClickListener(
OnItemLongClickListener listener) {
this.mOnItemLongClickListener = listener;
super.setOnItemLongClickListener(this);
}
public void setOnItemLongClickListener(OnItemLongClickListener listener) {
mOnItemLongClickListener = listener;
}
@Override
public void setOnItemLongClickListener(
android.widget.AdapterView.OnItemLongClickListener listener) {
this.mOnItemLongClickListener = listener;
super.setOnItemLongClickListener(this);
}
/**
* Sets the listener, which should be notified, when an item of the bottom sheet has been
* long-clicked.
*
* @param listener
* The listener, which should be set, as an instance of the type {@link
* OnItemLongClickListener} or null, if no listener should be notified
* @return The builder, the method has been called upon, as an instance of the class {@link
* Builder}
*/
public final Builder setOnItemLongClickListener(
@Nullable final OnItemLongClickListener listener) {
bottomSheet.setOnItemLongClickListener(listener);
return this;
}
/**
* Sets the listener, which should be notified, when an item of the bottom sheet has been
* long-clicked.
*
* @param listener
* The listener, which should be set, as an instance of the type {@link
* OnItemLongClickListener} or null, if no listener should be notified
*/
public final void setOnItemLongClickListener(@Nullable final OnItemLongClickListener listener) {
this.itemLongClickListener = listener;
}
/**
* Register a callback to be invoked when an item in this AdapterView has been clicked and held
*
* @param listener
* The callback that will run
*/
public final void setOnItemLongClickListener(final OnItemLongClickListener listener) {
getView().setOnItemLongClickListener(listener);
}