Example usage for android.widget ListView smoothScrollToPosition

List of usage examples for android.widget ListView smoothScrollToPosition

Introduction

In this page you can find the example usage for android.widget ListView smoothScrollToPosition.

Prototype

@android.view.RemotableViewMethod
public void smoothScrollToPosition(int position) 

Source Link

Document

Smoothly scroll to the specified adapter position.

Usage

From source file:Main.java

public static void ensureVisibleSmoothScroll(ListView listView, int pos) {
    if (listView == null)
        return;/* w w w .  jav  a2s .c o m*/

    if (pos < 0 || pos >= listView.getCount())
        return;

    listView.smoothScrollToPosition(pos);
}

From source file:org.deafsapps.sordomartinezpabloluismarspics.fragments.MainFragment.java

@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
    super.onListItemClick(listView, view, position, id);

    listView.smoothScrollToPosition(position);
    view.setSelected(true);/* w  w w  .  jav a2s.  c  om*/

    mListener.onMainFragmentInteraction(mNumPage * mNumListItems + position);
}

From source file:me.futuretechnology.blops.ui.HomeActivity.java

@Override
protected void initUI() {
    super.initUI();

    final FeedsAdapter adapter = new FeedsAdapter(getApplication(), getSupportFragmentManager());

    final ViewPager pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);//from  w w  w . j ava2  s .  c o m

    FeedsIndicator indicator = (FeedsIndicator) findViewById(R.id.tabs);
    indicator.setViewPager(pager, adapter.getCount() > 1 ? 1 : 0);
    indicator.setOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            Log.i("BLOPS", "onPageSelected: ", position);
            if (position > 1) { // first page is handled in onPageScrolled()
                adapter.refreshFeed(position);
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // workaround for onPageSelected not being called the first time
            if (position == 1 && positionOffsetPixels == 0) {
                Log.i("BLOPS", "onPageScrolled @ 0: ", position);
                adapter.refreshFeed(position);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
    indicator.setOnCenterItemClickListener(new TitlePageIndicator.OnCenterItemClickListener() {
        @Override
        public void onCenterItemClick(int position) {
            Fragment f = adapter.getFragment(position);
            if (f != null) {
                ListView l = ((ListFragment) f).getListView();

                if (position == 0) {
                    l.smoothScrollToPosition(0);
                } else {
                    ((AltListView) l).requestPositionToScreen(0, true);
                }
            }
        }
    });
}

From source file:ro.edi.novelty.ui.HomeActivity.java

@Override
protected void initUI() {
    super.initUI();

    final FeedsAdapter adapter = new FeedsAdapter(getApplication(), getSupportFragmentManager());

    final ViewPager pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);/* www  .  j a  v a  2s . c om*/

    FeedsIndicator indicator = (FeedsIndicator) findViewById(R.id.tabs);
    indicator.setViewPager(pager, adapter.getCount() > 1 ? 1 : 0);
    indicator.setOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            Log.i("NOVELTY", "onPageSelected: ", position);
            if (position > 1) { // first page is handled in onPageScrolled()
                adapter.refreshFeed(position);
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // workaround for onPageSelected not being called the first time
            if (position == 1 && positionOffsetPixels == 0) {
                Log.i("NOVELTY", "onPageScrolled @ 0: ", position);
                adapter.refreshFeed(position);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
    indicator.setOnCenterItemClickListener(new TitlePageIndicator.OnCenterItemClickListener() {
        @Override
        public void onCenterItemClick(int position) {
            Fragment f = adapter.getFragment(position);
            if (f != null) {
                ListView l = ((ListFragment) f).getListView();

                if (position == 0) {
                    l.smoothScrollToPosition(0);
                } else {
                    ((AltListView) l).requestPositionToScreen(0, true);
                }
            }
        }
    });
}

From source file:com.bydavy.card.receipts.activities.ReceiptListActivity.java

@Override
public void onReceiptPageSelected(int index) {
    mListIndexSelected = index;/*from ww  w . j a  v  a  2s .  co m*/
    // Link the pager fragment to the list fragment
    final ListView listView = mReceiptListFragment.getListView();
    listView.setItemChecked(index, true);
    listView.smoothScrollToPosition(index);
}

From source file:com.battlelancer.seriesguide.ui.EpisodesFragment.java

/**
 * Highlight the given episode in the list.
 *///w  w w.  java  2  s  .c  o m
public void setItemChecked(int position) {
    ListView list = getListView();
    list.setItemChecked(position, true);
    if (position <= list.getFirstVisiblePosition() || position >= list.getLastVisiblePosition()) {
        list.smoothScrollToPosition(position);
    }
}

From source file:com.jefftharris.passwdsafe.PasswdSafeListFragment.java

@Override
public void onLoadFinished(Loader<List<PasswdRecordListData>> loader, List<PasswdRecordListData> data) {
    int selPos = itsAdapter.setData(data, itsIsContents ? itsSelectedRecord : itsLocation.getRecord());
    if (isResumed()) {
        ListView list = getListView();
        if (selPos != -1) {
            list.setItemChecked(selPos, true);
            list.smoothScrollToPosition(selPos);
        } else {//w ww .jav a  2  s.c  om
            list.clearChoices();
        }

        if (itsEmptyText.getText().length() == 0) {
            itsEmptyText.setText(itsIsContents ? R.string.no_records : R.string.no_groups);
        }
    }
}

From source file:net.tjado.passwdsafe.PasswdSafeListFragmentTree.java

@Override
public void onLoadFinished(Loader<List<PasswdRecordListData>> loader, List<PasswdRecordListData> data) {
    int selPos = itsAdapter.setData(data, itsIsContents ? itsSelectedRecord : itsLocation.getRecord());

    if (isResumed()) {
        ListView list = getListView();
        if (selPos != -1) {
            list.setItemChecked(selPos, true);
            list.smoothScrollToPosition(selPos);
        } else {/*ww  w.  j  a  v  a2s .  co m*/
            list.clearChoices();
        }

        if (itsEmptyText.getText().length() == 0) {
            itsEmptyText.setText(itsIsContents ? R.string.no_records : R.string.no_groups);
        }
    }
}

From source file:org.mozilla.gecko.tests.AboutHomeTest.java

protected View getDisplayedBookmark(String url) {
    openAboutHomeTab(AboutHomeTabs.BOOKMARKS);
    mSolo.hideSoftKeyboard();/*from w ww  . jav  a 2 s  . c  o  m*/
    getInstrumentation().waitForIdleSync();
    ListView bookmarksTabList = findListViewWithTag(HomePager.LIST_TAG_BOOKMARKS);
    waitForNonEmptyListToLoad(bookmarksTabList);
    ListAdapter adapter = bookmarksTabList.getAdapter();
    if (adapter != null) {
        for (int i = 0; i < adapter.getCount(); i++) {
            // I am unable to click the view taken with getView for some reason so getting the child at i
            bookmarksTabList.smoothScrollToPosition(i);
            View bookmarkView = bookmarksTabList.getChildAt(i);
            if (bookmarkView instanceof android.widget.LinearLayout) {
                ViewGroup bookmarkItemView = (ViewGroup) bookmarkView;
                for (int j = 0; j < bookmarkItemView.getChildCount(); j++) {
                    View bookmarkContent = bookmarkItemView.getChildAt(j);
                    if (bookmarkContent instanceof android.widget.LinearLayout) {
                        ViewGroup bookmarkItemLayout = (ViewGroup) bookmarkContent;
                        for (int k = 0; k < bookmarkItemLayout.getChildCount(); k++) {
                            // Both the title and url are represented as text views so we can cast the view without any issues
                            TextView bookmarkTextContent = (TextView) bookmarkItemLayout.getChildAt(k);
                            if (url.equals(bookmarkTextContent.getText().toString())) {
                                return bookmarkView;
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}

From source file:ch.blinkenlights.android.vanilla.LibraryPagerAdapter.java

/**
 * Perform usability-related actions on pager and contained lists, e.g. highlight current song
 * or scroll to it if opted-in/*  w  w w .j av  a2  s .  co m*/
 * @param song song that is currently playing, can be null
  */
public void onSongChange(Song song) {
    if (mCurrentPage == -1) // no page active, nothing to do
        return;

    int type = mTabOrder[mCurrentPage];
    ListView view = mLists[type];
    if (view == null) // not initialized yet, nothing to do
        return;

    long id = MediaUtils.getCurrentIdForType(song, type);
    if (id == -1) // unknown type
        return;

    // scroll to song on song change if opted-in
    SharedPreferences sharedPrefs = PlaybackService.getSettings(mActivity);
    boolean shouldScroll = sharedPrefs.getBoolean(PrefKeys.ENABLE_SCROLL_TO_SONG,
            PrefDefaults.ENABLE_SCROLL_TO_SONG);
    if (shouldScroll) {
        int middlePos = (view.getFirstVisiblePosition() + view.getLastVisiblePosition()) / 2;
        for (int pos = 0; pos < view.getCount(); pos++) {
            if (view.getItemIdAtPosition(pos) == id) {
                if (Math.abs(middlePos - pos) < 30) {
                    view.smoothScrollToPosition(pos);
                } else {
                    view.setSelection(pos);
                }
                break;
            }
        }
    }
}