• cathome 猫家 开发日记-底部导航


    概述:一个linearlayout,根据数组动态插入textview. 并给textview添加click事件,同时记录点击的textview在数据中,所处的索引位置。

    1.item.setSelected(false); 可以实现那个被点击了
    2.使用委托把点击事件延迟到外部定义。

    底部导航封装为自定义控件。

    算是比较方便了。

    package com.utils.widgets;
    
    import android.content.Context;
    import android.content.res.ColorStateList;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.view.Gravity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class LSUIBottomNavigation extends LinearLayout implements View.OnClickListener
    {
        List<MenuItem> mMenus=new ArrayList<>();
        List<TextView> mTextViews=new ArrayList<>();
        int mSelectIndex=0;
        IBottomNavigation handle;
        int mNormalID;
        int mSelectColorid;
    
        public LSUIBottomNavigation(Context context)
        {
            super(context);
        }
    
        public LSUIBottomNavigation(Context context, AttributeSet attrs)
        {
            super(context, attrs);
            this.setOrientation(HORIZONTAL);
        }
    
        public void SetupControls(List<MenuItem> data,IBottomNavigation handlea,int normalid,int selectid,int selectIndex)
        {
            if(data==null)
            {
                return;
            }
            handle=handlea;
            mMenus=data;
            mNormalID=normalid;
            mSelectColorid=selectid;
            mSelectIndex=selectIndex;
            for(int i=0;i<mMenus.size();i++)
            {
                TextView tempTV=new TextView(this.getContext());
                tempTV.setText(mMenus.get(i).RID);
                tempTV.setTag(i);
                tempTV.setOnClickListener(this);
    
                Drawable drawable = mMenus.get(i).topPic;
                drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
                tempTV.setCompoundDrawables(null, drawable, null, null);
    
                SetStyle(tempTV);
                mTextViews.add(tempTV);
                this.addView(tempTV);
            }
            FlushMenu();
        }
    
        private void SetStyle(TextView tempTV)
        {
            LinearLayout.LayoutParams lyp=new LinearLayout.LayoutParams(100, ViewGroup.LayoutParams.MATCH_PARENT);
            lyp.weight=1;
            tempTV.setLayoutParams(lyp);
            tempTV.setGravity(Gravity.CENTER);
            tempTV.setTextColor(createColorStateList(getResources().getColor(mNormalID), getResources().getColor(mSelectColorid)));
        }
    
        /** 对TextView设置不同状态时其文字颜色。 */
        private ColorStateList createColorStateList(int normal, int pressed) {
            int[] colors = new int[] { pressed, normal };
            int[][] states = new int[2][];
            states[0] = new int[] {android.R.attr.state_selected };
            states[1] = new int[] {};
            ColorStateList colorList = new ColorStateList(states, colors);
            return colorList;
        }
    
        @Override
        public void onClick(View v)
        {
            if(v instanceof TextView)
            {
                TextView tv=(TextView)v;
                mSelectIndex=(int)tv.getTag();
                if(handle!=null)
                {
                    handle.OnClickMenu(v);
                }
            }
            FlushMenu();
        }
    
        private void FlushMenu()
        {
            for(TextView item : mTextViews)
            {
                item.setSelected(false);
            }
            if(mTextViews.size()>mSelectIndex)
            {
                mTextViews.get(mSelectIndex).setSelected(true);
            }
        }
    
        public static class MenuItem
        {
            public int RID;
            //public String title;
            public Drawable topPic;
        }
    
        public interface IBottomNavigation
        {
            public void OnClickMenu(View v);
        }
    }

    使用:

    1.view文件

    <com.utils.widgets.LSUIBottomNavigation android:orientation="horizontal" android:id="@+id/bottomMenu" android:layout_width="match_parent" android:layout_height="56dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:paddingLeft="1dp" android:paddingRight="1dp"></com.utils.widgets.LSUIBottomNavigation>

    2.activity:

    List<LSUIBottomNavigation.MenuItem> aa=new ArrayList<>();

     mMenu.SetupControls(aa,this,R.color.color_grey,R.color.color_green,0);

    3.可以在单个页面使用多个控件,互相不影响。也可以在不同的页面使用一个,好像是页面没 变一样。因为

    使用了内部变量 int mSelectIndex=0;,而且初始化控件提供了函数参数来赋值。最开始是打算用静态变量的。
    public void SetupControls(List<MenuItem> data,IBottomNavigation handlea,int normalid,int selectid,int selectIndex)

     一般采用fragment来展示不同页面。这里采用了继承的activity来展示不同页面。

    基类:

    package com.android.linson.catshome.Control;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.util.Log;
    import android.view.View;
    import android.widget.RelativeLayout;
    import com.android.linson.catshome.Control.Category.CategoryIndex;
    import com.android.linson.catshome.Control.wike.WikeIndex;
    import com.android.linson.catshome.R;
    import com.utils.widgets.LSUIBottomNavigation;
    
    import java.util.ArrayList;
    import java.util.List;
    
    //1.实现整体布局,留出主页面给派生类填充 。
    //2.实现底部菜单功能 。
    public abstract class MasterPage extends Activity implements LSUIBottomNavigation.IBottomNavigation
    {
        //control
        protected RelativeLayout mContent;
        protected com.utils.widgets.LSUIBottomNavigation mMenu;
    
        //data
        protected String TAG="DEBUG";
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.masterpage);
            FindControls();
            SetupMenu();
            mContent.addView(GetContent());
        }
    
        protected void SetupMenu()
        {
            LSUIBottomNavigation.MenuItem temp1=new LSUIBottomNavigation.MenuItem();
            temp1.RID=R.string.bottomMenu1;
            temp1.topPic=getResources().getDrawable(R.drawable.category );
            LSUIBottomNavigation.MenuItem temp2=new LSUIBottomNavigation.MenuItem();
            temp2.RID=R.string.bottomMenu2;
            temp2.topPic=getResources().getDrawable(R.drawable.book );
            LSUIBottomNavigation.MenuItem temp3=new LSUIBottomNavigation.MenuItem();
            temp3.RID=R.string.bottomMenu3;
            temp3.topPic=getResources().getDrawable(R.drawable.book );
            LSUIBottomNavigation.MenuItem temp4=new LSUIBottomNavigation.MenuItem();
            temp4.RID=R.string.bottomMenu4;
            temp4.topPic=getResources().getDrawable(R.drawable.book );
            LSUIBottomNavigation.MenuItem temp5=new LSUIBottomNavigation.MenuItem();
            temp5.RID=R.string.bottomMenu5;
            temp5.topPic=getResources().getDrawable(R.drawable.book );
    
            List<LSUIBottomNavigation.MenuItem> aa=new ArrayList<>();
            aa.add(temp1);
            aa.add(temp2);
            aa.add(temp3);
            aa.add(temp4);
            aa.add(temp5);
    
            mMenu.SetupControls(aa,this,R.color.color_grey,R.color.color_green,GetSelectIndex());
        }
    
        public abstract View GetContent();
        public abstract int GetSelectIndex();
    
        private void FindControls()
        {
            mContent=findViewById(R.id.RLayoutContent);
            mMenu=findViewById(R.id.bottomMenu);
        }
    
        @Override
        public void OnClickMenu(View v)
        {
            Log.i(TAG, "OnClickMenu: "+v.getTag());
            switch ((int)v.getTag())
            {
                case 0:
                {
                    Intent page=new Intent(this, CategoryIndex.class);
                    page.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(page);
                    break;
                }
                case 1:
                {
                    Intent page=new Intent(this, WikeIndex.class);
                    page.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(page);
                    break;
                }
            }
        }
    }

    有一个立即的好处。 导航的弹出。到基类页面写就好了。

    可以复用一些代码。

    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

  • 相关阅读:
    win7跨网段加域提示"找不到网络路径",解决后又提示"将该计算机的主域DNS更改为“”失败,名称仍然为xx.xx, 错误为“指定的服务器无法运行请求的操作”!
    XP原版系统激活
    使用SQLyog远程连接mysql,错误1130 解决方法
    激活Windows Server 2008 R2 Enterprise 方法
    Non-UTF-8 code starting with 'xff'解决,记录
    Linux 中,基础命令 command not found...问题
    winxp_sp3未激活的登录问题
    linux安装tomcat
    Notepad++ 配置python
    python idle 清屏问题的解决
  • 原文地址:https://www.cnblogs.com/lsfv/p/9822562.html
Copyright © 2020-2023  润新知