资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Android开发中如何将滑动组件固定在顶部-创新互联

今天就跟大家聊聊有关Android开发中如何将滑动组件固定在顶部,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

创新互联服务项目包括路桥网站建设、路桥网站制作、路桥网页制作以及路桥网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,路桥网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到路桥省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

效果是如下:

Android开发中如何将滑动组件固定在顶部

场景:有些时候是内容中间的组件当滑动至顶部的时候固定显示在顶部。

实现的思路:

1.目标组件(button)有两套,放在顶部和内容中间;

2.当内容中间的组件滑动至顶部栏位置时控制显示/隐藏顶部和中间的组件(涉及到组件获取在屏幕的位置知识点);

activity代码:

public class MainActivity extends AppCompatActivity implements ObservableScrollView.ScrollViewListener { 
 private ObservableScrollView scrollView; 
 private Button topBtn1, topBtn2, middleBtn1, middleBtn2; 
 private View topPanel, middlePanel; 
 private int topHeight; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
  initViews(); 
  initListeners(); 
 
 } 
 
 @Override 
 public void onWindowFocusChanged(boolean hasFocus) { 
  super.onWindowFocusChanged(hasFocus); 
 
  Rect frame = new Rect(); 
  getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 
  int statusBarHeight = frame.top;//状态栏高度 
 
  int titleBarHeight = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();//标题栏高度 
  topHeight = titleBarHeight + statusBarHeight; 
 } 
 
 
 private void initViews() { 
  scrollView = (ObservableScrollView) findViewById(R.id.scrollView); 
  topPanel = findViewById(R.id.topPanel); 
  topBtn1 = (Button) topPanel.findViewById(R.id.button1); 
  topBtn2 = (Button) topPanel.findViewById(R.id.button2); 
 
  middlePanel = findViewById(R.id.middlePanel); 
  middleBtn1 = (Button) middlePanel.findViewById(R.id.button1); 
  middleBtn2 = (Button) middlePanel.findViewById(R.id.button2); 
 
 } 
 
 private void initListeners() { 
  topBtn1.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View view) { 
    middleBtn1.setBackgroundColor(Color.WHITE); 
    topBtn1.setBackgroundColor(Color.WHITE); 
   } 
  }); 
 
  middleBtn1.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View view) { 
    middleBtn1.setBackgroundColor(Color.BLUE); 
    topBtn1.setBackgroundColor(Color.BLUE); 
   } 
  }); 
 
  scrollView.setScrollViewListener(this); 
 
 
 } 
 
 
 @Override 
 public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) { 
  int[] location = new int[2]; 
  middleBtn1.getLocationOnScreen(location); 
  int locationY = location[1]; 
  Log.e("locationY", locationY + " " + "topHeight的值是:" + topHeight); 
 
  if (locationY <= topHeight && (topPanel.getVisibility() == View.GONE || topPanel.getVisibility() == View.INVISIBLE)) { 
   topPanel.setVisibility(View.VISIBLE); 
  } 
 
  if (locationY > topHeight && topPanel.getVisibility() == View.VISIBLE) { 
   topPanel.setVisibility(View.GONE); 
  } 
 
 } 
} 

文章标题:Android开发中如何将滑动组件固定在顶部-创新互联
文章来源:http://cdkjz.cn/article/dcegsd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220