Java Code Examples for android.widget.listview#getFirstVisiblePosition()

The following examples show how to use android.widget.listview#getFirstVisiblePosition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ListPosition.java    From Dashchan with Apache License 2.0 6 votes vote down vote up
public static ListPosition obtain(ListView listView) {
	int position = listView.getFirstVisiblePosition();
	int y = 0;
	Rect rect = new Rect();
	int paddingTop = listView.getPaddingTop(), paddingLeft = listView.getPaddingLeft();
	for (int i = 0, count = listView.getChildCount(); i < count; i++) {
		View view = listView.getChildAt(i);
		view.getHitRect(rect);
		if (rect.contains(paddingLeft, paddingTop)) {
			position += i;
			y = rect.top - paddingTop;
			break;
		}
	}
	return new ListPosition(position, y);
}
 
Example 2
Source File: FsBrowserRecord.java    From edslite with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: PilotsSetupFragment.java    From Chorus-RF-Laptimer with MIT License 6 votes vote down vote up
public void updatePilotNames() {
    ListView mListView = (ListView)mRootView.findViewById(R.id.lvPilots);
    int count = AppState.getInstance().deviceStates.size();
    int firstVisibleItemPos = mListView.getFirstVisiblePosition();
    int lastVisibleItemPos = mListView.getLastVisiblePosition();

    // detect possible faulty edge cases
    if (firstVisibleItemPos > lastVisibleItemPos ||
            firstVisibleItemPos < 0 ||
            lastVisibleItemPos > count - 1 ) return;

    // update only visible list items
    for (int i = firstVisibleItemPos; i <= lastVisibleItemPos; i++) {
        // children enumeration starts with zero
        View convertView = mListView.getChildAt(i - firstVisibleItemPos);
        if (convertView != null) {
            EditText pilotName = (EditText) convertView.findViewById(R.id.editPilotName);
            String curPilotName = AppState.getInstance().deviceStates.get(i).pilotName;
            pilotName.setText(curPilotName);
        }
    }
}
 
Example 4
Source File: GitHistoryActivity.java    From APDE with GNU General Public License v2.0 6 votes vote down vote up
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));
		}
	}
}
 
Example 5
Source File: BaseExpandableListFragment.java    From 4pdaClient-plus with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (!Preferences.Lists.getScrollByButtons())
        return false;

    int action = event.getAction();

    ListView scrollView = getListView();
    int visibleItemsCount = scrollView.getLastVisiblePosition() - scrollView.getFirstVisiblePosition();

    int keyCode = event.getKeyCode();
    if(Preferences.System.isScrollUpButton(keyCode)){
        if (action == KeyEvent.ACTION_DOWN)
            scrollView.setSelection(Math.max(scrollView.getFirstVisiblePosition() - visibleItemsCount, 0));
        return true;// true надо обязательно возвращать даже если не ACTION_DOWN иначе звук нажатия
    }
    if(Preferences.System.isScrollDownButton(keyCode)){
        if (action == KeyEvent.ACTION_DOWN)
            scrollView.setSelection(Math.min(scrollView.getLastVisiblePosition(), scrollView.getCount() - 1));
        return true;// true надо обязательно возвращать даже если не ACTION_DOWN иначе звук нажатия
    }

    return false;
}
 
Example 6
Source File: SwipeRefreshListFragment.java    From soas with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to check whether a {@link ListView} can scroll up from it's current position.
 * Handles platform version differences, providing backwards compatible functionality where
 * needed.
 */
private static boolean canListViewScrollUp(ListView listView) {
    if (SdkUtils.hasIceCreamSandwich()) {
        // For ICS and above we can call canScrollVertically() to determine this
        return ViewCompat.canScrollVertically(listView, -1);
    } else {
        // Pre-ICS we need to manually check the first visible item and the child view's top
        // value
        return listView.getChildCount() > 0 &&
                (listView.getFirstVisiblePosition() > 0
                        || listView.getChildAt(0).getTop() < listView.getPaddingTop());
    }
}
 
Example 7
Source File: ListViewUtil.java    From NIM_Android_UIKit with MIT License 5 votes vote down vote up
public static Object getViewHolderByIndex(ListView listView, int index) {
    int firstVisibleFeedPosition = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount();
    int lastVisibleFeedPosition = listView.getLastVisiblePosition() - listView.getHeaderViewsCount();

    //只有获取可见区域的
    if (index >= firstVisibleFeedPosition && index <= lastVisibleFeedPosition) {
        View view = listView.getChildAt(index - firstVisibleFeedPosition);
        Object tag = view.getTag();
        return tag;
    } else {
        return null;
    }
}
 
