scrollview在Fragment的运用,bar滑动到顶部静止

    xiaoxiao2026-04-24  11

    首先先上图

    第一个布局

    Fragemnt里面的第一种布局 第一种方法 import com.example.CycleUse.MyScrollView; import com.example.CycleUse.MyScrollView.OnScrollListener; import android.support.v4.app.Fragment; import android.content.Context; import android.graphics.PixelFormat; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.WindowManager.LayoutParams; import android.widget.LinearLayout; public class FindLernerFragment extends Fragment implements OnScrollListener { private MyScrollView myScrollView; private LinearLayout mTopBuyLayout; private LinearLayout mBuyLayout; private WindowManager mWindowManager; /** * 手机屏幕宽度 */ private int screenWidth; /** * 悬浮框View */ private static View suspendView; /** * 悬浮框的参数 */ private static WindowManager.LayoutParams suspendLayoutParams; /** * 购买布局的高度 */ private int buyLayoutHeight; /** * myScrollView与其父类布局的顶部距离 */ private int myScrollViewTop; /** * 购买布局与其父类布局的顶部距离 */ private int buyLayoutTop; private View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_findlerner, null); myScrollView = (MyScrollView) view.findViewById(R.id.scrollView); mBuyLayout = (LinearLayout) view.findViewById(R.id.buy); mTopBuyLayout = (LinearLayout) view.findViewById(R.id.top_buy_layout); myScrollView.setOnScrollListener(this); view.findViewById(R.id.parent_layout).getViewTreeObserver() .addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // 这一步很重要,使得上面的购买布局和下面的购买布局重合 onScroll(myScrollView.getScrollY()); System.out.println(myScrollView.getScrollY()); } }); return view; } /** * 窗口有焦点的时候,即所有的布局绘制完毕的时候,我们来获取购买布局的高度和myScrollView距离父类布局的顶部位置 */ public void onScroll(int scrollY) { int mBuyLayout2ParentTop = Math.max(scrollY, mBuyLayout.getTop()); mTopBuyLayout.layout(0, mBuyLayout2ParentTop, mTopBuyLayout.getWidth(), mBuyLayout2ParentTop + mTopBuyLayout.getHeight()); } } 第二种方法 布局 方法 import com.example.CycleUse.MyScrollView; import com.example.CycleUse.MyScrollView.OnScrollListener; import android.support.v4.app.Fragment; import android.content.Context; import android.graphics.PixelFormat; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.WindowManager; import android.view.WindowManager.LayoutParams; import android.widget.LinearLayout; import android.widget.Toast; public class BignewsFragment extends Fragment implements OnScrollListener{ private MyScrollView myScrollView; private LinearLayout mBuyLayout; private WindowManager mWindowManager; /** * 手机屏幕宽度 */ private int screenWidth; /** * 悬浮框View */ private static View suspendView; /** * 悬浮框的参数 */ private static WindowManager.LayoutParams suspendLayoutParams; /** * 购买布局的高度 */ private int buyLayoutHeight; /** * myScrollView与其父类布局的顶部距离 */ private int myScrollViewTop; /** * 购买布局与其父类布局的顶部距离 */ private int buyLayoutTop; private View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.fragment_bignews, null); myScrollView = (MyScrollView) view.findViewById(R.id.scrollView); mBuyLayout = (LinearLayout)view. findViewById(R.id.buy); myScrollView.setOnScrollListener(this); mWindowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE); screenWidth = mWindowManager.getDefaultDisplay().getWidth(); mBuyLayout.post(new Runnable() { @Override public void run() { initHeight(); } }); return view; } /** * 窗口有焦点的时候,即所有的布局绘制完毕的时候,我们来获取购买布局的高度和myScrollView距离父类布局的顶部位置 */ public void initHeight() { buyLayoutHeight = mBuyLayout.getHeight(); buyLayoutTop = mBuyLayout.getTop(); myScrollViewTop = myScrollView.getTop(); } /** * 滚动的回调方法,当滚动的Y距离大于或者等于 购买布局距离父类布局顶部的位置,就显示购买的悬浮框 * 当滚动的Y的距离小于 购买布局距离父类布局顶部的位置加上购买布局的高度就移除购买的悬浮框 * */ @Override public void onScroll(int scrollY) { if(scrollY >= buyLayoutTop){ if(suspendView == null){ showSuspend(); } }else if(scrollY <= buyLayoutTop + buyLayoutHeight){ if(suspendView != null){ removeSuspend(); } } } /** * 显示购买的悬浮框 */ private void showSuspend(){ if(suspendView == null){ suspendView = LayoutInflater.from(getActivity()).inflate(R.layout.buy_layout, null); if(suspendLayoutParams == null){ suspendLayoutParams = new LayoutParams(); suspendLayoutParams.type = LayoutParams.TYPE_PHONE; suspendLayoutParams.format = PixelFormat.RGBA_8888; suspendLayoutParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE; suspendLayoutParams.gravity = Gravity.TOP; suspendLayoutParams.width = screenWidth; suspendLayoutParams.height = buyLayoutHeight; suspendLayoutParams.x = 0; suspendLayoutParams.y = myScrollViewTop; } } mWindowManager.addView(suspendView, suspendLayoutParams); } /** * 移除购买的悬浮框 */ private void removeSuspend(){ if(suspendView != null){ mWindowManager.removeView(suspendView); suspendView = null; } } } 工具类 package com.example.CycleUse; import android.content.Context; import android.util.AttributeSet; import android.widget.ScrollView; /** * @blog http://blog.csdn.net/xiaanming * * @author xiaanming * */ public class MyScrollView extends ScrollView { private OnScrollListener onScrollListener; public MyScrollView(Context context) { this(context, null); } public MyScrollView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * 设置滚动接口 * @param onScrollListener */ public void setOnScrollListener(OnScrollListener onScrollListener) { this.onScrollListener = onScrollListener; } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if(onScrollListener != null){ onScrollListener.onScroll(t); } } /** * * 滚动的回调接口 * * @author xiaanming * */ public interface OnScrollListener{ /** * 回调方法, 返回MyScrollView滑动的Y方向距离 * @param scrollY * 、 */ public void onScroll(int scrollY); } }                                                                                           8月16日伟哥写

    转载请注明原文地址: https://ju.6miu.com/read-1309186.html
    最新回复(0)