需求:现要实现一个特殊UI的处理,如下图所示:

该布局的上面是一个“按钮”,中间是一个“空白布局(当然也可以是ViewPager等)”,下面是一个页面的导航菜单,底部是一个ListView。

要求:滑动ListView“左边”、“右边”按钮跟着listview滑动,当“左边”、“右边”按钮遇到最上面的那个菜单时“左边”、“右边”按钮悬停,并且listView仍然能够继续滑动,当listview向下滑动时“左边”、“右边”按钮再次跟着listview联动,依次反复。

实现原理:“左边”、“右边”按钮这个导航菜单其实是两个,一个在布局时隐藏掉(固定到顶部菜单下面)另一个则和整个布局是一个整体,当整体的那个“左边”、“右边”按钮碰到顶部菜单时,把整体的那个“左边”、“右边”按钮隐藏到,显示固定的那个“左边”、“右边”按钮。几本原理就是那样了。哈哈。

上代码:

LinkAgeActivity.java

package com.yw.myapiupdate.toscrollviewlinkage;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView; import com.yw.myapiupdate.R; public class LinkAgeActivity extends Activity{
private MyListView myLv;
private MyScrollView mySv;
private LinearLayout linear_middle;
private LinearLayout linear_dong;
private LinearLayout linear_jing; int[] location = new int[2];
int[] location2 = new int[2];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linkage_layout);
initViews();
} private void initViews(){
myLv = (MyListView)findViewById(R.id.linkage_lv);
mySv = (MyScrollView)findViewById(R.id.linkage_scroll);
linear_middle = (LinearLayout)findViewById(R.id.linkage_linear_middle);
linear_dong = (LinearLayout)findViewById(R.id.linkage_linear_menudong);
linear_jing = (LinearLayout)findViewById(R.id.linkage_linear_jing);
LinkAgeBaseAdaper adapter = new LinkAgeBaseAdaper();
myLv.setAdapter(adapter);
setListViewHeightBasedOnChildren(myLv);
mySv.setOnTouchListener(new OnTouchListener(){
private int lastY = 0;
private int touchEventId = 10000;
Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == touchEventId) {
if (lastY != mySv.getScrollY()) {
//scrollview一直在滚动,会触发
handler.sendMessageDelayed(
handler.obtainMessage(touchEventId, mySv), 5);
lastY = mySv.getScrollY();
linear_dong.getLocationOnScreen(location);
linear_jing.getLocationOnScreen(location2);
//动的到静的位置时,静的显示。动的实际上还是网上滚动,但我们看到的是静止的那个
if (location[1] <= location2[1]) {
linear_jing.setVisibility(View.VISIBLE);
} else {
//静止的隐藏了
linear_jing.setVisibility(View.GONE);
}
}
}
}
}; @Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_MOVE://移动
handler.sendEmptyMessage(touchEventId);
break;
case MotionEvent.ACTION_UP://抬起
handler.sendEmptyMessageDelayed(touchEventId, 5);
break;
}
return false;
} }); } class LinkAgeBaseAdaper extends BaseAdapter{ @Override
public int getCount() {
return 6;
} @Override
public Object getItem(int arg0) {
return null;
} @Override
public long getItemId(int arg0) {
return arg0;
} @Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
LayoutInflater inflater = LayoutInflater.from(LinkAgeActivity.this);
if(convertView == null){
convertView = inflater.inflate(R.layout.linkage_item_layout, null);
}
return convertView;
} }
/**动态改变listView的高度*/
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
// totalHeight += 80;
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
// params.height = 80 * (listAdapter.getCount() - 1);
// params.height = 80 * (listAdapter.getCount());
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
((MarginLayoutParams) params).setMargins(0, 0, 0, 0);
listView.setLayoutParams(params); }
}

对应布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#99cc99"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="这是一个菜单"/>
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
> <ScrollView
android:id="@+id/linkage_scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <LinearLayout
android:id="@+id/linkage_linear_middle"
android:layout_width="fill_parent"
android:layout_height="120dp"
android:background="#ffcc00" >
<TextView
android:layout_width="fill_parent"
android:layout_height="120dp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linkage_linear_menudong"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="左边按钮"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="右边按钮"/>
</LinearLayout>
<ListView
android:id="@+id/linkage_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
> </ListView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/linkage_linear_jing"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff6600"
android:orientation="horizontal"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="左边按钮"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="右边按钮"/>
</LinearLayout>
</FrameLayout> </LinearLayout>

listview的item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#ff6600"/> </LinearLayout>

好了,本文到此结束,欢迎大家提出不同的解决方案

推荐链接:http://blog.csdn.net/xiaanming/article/details/17761431

