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

The following examples show how to use android.widget.ListView#setSelectionFromTop() . These examples are extracted from open source projects. 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
@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
@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
@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 Project: V.FlyoutTest   File: ListViewAutoScrollHelper.java    License: 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 Project: COCOQuery   File: AbstractViewQuery.java    License: 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 Project: guideshow   File: ListViewAutoScrollHelper.java    License: 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
/**
 * 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 8
/**
 * 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 9
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 10
Source Project: Dashchan   File: ListPosition.java    License: 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 11
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 12
Source Project: YiBo   File: MentionsListAdapter.java    License: 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 13
Source Project: YiBo   File: CommentsListAdapter.java    License: 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 14
Source Project: YiBo   File: MyHomeListAdapter.java    License: 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 15
Source Project: YiBo   File: DirectMessagesListAdapter.java    License: 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 16
Source Project: Onosendai   File: ScrollState.java    License: 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 17
@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 18
Source Project: SmoothRefreshLayout   File: ScrollCompat.java    License: 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;
}
 
Example 19
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
@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());
	}
}