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

原文地址:https://www.cnblogs.com/tony-yang-flutter/p/3393751.html