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

本文介绍了如何在Fragment中使用ScrollView,并实现滚动到顶部时保持静止的效果。通过实例展示了具体的布局设置和交互处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先先上图

第一个布局

  
  
  
      
  
  
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.youkuaiyun.com/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日伟哥写









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值