Java Code Examples for android.widget.ListView#setSelectionFromTop()

The following examples show how to use android.widget.ListView#setSelectionFromTop() . 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: UARTLogFragment.java    From Android-nRF-Toolbox with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onLoadFinished(@NonNull final Loader<Cursor> loader, final Cursor data) {
	// Here we have to restore the old saved scroll position, or scroll to the bottom if before adding new events it was scrolled to the bottom.  
	final ListView list = getListView();
	final int position = logScrollPosition;
	final boolean scrolledToBottom = position == LOG_SCROLLED_TO_BOTTOM || (list.getCount() > 0 && list.getLastVisiblePosition() == list.getCount() - 1);

	logAdapter.swapCursor(data);

	if (position > LOG_SCROLL_NULL) {
		list.setSelectionFromTop(position, 0);
	} else {
		if (scrolledToBottom)
			list.setSelection(list.getCount() - 1);
	}
	logScrollPosition = LOG_SCROLL_NULL;
}
 
Example 2
Source File: ListViewAutoScrollHelper.java    From adt-leanback-support with Apache License 2.0 6 votes vote down vote up
@Override
public void scrollTargetBy(int deltaX, int deltaY) {
    final ListView target = mTarget;
    final int firstPosition = target.getFirstVisiblePosition();
    if (firstPosition == ListView.INVALID_POSITION) {
        return;
    }

    final View firstView = target.getChildAt(0);
    if (firstView == null) {
        return;
    }

    final int newTop = firstView.getTop() - deltaY;
    target.setSelectionFromTop(firstPosition, newTop);
}
 
Example 3
Source File: ListViewAutoScrollHelper.java    From android-recipes-app with Apache License 2.0 6 votes vote down vote up
@Override
public void scrollTargetBy(int deltaX, int deltaY) {
    final ListView target = mTarget;
    final int firstPosition = target.getFirstVisiblePosition();
    if (firstPosition == ListView.INVALID_POSITION) {
        return;
    }

    final View firstView = target.getChildAt(0);
    if (firstView == null) {
        return;
    }

    final int newTop = firstView.getTop() - deltaY;
    target.setSelectionFromTop(firstPosition, newTop);
}
 
Example 4
Source File: ListViewAutoScrollHelper.java    From V.FlyoutTest with MIT License 6 votes vote down vote up
@Override
public void scrollTargetBy(int deltaX, int deltaY) {
    final ListView target = mTarget;
    final int firstPosition = target.getFirstVisiblePosition();
    if (firstPosition == ListView.INVALID_POSITION) {
        return;
    }

    final View firstView = target.getChildAt(0);
    if (firstView == null) {
        return;
    }

    final int newTop = firstView.getTop() - deltaY;
    target.setSelectionFromTop(firstPosition, newTop);
}
 
Example 5
Source File: AbstractViewQuery.java    From COCOQuery with Apache License 2.0 6 votes vote down vote up
public T smoothScrollTo(final int position) {
    if (view instanceof ListView) {
        final ListView listView = (ListView) view;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            listView.smoothScrollToPositionFromTop(position, 0);
            listView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // Mock touchEvent to stop listView Scrolling.
                    listView.onTouchEvent(MotionEvent.obtain(System.currentTimeMillis(),
                            System.currentTimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
                }
            }, 150 - 20);

            listView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    listView.setSelectionFromTop(position, 0);
                }
            }, 150);
        } else {
            listView.setSelectionFromTop(position, 0);
        }
    }
    return self();
}
 
Example 6
Source File: ListViewAutoScrollHelper.java    From guideshow with MIT License 6 votes vote down vote up
@Override
public void scrollTargetBy(int deltaX, int deltaY) {
    final ListView target = mTarget;
    final int firstPosition = target.getFirstVisiblePosition();
    if (firstPosition == ListView.INVALID_POSITION) {
        return;
    }

    final View firstView = target.getChildAt(0);
    if (firstView == null) {
        return;
    }

    final int newTop = firstView.getTop() - deltaY;
    target.setSelectionFromTop(firstPosition, newTop);
}
 