Example 8
Source File: BounceTouchListener.java    From Bounce with Apache License 2.0 5 votes vote down vote up
private boolean hasHitTop() {
    if (mMainView instanceof ScrollView) {
        ScrollView scrollView = (ScrollView) mMainView;
        return scrollView.getScrollY() == 0;
    } else if (mMainView instanceof ListView) {
        ListView listView = (ListView) mMainView;
        if (listView.getAdapter() != null) {
            if (listView.getAdapter().getCount() > 0) {
                return listView.getFirstVisiblePosition() == 0 &&
                        listView.getChildAt(0).getTop() >= 0;
            }
        }
    } else if (mMainView instanceof RecyclerView) {
        RecyclerView recyclerView = (RecyclerView) mMainView;
        if (recyclerView.getAdapter() != null && recyclerView.getLayoutManager() != null) {
            RecyclerView.Adapter adapter = recyclerView.getAdapter();
            if (adapter.getItemCount() > 0) {
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager instanceof LinearLayoutManager) {
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                    return linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 0;
                } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                    StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                    int[] checks = staggeredGridLayoutManager.findFirstCompletelyVisibleItemPositions(null);
                    for (int check : checks) {
                        if (check == 0)
                            return true;
                    }
                }
            }
        }
    }

    return false;
}
 
Example 9
Source File: LogcatViewerFloatingView.java    From LogcatViewer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get list item view by position
 *
 * @param pos      position of list item.
 * @param listView listview object.
 * @return list item view associated with position in the listview.
 */
private 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);
    }
}
 
Example 10
Source File: ViewUnit.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
public void invalidateCommentView(ListView listView, int position) {
	if (position != ListView.INVALID_POSITION) {
		int first = listView.getFirstVisiblePosition();
		int count = listView.getChildCount();
		int index = position - first;
		if (index >= 0 && index < count) {
			View child = listView.getChildAt(index);
			PostViewHolder holder = (PostViewHolder) child.getTag();
			holder.comment.invalidate();
		}
	}
}
 
Example 11
Source File: MyHomeListAdapter.java    From YiBo with Apache License 2.0 5 votes vote down vote up
public boolean refresh() {
	if (listNewBlogs == null || listNewBlogs.size() == 0) {
		return false;
	}

	addCacheToFirst(listNewBlogs);
	int offset = listNewBlogs.size();
	listNewBlogs.clear();

	ListView lvMicroBlog = (ListView)((Activity)context).findViewById(R.id.lvMicroBlog);
	if (lvMicroBlog == null) {
		return true;
	}
	Adapter adapter = lvMicroBlog.getAdapter();
	if (adapter instanceof HeaderViewListAdapter) {
		adapter = ((HeaderViewListAdapter)adapter).getWrappedAdapter();
	}
	if (adapter == this) {
		int position = lvMicroBlog.getFirstVisiblePosition();
		View view = lvMicroBlog.getChildAt(0);
		int y = 0;
		if (view != null && position >= 1) {
		    y = view.getTop();
		    //System.out.println("y:" + y + " position:" + position);
		    position += offset;
		    lvMicroBlog.setSelectionFromTop(position, y);
		}
	}

	return true;
}
 
Example 12
Source File: EndlessScrollListener.java    From buddycloud-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
		int visibleItemCount, int totalItemCount) {
	boolean loadMore = firstVisibleItem + visibleItemCount + VISIBLE_THRESHOLD >= totalItemCount;
	if (loadMore && !loading && !refreshing) {
		fragment.fillMore();
	}

	// enable/disable pull-to-refresh
	SwipeRefreshLayout refreshLayout = fragment.getSwipeRefreshLayout();
	if (firstVisibleItem == 0 && refreshLayout != null) {
		refreshLayout.setEnabled(true);
	} else {
		refreshLayout.setEnabled(false);
	}

	// show/hide post "+" button
	final ListView lw = fragment.getPostStreamView();
	if (view.getId() == lw.getId()) {
		final int currentFirstVisibleItem = lw.getFirstVisiblePosition();
		if (currentFirstVisibleItem > mLastFirstVisibleItem) {
			fragment.hideAddPostTopicBtn();
		} else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
			fragment.showAddPostTopicBtn();
		}

		mLastFirstVisibleItem = currentFirstVisibleItem;
	}
}
 
