Android实现导航菜单随着ListView联动,当导航菜单遇到顶部菜单时停止在哪里,并且listview仍能滑动
需求:现要实现一个特殊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仍能滑动的更多相关文章
- iOS_21团购_顶部菜单和弹出菜单联动
最后效果图: 各控件关系图1: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize ...
- FineUI大版本升级,外置ExtJS库、去AXD化、表格合计行、表格可编辑单元格的增删改、顶部菜单框架
这是一篇很长的文章,在开始正文之前,请允许我代表目前排名前 20 中唯一的 .Net 开源软件 FineUI 拉下选票: 投票地址: https://code.csdn.net/2013OSSurve ...
- Android ListView两种长按弹出菜单方式
转自:http://www.cnblogs.com/yejiurui/p/3247527.html package com.wyl.download_demo; import java.util.Ar ...
- 仿饿了点餐界面2个ListView联动
如图是效果图 是仿饿了的点餐界面 1.点击左侧的ListView,通过在在适配器中设置Item来改变颜色,再通过notifyDataSetInvalidated来刷新并用lv_home.setSele ...
- 仿饿了吗点餐界面两个ListView联动效果
这篇文章主要介绍了仿饿了点餐界面2个ListView联动效果的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下 如图是效果图: 是仿饿了的点餐界面 1.点击左侧的ListView,通过在在适 ...
- 不得不吐槽的Android PopupWindow的几个痛点(实现带箭头的上下文菜单遇到的坑)
说到PopupWindow,我个人感觉是又爱又恨,没有深入使用之前总觉得这个东西应该很简单,很好用,但是真正使用PopupWindow实现一些效果的时候总会遇到一些问题,但是即便是人家的api有问题, ...
- 解析ListView联动的实现--仿饿了么点餐界面
一.博客的由来 大神王丰蛋哥 之前一篇博客仿饿了点餐界面2个ListView联动(http://www.cnblogs.com/wangfengdange/p/5886064.html) 主要实现了2 ...
- Android仅2步实现 滚粗 汉堡导航栏效果~ 全新底部导航交互(滑动隐藏)
本文同步自wing的地方酒馆 布吉岛大家有木有看这一篇文章,再见,汉堡菜单,我们有了新的 Android 交互设计方案 本库下载地址:https://github.com/githubwing/Bye ...
- 仿美团外卖,饿了吗 两个ListView联动,左边点击切换右边,右边滑动切换左边
先上效果图: 实现思路: 1.先说右边标题: 首先,右边的数据源集合中的Javabean中含有三个属性name,type,title,而每个条目中会默认含有一个标题. 如果这是第一个条目,就让标题显示 ...
随机推荐
- 你不知道的JavasScript上篇·第三章·对象
1.Object.defineProperty()&&getter.setter Object.defineProperty(目标对象,属性名(string型),{ get:funct ...
- Python 并发编程(管道,事件,信号量,进程池)
管道 Conn1,conn2 = Pipe() Conn1.recv() Conn1.send() 数据接收一次就没有了 from multiprocessing import Process,Pip ...
- 第一章 深入.NET框架
一. .NET的过人之处 1..NET框架提高了软件的可重复行 ,可扩展性,可维护行和灵活性. 2.对web应用的强大支撑. 3.对Web Service(Web服务)的支持. 4.实现SOA,支持云 ...
- Java 接口和抽象类区别(写的很好,转了)
原文:http://blog.csdn.net/sunboard/article/details/3831823 1.概述 一个软件设计的好坏,我想很大程度上取决于它的整体架构,而这个整体架构其实就是 ...
- js判断数组是否包含某个字符串变量
最近碰到一个这样的现象,后台返回的数据中,数组里面有一些有变量值,有一些没有变量值. 举个例子,比如后台返回的例子是这样的: var arr=[ { "status":" ...
- openCV 视频分解及合成
1. 视频分解 import cv2 # ************************** # 分解视频 cap=cv2.VideoCapture('1.mp4')#获取一个视频cap isOpe ...
- Android 限定符
Android中一些常见的限定符可以参考下表. 使用最小宽度限定符 在上一小节中我们使用large限定符成功解决了单页双页的判断问题,不过很快又有一个新的问题出现了,large到底是指多大呢?有的时候 ...
- react+spring 记录跨域问题的解决方法
react 跨域访问后台,默认是有跨域问题,并且火弧和谷歌浏览器,对跨域问题展示还不一样. 谷歌浏览器如下图: 此处状态是200,然而在Response却没有任何信息,如下图 然而火弧浏览器,对该问题 ...
- SerialPort类的用法与示例
转:https://www.cnblogs.com/hwBeta/p/6926363.html Microsoft .Net框架SerialPort类的用法与示例 从Microsoft .Net 2. ...
- poj_3253 Fence Repair
Fence Repair Description Farmer John wants to repair a small length of the fence around the pasture. ...