Example 7
Source File: GitHistoryActivity.java    From APDE with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onActivityCreated(Bundle savedInstanceState) {
	super.onActivityCreated(savedInstanceState);
	
	if (getView() != null && savedInstanceState != null) {
		final ListView commitList = (ListView) getView().findViewById(R.id.git_history_commit_list);

		if (commitList != null) {
			commitList.setSelectionFromTop(savedInstanceState.getInt("listIndex", 0), savedInstanceState.getInt("listTop", 0));
			
			selectItem(savedInstanceState.getInt("selectedItem", -1));
		}
	}
}
 
Example 8
Source File: UDBaseListView.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * listview滚动到顶部
 *
 * @param animate 是否动画
 * @return
 */
public UDBaseListOrRecyclerView scrollToTop( int offset,  boolean animate) {
     ListView lv = getListView();
    if (lv != null) {
        if (animate) {
            if (lv.getFirstVisiblePosition() > 7) {//hack fast scroll
                lv.setSelection(7);
            }
            lv.smoothScrollToPositionFromTop(0, offset);
        } else {
            lv.setSelectionFromTop(0, offset);
        }
    }
    return this;
}
 
Example 9
Source File: ScrollState.java    From Onosendai with Apache License 2.0 5 votes vote down vote up
private void applyToListView (final ListView lv) {
	// NOTE if this seems unreliable try wrapping setSelection*() calls in lv.post(...).
	final ListAdapter adapter = lv.getAdapter();

	if (this.itemId >= 0L) {
		for (int i = 0; i < adapter.getCount(); i++) {
			if (adapter.getItemId(i) == this.itemId) {
				lv.setSelectionFromTop(i, this.top);
				return;
			}
		}
	}

	// Also search by time before giving up.
	if (this.itemTime > 0L && adapter instanceof TweetListCursorAdapter) {
		final TweetListCursorAdapter tlca = (TweetListCursorAdapter) adapter;
		for (int i = 0; i < tlca.getCount(); i++) {
			final long itime = tlca.getItemTime(i);
			if (itime > 0L && itime <= this.itemTime) {
				lv.setSelectionFromTop(i, 0);
				return;
			}
		}
		LOG.w("Failed to restore scroll state %s to list of %s items.", this, tlca.getCount());
	}
	else {
		LOG.w("Failed to restore scroll state %s.", this);
	}

	lv.setSelection(lv.getCount() - 1);
}
 
Example 10
Source File: DirectMessagesListAdapter.java    From YiBo with Apache License 2.0 5 votes vote down vote up
public boolean refresh() {
	if (ListUtil.isEmpty(newInboxList) && ListUtil.isEmpty(newOutboxList)) {
		return false;
	}

	addCacheToFirst(newInboxList);
	addCacheToFirst(newOutboxList);
	int offset = newInboxList.size() + newOutboxList.size();
	newInboxList.clear();
	newOutboxList.clear();
	newCount = 0;

	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 (lvMicroBlog != null 
		&& adapter == this) {
		int position = lvMicroBlog.getFirstVisiblePosition();
		View view = lvMicroBlog.getChildAt(0);
		int y = 0;
		if (view != null && position >= 1) {
		    y = view.getTop();
		    position += offset;
		    lvMicroBlog.setSelectionFromTop(position, y);
		}
	}

	return true;
}
 
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: CommentsListAdapter.java    From YiBo with Apache License 2.0 5 votes vote down vote up
public boolean refresh() {
	if (ListUtil.isEmpty(listNewComments)) {
		return false;
	}

	addCacheToFirst(listNewComments);
	int offset = listNewComments.size();
	listNewComments.clear();
	newCount = 0;

	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 (lvMicroBlog != null &&
		adapter == this
	) {
		int position = lvMicroBlog.getFirstVisiblePosition();
		View view = lvMicroBlog.getChildAt(0);
		int y = 0;
		if (view != null && position >= 1) {
		    y = view.getTop();
		    position += offset;
		    lvMicroBlog.setSelectionFromTop(position, y);
		}
	}

	return true;
}
 
