I use 2 controller for my project, when i click imgButton with air mouse my ListScroll is well but if i click dpad ok button scroll not working optimize. What can i do for this case. My code here;
imgNextPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pDialogList.setVisibility(View.VISIBLE);
scrollDown();
prevCount = 0;
}
});
scrollDown method is here
private void scrollDown() {
grd.setFastScrollEnabled(false);
grd.smoothScrollToPositionFromTop(adp.getCount(), 0, 1000);
grd.setSelection(newAdapter - oldAdapter);
grd.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == 0) {
grd.requestFocusFromTouch();
grd.setSelection(newAdapter);
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
}
});
}
Related
I can detected when I scroll until the very bottom using
.size() - 1 of my elem. But if I start to scroll up at the middle of the list, how can I know it, in the method GetView() of course.
Found a way to do it which works more or less well...
list.setOnScrollListener(new AbsListView.OnScrollListener() {
private int mLastFirstVisibleItem;
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if(mLastFirstVisibleItem<firstVisibleItem) /* DOWN */
{
scrolling = false;
}
if(mLastFirstVisibleItem>firstVisibleItem) /* UP*/
{
scrolling = true;
}
mLastFirstVisibleItem=firstVisibleItem;
}
});
How can i get the event that is written in title? I want to know if that RawLastChild is visible on screen or not.
How can i catch event?
You can use this code for expandablelistview to see the last element
mExpandableListView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount
, int totalItemCount) {
if(firstVisibleItem+visibleItemCount>=totalItemCount){
//the last item is visible
}
}
});
Following code snippet will solve your problem. Tested and working fine!
expandableListView.setOnScrollListener(new AbsListView.OnScrollListener() {
#TargetApi(Build.VERSION_CODES.KITKAT)
#Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
if (!absListView.canScrollList(View.SCROLL_AXIS_VERTICAL) && scrollState == SCROLL_STATE_IDLE) {
//When List reaches bottom
loadMoreData();
}
}
#Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
Everytime I scroll the list up or down I hide (or unhide) some views with OnScrollListener. Here is the code attached to my ListView.
lv.setOnScrollListener(new OnScrollListener() {
private int mLastFirstVisibleItem;
private boolean mIsScrollingUp = true;
private LinearLayout ll = (LinearLayout) getActivity()
.findViewById(R.id.llSearchPlaces);
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (view.getId() == lv.getId()) {
final int currentFirstVisibleItem = lv
.getFirstVisiblePosition();
if (currentFirstVisibleItem > mLastFirstVisibleItem) {
if (mIsScrollingUp == true) {
mIsScrollingUp = false;
Log.i("a", "scrolling down...");
floatingActionButton.hide();
Animation animation = new TranslateAnimation(0, 0,
0, 200);
animation.setDuration(300);
animation
.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(
Animation animation) {
ll.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(
Animation animation) {
}
#Override
public void onAnimationStart(
Animation animation) {
}
});
ll.startAnimation(animation);
}
} else if (currentFirstVisibleItem < mLastFirstVisibleItem) {
if (mIsScrollingUp == false) {
mIsScrollingUp = true;
floatingActionButton.show();
Log.i("a", "scrolling up...");
Animation animation = new TranslateAnimation(0, 0,
200, 0);
animation.setDuration(400);
animation
.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(
Animation animation) {
}
#Override
public void onAnimationRepeat(
Animation animation) {
}
#Override
public void onAnimationStart(
Animation animation) {
ll.setVisibility(View.VISIBLE);
}
});
ll.startAnimation(animation);
}
}
mLastFirstVisibleItem = currentFirstVisibleItem;
}
}
});
Layout:
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="true"
android:listSelector="#00000000"
android:scrollbars="none" />
</android.support.v4.widget.SwipeRefreshLayout>
Ever since I added the SwipeRefreshLayout, I do not get anything when I Log inside the listener above. How can I use both of these items together?
EDIT: It seems like this is what I need but I can't make it work still
As part of that article I added this in onScroll, tho it doesn't seem to work.
if (firstVisibleItem == 0) {
swipeLayout.setEnabled(true);
} else {
swipeLayout.setEnabled(false);
}
EDIT2: THIS IS THE HEART OF THE ISSUE: It seems the onScroll method fires when the Activity first starts and the list loads and then never more again.
I had the same issue with RecyclerView and ListView. Scrolling downwards for whatever amount of items, it was impossible to return to the top of the list.
This will disable the SwipeRefreshLayout until the first visible item or any item position is visible. You can also bind different scroll listeners along this one. Make sure you enable (if previously disabled) SwipeRefreshLayout whenever you repopulate the listivew.
public class SwipeRefreshLayoutToggleScrollListenerListView implements AbsListView.OnScrollListener {
private List<AbsListView.OnScrollListener> mScrollListeners = new ArrayList<AbsListView.OnScrollListener>();
private int mExpectedVisiblePosition = 0;
public SwipeRefreshLayoutToggleScrollListenerListView(SwipeRefreshLayout mSwipeLayout) {
this.mSwipeLayout = mSwipeLayout;
}
private SwipeRefreshLayout mSwipeLayout;
public void addScrollListener(AbsListView.OnScrollListener listener){
mScrollListeners.add(listener);
}
public boolean removeScrollListener(AbsListView.OnScrollListener listener){
return mScrollListeners.remove(listener);
}
public void setExpectedFirstVisiblePosition(int position){
mExpectedVisiblePosition = position;
}
private void notifyOnScrolled(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount){
for(AbsListView.OnScrollListener listener : mScrollListeners){
listener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
}
}
private void notifyScrollStateChanged(AbsListView view, int scrollState){
for(AbsListView.OnScrollListener listener : mScrollListeners){
listener.onScrollStateChanged(view, scrollState);
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
notifyScrollStateChanged(view, scrollState);
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
notifyOnScrolled(view, firstVisibleItem, visibleItemCount, totalItemCount);
if(firstVisibleItem != RecyclerView.NO_POSITION)
mSwipeLayout.setEnabled(firstVisibleItem == mExpectedVisiblePosition);
}
}
Edit:
lv.setOnScrollListener(new SwipeRefreshLayoutToggleScrollListenerListView(mSwiperLayout){
//override methods here, don't forget the super calls.
});
yourListview.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
boolean enable = false;
if(timelogListView != null && timelogListView.getChildCount() > 0){
boolean firstItemVisible = timelogListView.getFirstVisiblePosition() == 0;
boolean topOfFirstItemVisible = timelogListView.getChildAt(0).getTop() == 0;
enable = firstItemVisible && topOfFirstItemVisible;
}
swipeLayout.setEnabled(enable);
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
if(flag_loading == false)
{
flag_loading = true;
ConnectionDetector conn = new ConnectionDetector(getActivity());
if(conn.isConnectingToInternet()){
// call for your pagination async
}else{
}
}
}
}
});
By default : private boolean flag_loading = false; and in your pagination async if(YourListview.size() < 20) { flag_loading = true; } else{ flag_loading = false; }
You should override canChildScrollUp of SwipeRefreshView. This method is polled for the purpose of knowing whether the contained View wants to scroll up:
An extension with a settable interface for any arbritrary contained view:
public class SwipeRefreshWrapper extends SwipeRefreshLayout {
private ScrollResolver mScrollResolver;
public SwipeRefreshWrapper(Context context) {
super(context);
}
public SwipeRefreshWrapper(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setScrollResolver(ScrollResolver scrollResolver) {
mScrollResolver = scrollResolver;
}
#Override
public boolean canChildScrollUp() {
if(mScrollResolver != null){
return mScrollResolver.canScrollUp();
}else {
return super.canChildScrollUp();
}
}
public static interface ScrollResolver{
public boolean canScrollUp();
}
}
Usage (ListView also has a nice method: canScrollVertically()):
final SwipeRefreshWrapper wrapper = (SwipeRefreshWrapper) rootView.findViewById(R.id.wrapper);
final ListView list = (ListView) rootView.findViewById(R.id.list);
wrapper.setScrollResolver(new SwipeRefreshWrapper.ScrollResolver() {
#Override
public boolean canScrollUp() {
return list.canScrollVertically(-1);
}
});
Hi all I have listview in my application but after scrolling to bottom it again sets to first position.
listView = (ListView) findViewById(R.id.list);
start = listView.getFirstVisiblePosition();
listView.setSelectionFromTop(start, 0);
after removing following code it works fine but after scrolling setOnItemClickListener does not work.
listView.setOnScrollListener(new OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
if ( scrollState == OnScrollListener.SCROLL_STATE_IDLE )
{
listView.invalidateViews();
}
}
#Override
public void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {}
});
Any suggestion will be appreciated. Thanks in advance
Try this way,hope this will help you to solve your problem.
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
int lastInScreen = firstVisibleItem + visibleItemCount;
if((lastInScreen == totalItemCount)){
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
listView.smoothScrollToPosition(0);
}
},1000); // change (1000) value as per your requirement like : when list shown last item then after 1 second list scrolled to 0 position
}
}
});
I'm trying to hide a textView while scrolling a grid view and make the same textview visible again when the user stops scrolling.
Can anyone help me fixing this issue..?
implement OnScrollListener and then inside the onScroll make the textView invisible
and in the OnScrollStateChanged method check if the scroll stopped and make it visibile again
private OnScrollListener mScrollListener = new OnScrollListener() {
#Override
public void onScrollStateChanged(ViewGroup view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
textView.setVisibility = View.VISIBLE;
}
#Override
public void onScroll(ViewGroup view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
textView.setVisibility = View.INVISIBLE
}
};
You can use setVisibility in OnScrollListener
Example:
gridview.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
textview.setVisibility(View.VISIBLE);
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
textview.setVisibility(View.GONE);
}
});
Hope it helps.
gridView.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
textview.setVisibility(View.VISIBLE);
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
textview.setVisibility(View.GONE);
}
});
We use this code in my app and it work , I hope it will useful to you.
Try this code:
private OnScrollListener mScrollListener = new OnScrollListener() {
#Override
public void onScrollStateChanged(ViewGroup view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
tv.setVisibility(View.VISIBLE);
}
}
#Override
public void onScroll(ViewGroup view, int firstVisibleItem, int visibleItemCount, int totalount)
{
tv.setVisibility(View.INVISIBLE);
}
});
Try this:
Private OnScrollListener mScrollListener = new OnScrollListener() {
#Override
public void onScrollStateChanged(ViewGroup view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
mTextView.setVisibility = View.VISIBLE;
}
#Override
public void onScroll(ViewGroup view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
mTextView.setVisibility = View.INVISIBLE
}
};
You need to set this method on your gridview. it works for me. I am sure it will help you.
gridAdmin.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE)
{
text1.setVisibility = View.VISIBLE;
}
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
text1.setVisibility = View.INVISIBLE
}
});