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,而每个条目中会默认含有一个标题. 如果这是第一个条目,就让标题显示 ...
随机推荐
- 'QuerySet' object has no attribute '_meta'
'QuerySet' object has no attribute '_meta' 对象列表没有'_meta'属性 单独的对象才有, 忘记加first了 edit_obj = models.Role ...
- 如何启动一个Vue2.x项目
1. cd到工作目录2. npm init -y3. 先查看有没有安装全局的vue-cli,:vue-V,没有的话安装一下:npm install vue-cli4. 创建项目: vue init w ...
- python 类之间的关系
类与类之间的关系 在我们的世界中事物和事物之间总会有一些联系. 在面向对象中. 类和类之间也可以产生相关的关系 1. 依赖关系 执行某个动作的时候. 需要xxx来帮助你完成这个操作. 此时的关系是最轻 ...
- Java内部类的介绍
在Java的面向对象编程中,由于Java并没有类似C++的多重继承,所以采用了内部类这样的方式,现在介绍几种内部类的常见情况. 公开内部类 即由public关键词修饰的内部类,内部类作为外部类的一个成 ...
- python实现分页插件
class Pages: def __init__(self, current_page, data_count, per_page_count=10, pager_num=7):#pager_num ...
- FineReport单行与数据库交互的方法
1. 问题描述 我们在做一张报表填报的时候经常会遇到需要在一行进行添加动作,将该行数据直接与数据库交互,执行存储过程过程.我们可以通过每一行增加帆软“插入”按钮实现插入动作,并且在控件事件中增加和 ...
- profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 2
profile,bashrc,.bash_profile,.bash_login,.profile,.bashrc,.bash_logout浅析 Part 2 by:授客 QQ:103355312 ...
- chrome浏览器使用chrome://inspect调试app 网页,打开空白的问题
使用chrome浏览器,输入chrome://inspect可以调试android app里面的网页,如果inspect的时候,是空白, 问题截图: 那就在C:\Windows\System32\dr ...
- CSS 小结笔记之BFC
BFC 即为Block formatting context 的缩写,BFC 主要用来将一个盒子设置为一个隔离的容器,不管盒子内部的元素具有什么属性,都不会影响到盒子的外面. 1.哪些元素能产生BFC ...
- Python tuple
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表 语法 names = (a,b,c) 它只有2个方法,一个是count,一个是index. 当然也有可变元祖: 可 ...