Example 13
Source File: MentionsListAdapter.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();
	newCount = 0;

	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 (lvMicroBlog != null &&
		adapter == this
	) {
		int position = lvMicroBlog.getFirstVisiblePosition();
		View view = lvMicroBlog.getChildAt(0);
		int y = 0;
		if (view != null && position >= 1) {
		    y = view.getTop();
		    position += offset;
		    lvMicroBlog.setSelectionFromTop(position, y);
		}
	}

	return true;
}
 
Example 14
Source File: ListViewUtils.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
public static void setSelection(final ListView listView, int pos, boolean jumpToBottom) {
	if (jumpToBottom) {
		final View lastChild = listView.getChildAt(listView.getChildCount() - 1);
		if (lastChild != null) {
			listView.setSelectionFromTop(pos, -lastChild.getHeight());
			return;
		}
	}
	listView.setSelection(pos);
}
 
Example 15
Source File: ListPosition.java    From Dashchan with Apache License 2.0 5 votes vote down vote up
public void apply(final ListView listView) {
	if (listView.getHeight() == 0) {
		listView.post(() -> listView.setSelectionFromTop(position, y));
	} else {
		listView.setSelectionFromTop(position, y);
	}
}
 
Example 16
Source File: ListViewUtils.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
public static void setSelection(final ListView listView, int pos, boolean jumpToBottom) {
    if (jumpToBottom) {
        final View lastChild = listView.getChildAt(listView.getChildCount() - 1);
        if (lastChild != null) {
            listView.setSelectionFromTop(pos, -lastChild.getHeight());
            return;
        }
    }
    listView.setSelection(pos);
}
 
Example 17
Source File: UDBaseListView.java    From VideoOS-Android-SDK with GNU General Public License v3.0 5 votes vote down vote up
/**
 * listview滚动到某个位置
 *
 * @param section
 * @param rowInSection
 * @param offset
 * @return
 */
public UDBaseListOrRecyclerView scrollToItem( int section,  int rowInSection,  int offset,  boolean animate) {
     ListView lv = getListView();
    if (lv != null) {
        if (animate) {
            lv.smoothScrollToPositionFromTop(getPositionBySectionAndRow(section, rowInSection), offset);
        } else {
            lv.setSelectionFromTop(getPositionBySectionAndRow(section, rowInSection), offset);
        }
    }
    return this;
}
 
Example 18
Source File: ArtistList.java    From prettygoodmusicplayer with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void onResume() {
	super.onResume();
       SharedPreferences prefs = getSharedPreferences("PrettyGoodMusicPlayer", MODE_PRIVATE);
       prefs.edit();
       File bestGuessMusicDir = Utils.getBestGuessMusicDirectory();
       String prefDir = prefs.getString("ARTIST_DIRECTORY", bestGuessMusicDir.getAbsolutePath());
       ListView lv = (ListView) findViewById(R.id.artistListView);
       if(!prefDir.equals(baseDir)){
       	baseDir = prefDir;
       	populateArtists(baseDir);
           
           simpleAdpt = new SimpleAdapter(this, artists,  R.layout.pgmp_list_item, new String[] {"artist"}, new int[] {R.id.PGMPListItemText});
           lv.setAdapter(simpleAdpt);
       }
       
       int top = prefs.getInt("ARTIST_LIST_TOP", Integer.MIN_VALUE);
       int index = prefs.getInt("ARTIST_LIST_INDEX", Integer.MIN_VALUE);
       if(top > Integer.MIN_VALUE && index > Integer.MIN_VALUE){
       	Log.i(TAG, "Setting position from saved preferences");
       	lv.setSelectionFromTop(index, top);
       } else {
       	Log.i(TAG, "No saved position found");
       }

       SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
       String theme = sharedPref.getString("pref_theme", getString(R.string.light));
       String size = sharedPref.getString("pref_text_size", getString(R.string.medium));
       Log.i(TAG, "got configured theme " + theme);
       Log.i(TAG, "Got configured size " + size);
       if(currentTheme == null){
       	currentTheme = theme;
       } 
       
       if(currentSize == null){
       	currentSize = size;
       }
       if(!currentTheme.equals(theme) || !currentSize.equals(size)) {
		finish();
		startActivity(getIntent());
	}
}
 
