我在滚动视图中的文本视图中有一个固定的内容.当用户滚动到某个位置时,我想开始一个活动或触发一个Toast.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/scroller">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent" android:id="@+id/story"
android:layout_height="wrap_content" android:text="@string/lorem"
android:gravity="fill" />
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
我的问题是实现受保护的方法onScrollChanged来找出滚动视图的位置.
我找到了这个答案,是否有一个更简单的解决方案,而不是声明一个界面,覆盖滚动视图等,如我发布的链接上所示?
我已经实现了一个recyclerview,我在其中添加纹理视图作为列表项来播放来自url的视频.现在像藤蔓和Instagram应用程序我想播放视频,当它在recyclerview中可见,并在listitem离开屏幕时停止/暂停视频.以下是我的代码:
VideosAdapter类:
public class VideosAdapter extends RecyclerView.Adapter<VideosAdapter.ViewHolder> {
private static String TAG = "VideosAdapter";
Context context;
private ArrayList<String> urls;
RecyclerView recyclerView;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextureView textureView;
public TextView textView;
public ViewHolder(View v) {
super(v);
textureView = (TextureView) v.findViewById(R.id.textureView);
textView = (TextView) v.findViewById(R.id.textView);
}
}
public VideosAdapter(Context context, RecyclerView recyclerView, final ArrayList<String> urls) {
this.context = context;
this.recyclerView = recyclerView;
this.urls = urls;
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { …Run Code Online (Sandbox Code Playgroud) android-scrollview android-textureview android-recyclerview onscrollchanged
我已经实现了一个侦听器类来检测滚动视图的结尾,参考链接https://gist.github.com/marteinn/9427072
public class ResponsiveScrollView extends ScrollView {
private OnBottomReachedListener listener;
public ResponsiveScrollView(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());
if (diff == 0 && listener!= null) {
listener.onBottomReached(this);
}
super.onScrollChanged(l, t, oldl, oldt);
}
public OnBottomReachedListener getBottomChangedListener() {
return listener;
}
public void setBottomReachesListener(OnBottomReachedListener onBottomReachedListener) {
this.listener = onBottomReachedListener;
}
public interface OnBottomReachedListener {
public void onBottomReached(View view);
}
}
Run Code Online (Sandbox Code Playgroud)
监听器设置为scrollView:
scrollView.setBottomReachesListener(new …Run Code Online (Sandbox Code Playgroud) android scrollview android-scrollview onscrolllistener onscrollchanged
我正在编写一个具有 NestedScrollView 的应用程序。我想处理滚动是否发生变化。我在互联网上搜索,发现我必须创建自定义 ScrollView。这是我的代码:
public interface OnScrollChangedListener {
void onScrollChanged(MyScrollView nestedScrollView, int l, int t, int oldl, int oldt);
}
Run Code Online (Sandbox Code Playgroud)
首先,我创建了一个界面来处理滚动更改,然后我创建了自定义 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, …Run Code Online (Sandbox Code Playgroud)