下面列出了android.widget.ListView#getCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void scrollList(int scrollPosition)
{
if(scrollPosition > 0)
{
ListView lv = getListView();
if(lv.getFirstVisiblePosition() == 0)
{
int num = lv.getCount();
int sp = scrollPosition;
if(scrollPosition >= num)
sp = num - 1;
if(sp >= 0)
//lv.setSelection(sp);
lv.smoothScrollToPosition(sp);
}
}
}
public void selectItem(int num) {
final ListView commitList = (ListView) getView().findViewById(R.id.git_history_commit_list);
selectedItem = num;
int selection = num - commitList.getFirstVisiblePosition();
//Keep the selected commit on screen... with a little bit of breathing room
if (num < commitList.getFirstVisiblePosition() + 2) {
commitList.setSelection(num == 0 ? num : num - 1);
} else if (num > commitList.getLastVisiblePosition() - 2) {
commitList.setSelection(num == commitList.getCount() - 1 ? num : num + 1);
}
for (int i = 0; i < commitList.getCount(); i ++) {
View child = commitList.getChildAt(i);
if (child != null) {
child.setBackgroundColor(selection == i
? getResources().getColor(R.color.holo_select)
: getResources().getColor(android.R.color.transparent));
}
}
}
public static RowViewInfo getCurrentRowViewInfo(FileListViewFragment host, Object item)
{
if(host == null || host.isRemoving() || !host.isResumed())
return null;
ListView list = host.getListView();
if(list == null)
return null;
int start = list.getFirstVisiblePosition();
for(int i=start, j=list.getLastVisiblePosition();i<=j;i++)
if(j<list.getCount() && item == list.getItemAtPosition(i))
{
RowViewInfo rvi = new RowViewInfo();
rvi.view = list.getChildAt(i-start);
rvi.position = i;
rvi.listView = list;
return rvi;
}
return null;
}
private void actionModeDelete() {
ListView lv = getListView();
ArrayList<Long> checkedIds = new ArrayList<Long>();
for(int i = 0; i < lv.getCount(); i++) {
if(lv.isItemChecked(i)) {
long[] selectedIds = mAdapter.getCallIdsAtPosition(i);
for(long id : selectedIds) {
checkedIds.add(id);
}
}
}
if(checkedIds.size() > 0) {
String strCheckedIds = TextUtils.join(", ", checkedIds);
Log.d(THIS_FILE, "Checked positions ("+ strCheckedIds +")");
getActivity().getContentResolver().delete(SipManager.CALLLOG_URI, Calls._ID + " IN ("+strCheckedIds+")", null);
mMode.finish();
}
}
private void actionModeDialpad() {
ListView lv = getListView();
for(int i = 0; i < lv.getCount(); i++) {
if(lv.isItemChecked(i)) {
mAdapter.getItem(i);
String number = mAdapter.getCallRemoteAtPostion(i);
if(!TextUtils.isEmpty(number)) {
Intent it = new Intent(Intent.ACTION_DIAL);
it.setData(SipUri.forgeSipUri(SipManager.PROTOCOL_SIP, number));
startActivity(it);
}
break;
}
}
mMode.invalidate();
}
public boolean canTargetScrollVertically(int direction) {
ListView target = this.mTarget;
int itemCount = target.getCount();
if (itemCount == 0) {
return false;
}
int childCount = target.getChildCount();
int firstPosition = target.getFirstVisiblePosition();
int lastPosition = firstPosition + childCount;
if (direction > 0) {
if (lastPosition >= itemCount && target.getChildAt(childCount - 1).getBottom() <= target.getHeight()) {
return false;
}
} else if (direction >= 0) {
return false;
} else {
if (firstPosition <= 0 && target.getChildAt(0).getTop() >= 0) {
return false;
}
}
return true;
}
/**
* Deletes one track.
*/
public void testDeleteOneTrack() {
EndToEndTestUtils.createSimpleTrack(1, true);
ListView listView = EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0);
int trackCount = listView.getCount();
assertTrue(trackCount > 0);
EndToEndTestUtils.SOLO.clickOnView(listView.getChildAt(0));
EndToEndTestUtils.SOLO.waitForText(
trackListActivity.getString(R.string.track_detail_chart_tab));
EndToEndTestUtils.findMenuItem(trackListActivity.getString(R.string.menu_delete), true);
EndToEndTestUtils.getButtonOnScreen(
trackListActivity.getString(R.string.generic_yes), true, true);
EndToEndTestUtils.waitTextToDisappear(
trackListActivity.getString(R.string.generic_progress_title));
assertEquals(
trackCount - 1, EndToEndTestUtils.SOLO.getCurrentViews(ListView.class).get(0).getCount());
}
protected ArrayList<BrowserRecord> getSelectedFiles()
{
ArrayList<BrowserRecord> selectedRecordsList = new ArrayList<>();
ListView lv = getListView();
int count = lv.getCount();
for(int i=0; i<count;i++)
{
BrowserRecord file = (BrowserRecord) lv.getItemAtPosition(i);
if (file.isSelected())
selectedRecordsList.add(file);
}
return selectedRecordsList;
}
protected Collection<BrowserRecord> getSelectableFiles()
{
ArrayList<BrowserRecord> selectableFilesList = new ArrayList<>();
ListView lv = getListView();
int count = lv.getCount();
for(int i=0; i<count;i++)
{
BrowserRecord file = (BrowserRecord) lv.getItemAtPosition(i);
if (file!=null && file.allowSelect())
selectableFilesList.add(file);
}
return selectableFilesList;
}
protected boolean haveSelectedFiles()
{
ListView lv = getListView();
int count = lv.getCount();
for(int i=0; i<count;i++)
{
BrowserRecord file = (BrowserRecord) lv.getItemAtPosition(i);
if (file.isSelected())
return true;
}
return false;
}
@Override
public boolean canTargetScrollVertically(int direction) {
final ListView target = mTarget;
final int itemCount = target.getCount();
final int childCount = target.getChildCount();
final int firstPosition = target.getFirstVisiblePosition();
final int lastPosition = firstPosition + childCount;
if (direction > 0) {
// Are we already showing the entire last item?
if (lastPosition >= itemCount) {
final View lastView = target.getChildAt(childCount - 1);
if (lastView.getBottom() <= target.getHeight()) {
return false;
}
}
} else if (direction < 0) {
// Are we already showing the entire first item?
if (firstPosition <= 0) {
final View firstView = target.getChildAt(0);
if (firstView.getTop() >= 0) {
return false;
}
}
} else {
// The behavior for direction 0 is undefined and we can return
// whatever we want.
return false;
}
return true;
}
private void clearSelectedFlag()
{
ListView lv = getListView();
for(int i=0, count = lv.getCount(); i<count;i++)
{
LocationInfo li = (LocationInfo) lv.getItemAtPosition(i);
if (li.isSelected)
{
li.isSelected = false;
updateRowView(lv, i);
}
}
}
private int getItemPosition(LocationInfo li)
{
ListView lv = getListView();
for(int i=0, n = lv.getCount();i<n;i++)
{
LocationInfo info = (LocationInfo) lv.getItemAtPosition(i);
if(li == info)
return i;
}
return -1;
}
protected boolean onSelectionActionItemClicked(final ActionMode mode, MenuItem item) {
ListView listView = getListView();
switch (item.getItemId()) {
case R.id.blob_descriptor_delete:
int count = listView.getCheckedItemCount();
String countStr = getResources().getQuantityString(getDeleteConfirmationItemCountResId(), count, count);
String message = getString(R.string.blob_descriptor_confirm_delete, countStr);
deleteConfirmationDialog = new AlertDialog.Builder(getActivity())
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("")
.setMessage(message)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
deleteSelectedItems();
mode.finish();
deleteConfirmationDialog = null;
}
})
.setNegativeButton(android.R.string.no, null).create();
deleteConfirmationDialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
@Override
public void onDismiss(DialogInterface dialogInterface) {
deleteConfirmationDialog = null;
}
});
deleteConfirmationDialog.show();
return true;
case R.id.blob_descriptor_select_all:
int itemCount = listView.getCount();
for (int i = itemCount - 1; i > -1; --i) {
listView.setItemChecked(i, true);
}
return true;
default:
return false;
}
}
private void collapseAll()
{
ListView lv = getDrawerController().getDrawerListView();
for(int i=0;i<lv.getCount();i++)
{
Object di = lv.getItemAtPosition(i);
if(di instanceof DrawerSubMenuBase && ((DrawerSubMenuBase)di).isExpanded())
((DrawerSubMenuBase) di).collapse();
}
}
private ScrollState getScrollPosition() {
final ListView listView = this.binding == null ? null : this.binding.messagesView;
if (listView == null || listView.getCount() == 0 || listView.getLastVisiblePosition() == listView.getCount() - 1) {
return null;
} else {
final int pos = listView.getFirstVisiblePosition();
final View view = listView.getChildAt(0);
if (view == null) {
return null;
} else {
return new ScrollState(pos, view.getTop());
}
}
}
private void actionModeInvertSelection() {
ListView lv = getListView();
for(int i = 0; i < lv.getCount(); i++) {
lv.setItemChecked(i, !lv.isItemChecked(i));
}
mMode.invalidate();
}
@Test
public void testAvailableSettings()
{
ActivityController activityController = Robolectric.buildActivity(SettingsActivity.class).create();
Activity activity = (Activity)activityController.get();
activityController.start();
activityController.resume();
activityController.visible();
ListView list = (ListView)activity.findViewById(android.R.id.list);
shadowOf(list).populateItems();
List<String> settingTitles = new LinkedList<>
(Arrays.asList(
"Receipt Quality"
));
assertEquals(settingTitles.size(), list.getCount());
for(int index = 0; index < list.getCount(); index++)
{
ListPreference preference = (ListPreference)list.getItemAtPosition(0);
String title = preference.getTitle().toString();
assertTrue(settingTitles.remove(title));
}
assertTrue(settingTitles.isEmpty());
}
private void checkAll(DialogInterface dialog, boolean val) {
if (dialog == null) {
return;
}
ListView lv = (ListView) ((AlertDialog) dialog).findViewById(R.id.searchListView);
int size = lv.getCount();
for (int i = 0; i < size; i++) {
lv.setItemChecked(i, val);
mClickedDialogEntryIndices[i] = val;
}
}
/**
* Detect SimpleType of a Widget
*
* @param v Widget
* @return
*/
public static String getSimpleType(View v, String type, boolean alreadyCalled)
{
if (type.endsWith("null")) return NULL;
if (type.endsWith("RadioButton")) return RADIO;
if (type.endsWith("RadioGroup")) return RADIO_GROUP;
if (type.endsWith("CheckBox") || type.endsWith("CheckedTextView")) return CHECKBOX;
if (type.endsWith("ToggleButton")) return TOGGLE_BUTTON;
if (type.endsWith("MenuDropDownListView") || type.endsWith("IconMenuView") || type.endsWith("ActionMenuView")) return MENU_VIEW;
if (type.endsWith("ListMenuItemView") || type.endsWith("IconMenuItemView") || type.endsWith("ActionMenuItemView")) return MENU_ITEM;
if (type.endsWith("DatePicker")) return DATE_PICKER;
if (type.endsWith("TimePicker")) return TIME_PICKER;
if (type.endsWith("DialogTitle")) return DIALOG_VIEW;
if (type.endsWith("Button")) return BUTTON;
if (type.endsWith("EditText")) return EDIT_TEXT;
if (type.endsWith("SearchAutoComplete")) return SEARCH_BAR;
if (type.endsWith("Spinner")) {
Spinner s = (Spinner)v;
if (s.getCount() == 0) return EMPTY_SPINNER;
return SPINNER;
}
if (type.endsWith("SeekBar")) return SEEK_BAR;
if (v instanceof RatingBar && (!((RatingBar)v).isIndicator())) return RATING_BAR;
if (type.endsWith("TabHost")) return TAB_HOST;
//if (type.endsWith("ExpandedMenuView") || type.endsWith("AlertController$RecycleListView")) { return EXPAND_MENU; }
if (type.endsWith("ListView") || type.endsWith("ExpandedMenuView")) {
ListView l = (ListView)v;
if (l.getCount() == 0) return EMPTY_LIST;
if (l.getAdapter().getClass().getName().endsWith("PreferenceGroupAdapter")) {
return PREFERENCE_LIST;
}
switch (l.getChoiceMode()) {
case ListView.CHOICE_MODE_NONE: return LIST_VIEW;
case ListView.CHOICE_MODE_SINGLE: return SINGLE_CHOICE_LIST;
case ListView.CHOICE_MODE_MULTIPLE: return MULTI_CHOICE_LIST;
}
}
if (type.endsWith("AutoCompleteTextView")) return AUTOCOMPLETE_TEXTVIEW;
if (type.endsWith("TextView")) return TEXT_VIEW;
if (type.endsWith("ImageView")) return IMAGE_VIEW;
if (type.endsWith("LinearLayout")) return LINEAR_LAYOUT;
if (type.endsWith("RelativeLayout")) return RELATIVE_LAYOUT;
if (type.endsWith("SlidingDrawer")) return SLIDING_DRAWER;
if (type.endsWith("DrawerLayout")) return DRAWER_LAYOUT;
if ((v instanceof WebView) || type.endsWith("WebView")) return WEB_VIEW;
if (type.endsWith("TwoLineListItem")) return LIST_ITEM;
if (type.endsWith("NumberPicker")) return NUMBER_PICKER;
if (type.endsWith("NumberPickerButton")) return NUMBER_PICKER_BUTTON;
String parentType = v.getClass().getSuperclass().getName();
if (alreadyCalled == false && parentType != null) {
System.out.print(">>>>>> " + parentType);
return getSimpleType(v, parentType, true);
}
return "";
}