我正在编写一个具有NestedScrollView的应用程序。如果滚动已更改,我想处理。我在互联网上搜索,发现我必须创建自定义ScrollView。这是我的代码:
public interface OnScrollChangedListener {
void onScrollChanged(MyScrollView nestedScrollView, int l, int t, int oldl, int oldt);
}
首先,我创建了一个处理滚动更改的界面 然后我创建了自定义NestedScrollView
public class MyScrollView extends NestedScrollView{
private OnScrollChangedListener onScrollChangedListener;
public void setOnScrollChangedListener(OnScrollChangedListener onScrollChangedListener) {
this.onScrollChangedListener = onScrollChangedListener;
}
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if(onScrollChangedListener != null){
onScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
}
}
}
然后我在我的布局中使用了这个自定义的NestedScrollView
<com.example.whatshouldido.app.activities.MyScrollView
android:layout_width="match_parent"
android:clickable="false"
android:scrollbars="none"
android:id="@+id/nested_scroll"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent">
<TableLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:elevation="5dp"
android:padding="2dp"
android:background="@android:color/background_light">
<TableRow>
<FrameLayout android:layout_width="0dp"
android:elevation="5dp"
android:id="@+id/fl_theaters"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:background="@drawable/ripple_efect"
android:layout_marginBottom="2dp"
android:layout_marginEnd="2dp"
android:layout_weight="1"
android:layout_height="150dp">
<TextView android:layout_width="match_parent"
android:textSize="20sp"
android:text="@string/theaters"
android:textColor="@color/color_primary"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"/>
<ImageView android:layout_width="match_parent"
android:id="@+id/iv_theater"
android:src="@mipmap/ic_theaters_white_36dp"
android:scaleType="centerCrop"
android:padding="50dp"
android:layout_height="match_parent"/>
</FrameLayout>
<FrameLayout android:orientation="horizontal"
android:layout_width="0dp"
android:clickable="true"
android:id="@+id/fl_movies"
android:background="@drawable/ripple_efect"
android:layout_marginBottom="2dp"
android:elevation="5dp"
android:layout_weight="1"
android:layout_height="150dp">
<TextView android:layout_width="match_parent"
android:textSize="20sp"
android:text="@string/cinemas"
android:textColor="@color/movie_color"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"/>
<ImageView android:layout_width="match_parent"
android:padding="50dp"
android:id="@+id/iv_movie"
android:src="@mipmap/ic_movie_white_36dp"
android:scaleType="centerCrop"
android:layout_height="match_parent"/>
</FrameLayout>
</TableRow>
<TableRow>
<FrameLayout android:orientation="horizontal"
android:layout_width="0dp"
android:clickable="true"
android:id="@+id/fl_restaurants"
android:background="@drawable/ripple_efect"
android:elevation="5dp"
android:layout_marginBottom="2dp"
android:layout_marginEnd="2dp"
android:layout_weight="1"
android:layout_height="150dp">
<TextView android:layout_width="match_parent"
android:textSize="20sp"
android:text="@string/restaurants"
android:textColor="@color/restaurant_color"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"/>
<ImageView android:layout_width="match_parent"
android:padding="50dp"
android:id="@+id/iv_restaurant"
android:src="@mipmap/ic_restaurant_white_36dp"
android:scaleType="centerCrop"
android:layout_height="match_parent"/>
</FrameLayout>
<FrameLayout android:orientation="horizontal"
android:layout_width="0dp"
android:id="@+id/fl_cafes"
android:layout_marginBottom="2dp"
android:clickable="true"
android:background="@drawable/ripple_efect"
android:elevation="5dp"
android:layout_weight="1"
android:layout_height="150dp">
<TextView android:layout_width="match_parent"
android:textSize="20sp"
android:text="@string/cafes"
android:textColor="@color/cafe_color"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"/>
<ImageView android:layout_width="match_parent"
android:id="@+id/iv_cafe"
android:src="@mipmap/ic_free_breakfast_white_36dp"
android:scaleType="centerCrop"
android:padding="50dp"
android:layout_height="match_parent"/>
</FrameLayout>
</TableRow>
<TableRow>
<FrameLayout android:orientation="horizontal"
android:layout_width="0dp"
android:elevation="5dp"
android:id="@+id/fl_gyms"
android:layout_marginEnd="2dp"
android:clickable="true"
android:background="@drawable/ripple_efect"
android:layout_weight="1"
android:layout_height="150dp">
<TextView android:layout_width="match_parent"
android:textSize="20sp"
android:text="@string/fitness_centers"
android:textColor="@color/fitness_color"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"/>
<ImageView android:layout_width="match_parent"
android:padding="50dp"
android:id="@+id/iv_fitness"
android:src="@mipmap/ic_fitness_center_white_36dp"
android:scaleType="centerCrop"
android:layout_height="match_parent"/>
</FrameLayout>
<FrameLayout android:orientation="horizontal"
android:layout_width="0dp"
android:clickable="true"
android:id="@+id/fl_clubs"
android:background="@drawable/ripple_efect"
android:elevation="5dp"
android:layout_weight="1"
android:layout_height="150dp">
<TextView android:layout_width="match_parent"
android:textSize="20sp"
android:text="@string/clubs"
android:textColor="@color/bar_color"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"/>
<ImageView android:layout_width="match_parent"
android:padding="50dp"
android:id="@+id/iv_bar"
android:src="@mipmap/ic_local_bar_white_36dp"
android:scaleType="centerCrop"
android:layout_height="match_parent"/>
</FrameLayout>
</TableRow>
</TableLayout>
</com.example.whatshouldido.app.activities.MyScrollView>
我在MainActivity的onCreate()中声明了它
MyScrollView myScrollView = (MyScrollView) findViewById(R.id.nested_scroll);
myScrollView.setOnScrollChangedListener(new OnScrollChangedListener() {
@Override
public void onScrollChanged(MyScrollView nestedScrollView, int l, int t, int oldl, int oldt) {
onClick(fabLike);
}
});
但我仍然无法处理滚动更改。我只需要知道滚动是否已经改变。我也试过onTouchListener,但NestedScrollView冻结了。我也试过这个
myScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
onClick(fabLike);
}
});
但它没有帮助。任何帮助,将不胜感激。提前谢谢。