Example 19
Source File: TodoTxtTouch.java    From endpoints-codelab-android with GNU General Public License v3.0 4 votes vote down vote up
void setFilteredTasks(boolean reload) {
	if (reload) {
		try {
			taskBag.reload();
		} catch (Exception e) {
			Log.e(TAG, e.getMessage(), e);
		}
	}

	if (mScrollPosition < 0) {
		calculateScrollPosition();
	}

	m_adapter.clear();
	for (Task task : taskBag.getTasks(FilterFactory.generateAndFilter(
			m_app.m_prios, m_app.m_contexts, m_app.m_projects,
			m_app.m_search, false), m_app.sort.getComparator())) {
		m_adapter.add(task);
	}

	ListView lv = getListView();
	lv.setSelectionFromTop(mScrollPosition, mScrollTop);

	final TextView filterText = (TextView) findViewById(R.id.filter_text);
	final LinearLayout actionbar = (LinearLayout) findViewById(R.id.actionbar);
	final ImageView actionbar_icon = (ImageView) findViewById(R.id.actionbar_icon);

	if (filterText != null) {
		if (m_app.m_filters.size() > 0) {
			String filterTitle = getString(R.string.title_filter_applied)
					+ " ";
			int count = m_app.m_filters.size();

			for (int i = 0; i < count; i++) {
				filterTitle += m_app.m_filters.get(i) + " ";
			}

			if (!Strings.isEmptyOrNull(m_app.m_search)) {
				filterTitle += getString(R.string.filter_tab_search);
			}

			actionbar_icon.setImageResource(R.drawable.ic_actionbar_filter);

			actionbar.setVisibility(View.VISIBLE);
			filterText.setText(filterTitle);
		} else if (!Strings.isEmptyOrNull(m_app.m_search)) {
			if (filterText != null) {
				actionbar_icon
						.setImageResource(R.drawable.ic_actionbar_search);
				filterText.setText(getString(R.string.title_search_results)
						+ " " + m_app.m_search);

				actionbar.setVisibility(View.VISIBLE);
			}
		} else {
			filterText.setText("");
			actionbar.setVisibility(View.GONE);
		}
	}

	m_swipeList.setEnabled(!inActionMode());
}
 
Example 20
Source File: ScrollCompat.java    From SmoothRefreshLayout with MIT License 4 votes vote down vote up
public static boolean scrollCompat(View view, float deltaY) {
    if (view != null) {
        if (view instanceof AbsListView) {
            final AbsListView absListView = (AbsListView) view;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                absListView.scrollListBy((int) deltaY);
            } else if (absListView instanceof ListView) {
                // {@link android.support.v4.widget.ListViewCompat#scrollListBy(ListView,
                // int)}
                final ListView listView = (ListView) absListView;
                final int firstPosition = listView.getFirstVisiblePosition();
                if (firstPosition == ListView.INVALID_POSITION) {
                    return false;
                }
                final View firstView = listView.getChildAt(0);
                if (firstView == null) {
                    return false;
                }
                final int newTop = (int) (firstView.getTop() - deltaY);
                listView.setSelectionFromTop(firstPosition, newTop);
            } else {
                absListView.smoothScrollBy((int) deltaY, 0);
            }
            return true;
        } else if (view instanceof WebView
                || view instanceof ScrollView
                || view instanceof NestedScrollView) {
            view.scrollBy(0, (int) deltaY);
            return true;
        } else if (ViewCatcherUtil.isRecyclerView(view)) {
            RecyclerView recyclerView = (RecyclerView) view;
            if (!(recyclerView.getOnFlingListener() instanceof PagerSnapHelper)) {
                // Fix the problem of adding new data to RecyclerView while in Fling state,
                // the new items will continue to Fling
                if (recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_SETTLING) {
                    recyclerView.stopScroll();
                }
                view.scrollBy(0, (int) deltaY);
            }
            return true;
        }
    }
    return false;
}