[Difficult for Android] setSelection and setSelectionFromTop, androidselection

Source: Internet
Author: User

[Difficult for Android] setSelection and setSelectionFromTop, androidselection

Generally, data in the app is displayed in ListView format. By default, "new" data is added to the end of the Data list.

However, for an IM-type app, such as viewing historical messages. The new data is not inserted at the end of the Data list, but in the header of the Data list.

To achieve a better user experience, you must maintain the current position of the ListView. In other words, if we can specify the position of the ListView to scroll freely, this problem will be solved.

In ListView, there is a setSelectionFromTop () method. The following is an example. The Code is as follows:

@ Override public void loaded (Long loadTime, int thisPageNumber, boolean isFromZero, boolean isHasMoreToLoad, List data) {refreshComplete (); checkIfHasMoreToLoad (isHasMoreToLoad); if (thisPageNumber! = 1) {// not the first page of mListView. setSelectionFromTop (5 + 2, response. getHeaderHeight (); mIMPullToRefreshListView. getHeaderView (). setVisibility (View. GONE );}}

Take a look at the specific implementation of setSelectionFromTop (). The Code is as follows:

    /**     * Sets the selected item and positions the selection y pixels from the top edge     * of the ListView. (If in touch mode, the item will not be selected but it will     * still be positioned appropriately.)     *     * @param position Index (starting at 0) of the data item to be selected.     * @param y The distance from the top edge of the ListView (plus padding) that the     *        item will be positioned.     */    public void setSelectionFromTop(int position, int y) {        if (mAdapter == null) {            return;        }        if (!isInTouchMode()) {            position = lookForSelectablePosition(position, true);            if (position >= 0) {                setNextSelectedPositionInt(position);            }        } else {            mResurrectToPosition = position;        }        if (position >= 0) {            mLayoutMode = LAYOUT_SPECIFIC;            mSpecificTop = mListPadding.top + y;            if (mNeedSync) {                mSyncPosition = position;                mSyncRowId = mAdapter.getItemId(position);            }            requestLayout();        }    }
From the code above, we can know that setSelectionFromTop () is used to set the position selected by the ListView and an offset (padding value) on the Y axis ).

Another method of ListView is setSelection (). If an index integer value is input, the ListView can locate the specified Item.

What are the differences between the two methods? Take a look at the specific implementation of setSelection (). The Code is as follows:

    /**     * Sets the currently selected item. If in touch mode, the item will not be selected     * but it will still be positioned appropriately. If the specified selection position     * is less than 0, then the item at position 0 will be selected.     *     * @param position Index (starting at 0) of the data item to be selected.     */    @Override    public void setSelection(int position) {        setSelectionFromTop(position, 0);    }
It turns out that setSelection () calls setSelectionFromTop () internally, except that the offset of the Y axis is 0.

Now we should have a deeper understanding of setSelection () and setSelectionFromTop.


References

Http://developer.android.com/reference/android/widget/ListView.html#setSelection%28int%29

Http://www.cnblogs.com/over140/archive/2013/05/20/2948239.html

Http://blog.csdn.net/jdsjlzx/article/details/17794209

Http://blog.csdn.net/a859522265/article/details/8154103


Why is the android editText setselection always located at the bottom of the screen? Can it be located at the top or middle of the screen?

In my memory, setSelection seems to be the position of the edited cursor. In EditText, it seems that the cursor can be set at the end of the content.

In the ListView control of Android, how does one place the selected item in the middle of the list?

Set onScrollListener for ListView to listen to onScroll events and obtain the current firstVisibleItem and visibleItemCount. Set OnItemClickListener for ListView to listen to the itemclick event to obtain the index of the selected item, and call the setSelection (int index) method of listView to locate the selected position of listView again, the index is calculated based on the index obtained by the itemClick event and the value obtained by the scroll event. This is not verified, and I don't know the accuracy. There should be a better way to ask Daniel.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.