ScrollView上滑顶部布局被覆盖,下滑顶部布局显示,顶部布局有两个点击事件
1条回答 默认 最新
- MaizyLau 2019-03-25 16:53关注
1. 顶部1,顶部2布局需要完全一样,初始化时顶部1不显示,为gone。
2. 当顶部2向上滑动到屏幕顶端时,设置顶部1可见,由于顶部1不包含在ScrollVIew里面,所以会一直存在,不会被滑动;
而向下滑时,当顶部2到达屏幕顶端,隐藏顶部1,显示顶部2即可。
所以实现关键步骤是:
判断顶部2是否滑动到了顶部,也就是标题栏之下。
先看布局,很简单:
布局和原理里面一致:顶部1,ScrollView 就没了。
只是有一个自定义的ScrollView,这个是重点,在代码里面讲,布局也没什么。
记得顶部1不在ScrollView里面,二顶部2在ScrollView里面哦~
<tao.demo.testscrollview.view.MyScrollView
android:id="@+id/myscroview"
android:layout_width="match_parent"
android:layout_height="match_parent"<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="match_parent" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@mipmap/ic_launcher" /> <RelativeLayout android:id="@+id/rl2" android:layout_width="match_parent" android:layout_height="50dp" android:background="#66ff0000"> <TextView android:textSize="20sp" android:textColor="#ffffff" android:gravity="center" android:text="滑动到顶部悬浮" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> <android.support.v7.widget.RecyclerView android:id="@+id/mrecycle" android:layout_width="match_parent" android:layout_height="match_parent"></android.support.v7.widget.RecyclerView> </LinearLayout>
<RelativeLayout
android:id="@+id/rl1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#aa00ff00"
android:visibility="gone"<TextView android:textSize="20sp" android:textColor="#ffffff" android:gravity="center" android:text="滑动到顶部悬浮" android:layout_width="match_parent" android:layout_height="match_parent" />
接下来就看自定义的ScrollView:
这里面关键的就是onScrollChanged这个方法,实现了ScrollView滑动距离的监听。
public class MyScrollView extends ScrollView {
private static StopCall stopCall; //ScrollView向上滑动到顶部的距离 private int upH; public MyScrollView(Context context) { super(context); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); //赋值:200很重要,这个值是顶部2上面的高度 upH = dpTopx(200);//单位是dp } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public static void setCallback(StopCall c) { stopCall = c; } /** * 关键部分在这里,测量当前ScrollView滑动的距离 * <p> * 其中t就是,单位是px,不是dp * <p> * stopCall是一个接口,是为了在Activity中实现设置顶部1/2可不可见 */ @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (t > upH) {//如果向上滑动距离>本例中图片高度 stopCall.stopSlide(true);//设置顶部1可见,顶部2不可见 } else {//否则 stopCall.stopSlide(false);//设置顶部1不可见,顶部2可见 } } /** * F: 将dp转成为px */ private int dpTopx(int dpValue) { final float scale = this.getResources().getDisplayMetrics().density; return (int) (dpValue * scale + 0.5f); } public interface StopCall { void stopSlide(boolean isStop); }
}
在activity中
public class MainActivity extends AppCompatActivity implements MyScrollView.StopCall{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myscroview.setCallback(this);//设置myscrollview滑动的监听
}@Override
public void stopSlide(boolean isStop) {//根据滑动距离显示或隐藏顶部1,顶部2
if (isStop) {
rl2.setVisibility(View.INVISIBLE);
rl1.setVisibility(View.VISIBLE);
} else {
rl1.setVisibility(View.INVISIBLE);
rl2.setVisibility(View.VISIBLE);
}}
}
作者:江月明
来源:CSDN
原文:https://blog.csdn.net/lst_shuo/article/details/78280918
版权声明:本文为博主原创文章,转载请附上博文链接!解决 无用评论 打赏 举报
悬赏问题
- ¥60 版本过低apk如何修改可以兼容新的安卓系统
- ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
- ¥50 有数据,怎么建立模型求影响全要素生产率的因素
- ¥50 有数据,怎么用matlab求全要素生产率
- ¥15 TI的insta-spin例程
- ¥15 完成下列问题完成下列问题
- ¥15 C#算法问题, 不知道怎么处理这个数据的转换
- ¥15 YoloV5 第三方库的版本对照问题
- ¥15 请完成下列相关问题!
- ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?