Example 13
Source File: FragmentSocialTimeline.java    From aptoide-client with GNU General Public License v2.0 5 votes vote down vote up
private static boolean canListViewScrollUp(ListView listView) {
    if (android.os.Build.VERSION.SDK_INT >= 14) {
        // For ICS and above we can call canScrollVertically() to determine this
        return ViewCompat.canScrollVertically(listView, -1);
    } else {
        // Pre-ICS we need to manually check the first visible item and the child view's top
        // value
        return listView.getChildCount() > 0 &&
                (listView.getFirstVisiblePosition() > 0
                        || listView.getChildAt(0).getTop() < listView.getPaddingTop());
    }
}
 
Example 14
Source File: WordListPreference.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 5 votes vote down vote up
void onWordListClicked(final View v) {
    // Note : v is the preference view
    final ViewParent parent = v.getParent();
    // Just in case something changed in the framework, test for the concrete class
    if (!(parent instanceof ListView)) return;
    final ListView listView = (ListView)parent;
    final int indexToOpen;
    // Close all first, we'll open back any item that needs to be open.
    final boolean wasOpen = mInterfaceState.isOpen(mWordlistId);
    mInterfaceState.closeAll();
    if (wasOpen) {
        // This button being shown. Take note that we don't want to open any button in the
        // loop below.
        indexToOpen = -1;
    } else {
        // This button was not being shown. Open it, and remember the index of this
        // child as the one to open in the following loop.
        mInterfaceState.setOpen(mWordlistId, mStatus);
        indexToOpen = listView.indexOfChild(v);
    }
    final int lastDisplayedIndex =
            listView.getLastVisiblePosition() - listView.getFirstVisiblePosition();
    // The "lastDisplayedIndex" is actually displayed, hence the <=
    for (int i = 0; i <= lastDisplayedIndex; ++i) {
        final ButtonSwitcher buttonSwitcher = (ButtonSwitcher)listView.getChildAt(i)
                .findViewById(R.id.wordlist_button_switcher);
        if (i == indexToOpen) {
            buttonSwitcher.setStatusAndUpdateVisuals(getButtonSwitcherStatus(mStatus));
        } else {
            buttonSwitcher.setStatusAndUpdateVisuals(ButtonSwitcher.STATUS_NO_BUTTON);
        }
    }
}
 
Example 15
Source File: SwipeRefreshWidget.java    From physical-web with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canChildScrollUp() {
  // The real child maps cares about is the list, so check if that can scroll.
  ListView target = (ListView) findViewById(android.R.id.list);
  return target.getChildCount() > 0
      && (target.getFirstVisiblePosition() > 0
      || target.getChildAt(0).getTop() < target.getPaddingTop());
}
 
Example 16
Source File: ScrollState.java    From Onosendai with Apache License 2.0 5 votes vote down vote up
public static ScrollState from (final ListView lv, final ScrollIndicator scrollIndicator, final ScrollDirection scrollDirection) {
	final int index = lv.getFirstVisiblePosition();
	final View v = lv.getChildAt(0);
	final int top = (v == null) ? 0 : v.getTop();

	final long itemId = lv.getAdapter().getItemId(index);
	if (itemId <= 0) return null;

	long time = ((TweetListCursorAdapter) lv.getAdapter()).getItemTime(index);
	if (time < 0L) time = 0L;

	return new ScrollState(itemId, top, time, scrollIndicator.getUnreadTime(), scrollDirection);
}
 
Example 17
Source File: ListViewAutoScrollHelper.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canTargetScrollVertically(int direction) {
    final ListView target = mTarget;
    final int itemCount = target.getCount();
    if (itemCount == 0) {
        return false;
    }

    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;
}
 
Example 18
Source File: DataList.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
    ListView listView = getListView();
    int count = listView.getCheckedItemCount();
    mode.setTitle(getResources().getQuantityString(R.plurals.itemsSelected, count, count));
    // Update (redraw) list item view
    int start = listView.getFirstVisiblePosition();
    for (int i = start, j = listView.getLastVisiblePosition(); i <= j; i++) {
        if (position == i) {
            View view = listView.getChildAt(i - start);
            listView.getAdapter().getView(i, view, listView);
            break;
        }
    }
}
 
Example 19
Source File: ListViewActivity.java    From easy-guide-android with Apache License 2.0 5 votes vote down vote up
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 null;
    } else {
        final int childIndex = pos - firstListItemPosition;
        return listView.getChildAt(childIndex);
    }
}
 
Example 20
Source File: UiUtil.java    From android-file-chooser with Apache License 2.0 4 votes vote down vote up
public static int getListYScroll(@NonNull final ListView list) {
    View child = list.getChildAt(0);
    return child == null ? -1
        : list.getFirstVisiblePosition() * child.getHeight() - child.getTop() + list.getPaddingTop();
}