2015-03-30 2 views
0

У меня есть LinearLayout (вертикальный), инкапсулированный в ScrollView. LinearLayout загружается динамически, и я хочу загрузить больше данных, когда нижняя страница страницы будет достигнута после прокрутки.android linear layout: y и height никогда не соответствуют (onScrollChanged)

Чтобы знать, когда нижняя страница будет достигнут, я перекрытый метод ScrollView.onScrollChanged():

public class VerticalScrollView extends ScrollView { 

    private IVerticalScrollListener listener; 
    private LinearLayout list; 

    public VerticalScrollView(Context context, AttributeSet attributes) { 
     super(context, attributes); 
    } 

    public void setListener(IVerticalScrollListener listener) { 
     this.listener = listener; 
    } 

    @Override 
    protected void onScrollChanged(int x, int y, int oldx, int oldy) { 
     super.onScrollChanged(x, y, oldx, oldy); 

     if (list == null) { 
      list = (LinearLayout) findViewById(R.id.list); 
     } 

     final int bottom = list.getMeasuredHeight(); 

     if (y >= bottom) { //never occures 
      listener.onPageBottomReached(); 
     } 
    } 

} 

Неприятность состоит в том, что «у» значение всегда (пока) меньше, чем высота LinearLayout.

Почему? Спасибо.

+0

Возможно, вам стоит использовать это? [Ответить] [1] [1]: http://stackoverflow.com/questions/10316743/detect-end-of-scrollview –

ответ

0

Вы не должны определять это по высоте. Вместо этого вы должны обнаружить элемент, который в настоящее время находится на экране пользователя.

This link может помочь.

+0

Спасибо за ваш ответ. Как использовать ListView http://www.vogella.com/tutorials/AndroidListView/article.html – hadf

0

Код, который я использую для определения конца прокрутки. Таймер так, что конечное событие не срабатывает многократно, когда конец достигнут.

import android.content.Context; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.View; 
import android.widget.ScrollView; 

public class EndDetectableScrollView extends ScrollView { 

    private long mLastEndCall = 0; 
    private ScrollViewListener scrollViewListener = null; 

    public EndDetectableScrollView(Context context) { 
     super(context); 

    } 

    public EndDetectableScrollView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

    } 

    public EndDetectableScrollView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 


    public void setScrollViewListener(ScrollViewListener scrollViewListener) 
    { 
     this.scrollViewListener = scrollViewListener; 
    } 

    @Override 
    protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
     super.onScrollChanged(l, t, oldl, oldt); 

     if (scrollViewListener != null) { 
     View view = (View) getChildAt(0); 
      int diff = (view.getBottom()-(getHeight()+getScrollY()+view.getTop())); 
      if (diff <= 0) { 
       long currentTime = System.currentTimeMillis(); 
       if (currentTime > mLastEndCall + 1000) 
       { 
       Log.d("TAG","CurrentTime: " + currentTime + " lastTime: " + mLastEndCall); 
       mLastEndCall = currentTime; 
       scrollViewListener.onScrollEnd(); 
       } 
      } 

     } 
    } 
} 
Смежные вопросы