Android实现导航菜单随着ListView联动,当导航菜单遇到顶部菜单时停止在哪里,并且listview仍能滑动的更多相关文章

  1. iOS_21团购_顶部菜单和弹出菜单联动

    最后效果图: 各控件关系图1: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize ...

  2. FineUI大版本升级,外置ExtJS库、去AXD化、表格合计行、表格可编辑单元格的增删改、顶部菜单框架

    这是一篇很长的文章,在开始正文之前,请允许我代表目前排名前 20 中唯一的 .Net 开源软件 FineUI 拉下选票: 投票地址: https://code.csdn.net/2013OSSurve ...

  3. Android ListView两种长按弹出菜单方式

    转自:http://www.cnblogs.com/yejiurui/p/3247527.html package com.wyl.download_demo; import java.util.Ar ...

  4. 仿饿了点餐界面2个ListView联动

    如图是效果图 是仿饿了的点餐界面 1.点击左侧的ListView,通过在在适配器中设置Item来改变颜色,再通过notifyDataSetInvalidated来刷新并用lv_home.setSele ...

  5. 仿饿了吗点餐界面两个ListView联动效果

    这篇文章主要介绍了仿饿了点餐界面2个ListView联动效果的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下 如图是效果图: 是仿饿了的点餐界面 1.点击左侧的ListView,通过在在适 ...

  6. 不得不吐槽的Android PopupWindow的几个痛点(实现带箭头的上下文菜单遇到的坑)

    说到PopupWindow,我个人感觉是又爱又恨,没有深入使用之前总觉得这个东西应该很简单,很好用,但是真正使用PopupWindow实现一些效果的时候总会遇到一些问题,但是即便是人家的api有问题, ...

  7. 解析ListView联动的实现--仿饿了么点餐界面

    一.博客的由来 大神王丰蛋哥 之前一篇博客仿饿了点餐界面2个ListView联动(http://www.cnblogs.com/wangfengdange/p/5886064.html) 主要实现了2 ...

  8. Android仅2步实现 滚粗 汉堡导航栏效果~ 全新底部导航交互(滑动隐藏)

    本文同步自wing的地方酒馆 布吉岛大家有木有看这一篇文章,再见,汉堡菜单,我们有了新的 Android 交互设计方案 本库下载地址:https://github.com/githubwing/Bye ...

  9. 仿美团外卖,饿了吗 两个ListView联动,左边点击切换右边,右边滑动切换左边

    先上效果图: 实现思路: 1.先说右边标题: 首先,右边的数据源集合中的Javabean中含有三个属性name,type,title,而每个条目中会默认含有一个标题. 如果这是第一个条目,就让标题显示 ...

随机推荐

  1. js同时获取多个同name的input框的值

    demo代码 <!doctype html> <html ng-app="a3_4"> <head> <title>表头排序< ...

  2. vue webpack 打包后css背景图路径问题

    最近在写vue-webpack项目时,打包后遇到了css背景图片路径报错的问题 奇怪的是,通过img标签引入的图片路径却没有问题,看来是webpack在打包后,读取css中图片的相对路径出错了. 稍微 ...

  3. photoSwiper移动端图片双击手势缩放

    首先引入几个文件: <link rel="stylesheet" type="text/css" href="css/photoswipe.cs ...

  4. VC中添加头文件以及库

    原文:http://blog.csdn.net/lwb102063/article/details/52068389   附加头文件包含 VC6.0中: VC6.0默认include包含路径:Tool ...

  5. C# 获得目录下所有文件或指定文件类型文件(包含所有子文件夹)

    public partial class FileGet { /// <summary> /// 私有变量 /// </summary> private static List ...

  6. MySQL——索引实现原理

    在MySQL中,索引属于存储引擎级别的概念,不同存储引擎对索引的实现方式是不同的,本文主要讨论MyISAM和InnoDB两个存储引擎的索引实现方式. MyISAM索引实现 MyISAM引擎使用B+Tr ...

  7. LeetCode题解之Happy Number

    1.题目描述 2.题目分析 根据 happy number 的 性质,如果循环7次还没有到达 1,则这个数不是happy number . 3.代码 bool isHappy(int n) { ) r ...

  8. Oracle EBS 自治事务

    自治事务程序主要是自主性,那就是,独立于主要的事务.之所以独立,或者提交之后会影响其他事务处理,本质在于它本身符合编译指令的规则,也就是说它属于在编译阶段就执行的指令,而不是在运行阶段执行的. 当自治 ...

  9. 使用Amanda ZRM备份远程MySQL数据库

    本文写道最后的时候,我才发现ZRM for MySQL的一个致命问题,就我目前的理解和测试来看,它恢复数据的时候是采取覆盖的方式,举个例子,假定某台数据库服务器上有两个数据库test1,test2,你 ...

  10. 分享一个基于小米 soar 的开源 sql 分析与优化的 WEB 图形化工具

    soar-web 基于小米 soar 的开源 sql 分析与优化的 WEB 图形化工具,支持 soar 配置的添加.修改.复制,多配置切换,配置的导出.导入与导入功能. 环境需求 python3.xF ...