private static int save = -1; public void onListItemClick(ListView parent, View v, int position, long id) { parent.getChildAt(position).setBackgroundColor(Color.BLUE); if (save != -1 && save != position){ parent.getChildAt(save).setBackgroundColor(Color.BLACK); } save = position; }
@Override public View getChildAt(int position) { return mListView.getChildAt(position); }
@Override public void onListItemClick(ListView l, View v, int position, long id) { if (firstTimeStartup) {// first time highlight first row currentSelectedView = l.getChildAt(0); } firstTimeStartup = false; if (currentSelectedView != null && currentSelectedView != v) { unhighlightCurrentRow(currentSelectedView); } currentSelectedView = v; highlightCurrentRow(currentSelectedView); //other codes }
public View getViewByPosition(int pos, ListView listView) { final int firstListItemPosition = listView.getFirstVisiblePosition(); final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1; if (pos < firstListItemPosition || pos > lastListItemPosition ) { return listView.getAdapter().getView(pos, null, listView); } else { final int childIndex = pos - firstListItemPosition; return listView.getChildAt(childIndex); } }
public static int getScrollY(ListView lv) { View c = lv.getChildAt(0); if (c == null) { return 0; } int firstVisiblePosition = lv.getFirstVisiblePosition(); int top = c.getTop(); int scrollY = -top + firstVisiblePosition * c.getHeight(); return scrollY; }
private int calculateScrollYList() { View c = mListView.getChildAt(0); if (c == null) { return 0; } //TODO support more than 1 header? int firstVisiblePosition = mListView.getFirstVisiblePosition(); int top = c.getTop(); int headerHeight = 0; if (firstVisiblePosition >= 1) { //TODO >= number of header headerHeight = mHeightHeader; } return -top + firstVisiblePosition * c.getHeight() + headerHeight; }
/** * 判断mContentView是否处于顶部 * * @return */ private boolean checkIsTop() { int childCount = mContentView.getChildCount(); if (childCount > 0) { View firstChild = mContentView.getChildAt(0); if (firstChild.getTop() >= 0 && firstItem == 0 && currentTop == 0) { return true; } else return false; } else { return false; } }
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ((SwipeLayout)(mListView.getChildAt(position - mListView.getFirstVisiblePosition()))).open(true); } });
/** * 判断是否需要拦截触摸事件 * * @return */ private boolean shouldIntercept() { if (bDraging) return true; int childCount = mContentView.getChildCount(); if (childCount > 0) { View firstChild = mContentView.getChildAt(0); if (firstChild.getTop() >= 0 && firstItem == 0 && currentTop == 0 && bScrollDown) { return true; } else return false; } else { return true; } }
public View getViewByPosition(int position, ListView listView) { final int firstListItemPosition = listView.getFirstVisiblePosition(); final int lastListItemPosition = firstListItemPosition + listView.getChildCount() - 1; if (position < firstListItemPosition || position > lastListItemPosition ) { return listView.getAdapter().getView(position, null, listView); } else { final int childIndex = position - firstListItemPosition; return listView.getChildAt(childIndex); } }
ListView list = getListView(); int start = list.getFirstVisiblePosition(); for(int i=start, j=list.getLastVisiblePosition();i<=j;i++) if(target==list.getItemAtPosition(i)){ View view = list.getChildAt(i-start); list.getAdapter().getView(i, view, list); break; }
ListView list = getListView(); int start = list.getFirstVisiblePosition(); for(int i=start, j=list.getLastVisiblePosition();i<=j;i++) if(target==list.getItemAtPosition(i)){ View view = list.getChildAt(i-start); list.getAdapter().getView(i, view, list); break; }
@Override public View getViewAt(int position) { View itemView = list.getChildAt(position - list.getFirstVisiblePosition()); return itemView == null ? null : ListAdapter.getImageView(itemView); } };
@Override public void run() { if (lv == null) return; synchronized (this) { int start = lv.getFirstVisiblePosition(); int end = Math.min(lv.getLastVisiblePosition(), list.size()); for (int i = start; i <= end; i++) if (target == list.get(i)) { View view = lv.getChildAt(i - start); getView(i, view, lv); break; } } } });
public static int dpToPx(int dp, Context ctx) { Resources r = ctx.getResources(); return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()); } //originally: http://stackoverflow.com/questions/5418510/disable-the-touch-events-for-all-the-views //modified for the needs here public static void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) { int childCount = viewGroup.getChildCount(); for (int i = 0; i < childCount; i++) { View view = viewGroup.getChildAt(i); if(view.isFocusable()) view.setEnabled(enabled); if (view instanceof ViewGroup) { enableDisableViewGroup((ViewGroup) view, enabled); } else if (view instanceof ListView) { if(view.isFocusable()) view.setEnabled(enabled); ListView listView = (ListView) view; int listChildCount = listView.getChildCount(); for (int j = 0; j < listChildCount; j++) { if(view.isFocusable()) listView.getChildAt(j).setEnabled(false); } } } }
public void show() { int top = 0; if (getRefreshView().getChildCount() > 0 && getRefreshView().getChildAt(0).getHeight() > 0) { top = getRefreshView().getChildAt(0).getHeight() * 3; } else { top = Utils.dip2px(getActivity(), 175); } getRefreshView().setSelectionFromTop(selectedPosition, top); }
ListView listView; Runnable fitsOnScreen = new Runnable() { @Override public void run() { int last = listView.getLastVisiblePosition(); if(last == listView.getCount() - 1 && listView.getChildAt(last).getBottom() <= listView.getHeight()) { // It fits! } else { // It doesn't fit... } } };
@Test public void setAdapter_shouldNotClearHeaderOrFooterViews() throws Exception { View header = new View(context); listView.addHeaderView(header); View footer = new View(context); listView.addFooterView(footer); prepareListWithThreeItems(); assertThat(listView.getChildCount()).isEqualTo(5); assertThat(listView.getChildAt(0)).isSameAs(header); assertThat(listView.getChildAt(4)).isSameAs(footer); }
@Test public void getPositionForView_shouldReturnThePositionInTheListForTheView() throws Exception { prepareWithListAdapter(); View childViewOfListItem = ((ViewGroup) listView.getChildAt(1)).getChildAt(0); assertThat(listView.getPositionForView(childViewOfListItem)).isEqualTo(1); }
@Test public void findItemContainingText_shouldFindChildByString() throws Exception { ShadowListView shadowListView = prepareListWithThreeItems(); View item1 = shadowListView.findItemContainingText("Item 1"); assertThat(item1).isSameAs(listView.getChildAt(1)); }