2016-12-03 4 views
0

У меня есть обложка прокрутки много элементов внутри. И я хочу проверить, прокручивается ли прокрутка в нижней части scrool. Как это реализовать. пожалуйста, любой меня пересчитайте. Благодарю.Как проверить, прокручивается ли прокрутка до нижней ступени андроида

<ScrollView 
    android:id="@+id/scrollview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" > 

     <LinearLayout 
      android:id="@+id/layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" 
      android:showDividers="end|middle" > 
       <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text=" 
       text....... 
       " /> 
       <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text=" 
        text....... 
       " /> 
     </LinearLayout> 
    </ScrollView> 
+0

Я предлагаю использовать ListView или вид Recycler для прокрутки –

+0

@NitinKarande If Я использую Recyclerview или listview, которые мне нужны для пользовательского нижнего колонтитула заголовка .... трудно для меня. –

+0

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

ответ

1

Создать пользовательский Scrollview следующим образом:

общественный класс CustomScrollView простирается Scrollview {OnBottomReachedListener mListener;

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

public CustomScrollView(Context context, AttributeSet attrs) { 
super(context, attrs); 
} 

public CustomScrollView(Context context) { 
super(context); 
} 

@Override 
protected void onScrollChanged(int l, int t, int oldl, int oldt) { 
View view = getChildAt(getChildCount() - 1); 
int diff = (view.getBottom() - (getHeight() + getScrollY())) - view.getPaddingBottom(); 

if (diff <= 0 && mListener != null) { 
    mListener.onBottomReached(); 
} 

super.onScrollChanged(l, t, oldl, oldt); 
} 

// Getters & Setters 

public OnBottomReachedListener getOnBottomReachedListener() { 
return mListener; 
} 

public void setOnBottomReachedListener(
    OnBottomReachedListener onBottomReachedListener) { 
mListener = onBottomReachedListener; 
} 

//Event listener. 

public interface OnBottomReachedListener { 
public void onBottomReached(); 
} 

}

В вашей основной деятельности:

CustomScrollView Scrollview = (CustomScrollView) findViewById (R.id.scrollView);

scrollView.setOnBottomReachedListener (новый CustomScrollView.OnBottomReachedListener() {

@Override 
    public void onBottomReached() { 
     // ScrollView Reached bottom 
    } 
}); 

В файле XML:

<CustomScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="wrap_content" > 

</CustomScrollView> 
+0

error активность ComponentInfo {kh.com.iknow.scrolllistener/kh.com.iknow.scrolllistener.MainActivity}: android.view.InflateException: двоичная строка XML-файла # 13: ошибка раздувания класса CustomScrollView

+0

В вашем xml вы должны поместить полный путь своего CustomScrollView. Например, я поместил свой CustomScrollView в пакет com.mysample.customviews , тогда мне нужно написать

+0

Он отлично работает благодаря –

0

Вы можете сделать это, сравнивая высоту ScrollView и ваш LinearLayout:

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollview); 
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.layout); 

if(linearLayout.getMeasuredHeight() <= scrollView.getScrollY() + 
     scrollView.getHeight()) { 
    //scroll view is at the very bottom 
} 
else { 
    //scroll view is in somewhere middle 
} 
+0

Это не работает –

+0

Конечно, потому что этот код называется сразу. –