android滑动菜单图标,Android实现简单底部导航栏 Android仿微信滑动切换效果

Android仿微信滑动切换最终实现效果:

大体思路:

1. 主要使用两个自定义View配合实现; 底部图标加文字为一个自定义view,底部导航栏为一个载体,根据需要来添加底部图标;

2. 底部导航栏的设置方法类似于TabLayout的关联,View需要创建关联方法,用来关联VIewPager;

3. 通过关联方法获取ViewPager实例后,根据ViewPager页面数创建底部导航栏的图标按钮;

代码实现:

1. 新建第一个自定义View, 图标 + 文字 的底部按钮;

/**

* 自定义控件,该控件为底部导航栏中的图标

* Created by MrZheng on 2017/8/2.

*/

public class TabView extends LinearLayout {

BotBean mBean;

private TextView title;

private ImageView iconImage;

/**

* 引用此控件,只能通过new 方法;接收一个TabView

* @param context

*/

public TabView(Context context, BotBean bean) {

super(context);

this.mBean = bean;

initView();

}

/**

* 初始化布局

*/

public void initView() {

setOrientation(VERTICAL);

setGravity(Gravity.CENTER);

//添加小图标

iconImage = new ImageView(getContext());

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT

, ViewGroup.LayoutParams.WRAP_CONTENT);

iconImage.setLayoutParams(layoutParams);

iconImage.setImageResource(mBean.getUncheckedId());

Drawable drawable = getContext().getResources().getDrawable(mBean.getUncheckedId());

Drawable wrapDrawable = DrawableCompat.wrap(drawable);

DrawableCompat.setTintList(wrapDrawable, ColorStateList.valueOf(Color.BLACK));

iconImage.setImageDrawable(wrapDrawable);

addView(iconImage);

//标题

title = new TextView(getContext());

LinearLayout.LayoutParams titleParams = new LayoutParams(LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT);

title.setLayoutParams(titleParams);

title.setText(mBean.getContent());

addView(title);

}

//判断选择状态,改变图标

//供外部调用

public void setSelected(boolean isSelected) {

if (mBean == null) {

return;

}

if (isSelected) {

if (iconImage != null) {

//使用颜色过滤器,改变选中时的颜色

Drawable drawable = getContext().getResources().getDrawable(mBean.getUncheckedId());

Drawable wrapDrawable = DrawableCompat.wrap(drawable);

DrawableCompat.setTintList(wrapDrawable, ColorStateList.valueOf(Color.GREEN));

iconImage.setImageDrawable(wrapDrawable);

title.setTextColor(Color.GREEN);

}

} else {

if (title != null) {

// iconImage.setImageResource(mBean.getUncheckedId());

Drawable drawable = getContext().getResources().getDrawable(mBean.getUncheckedId());

Drawable wrapDrawable = DrawableCompat.wrap(drawable);

DrawableCompat.setTintList(wrapDrawable, ColorStateList.valueOf(Color.BLACK));

iconImage.setImageDrawable(wrapDrawable);

title.setTextColor(Color.GRAY);

}

}

}

}

2. 创建第二个自定义View,该View为底部导航栏载体,根据 关联的ViewPager页面 个数创建 底部导航栏图标;

/**

* 该控件为底部导航栏图标载体

* Created by MrZheng on 2017/8/2.

*/

public class bottomView extends LinearLayout {

private ViewPager vp;

BottomPageChangeListener mBottomPageChangeListener;

public bottomView(Context context) {

super(context);

}

public bottomView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

}

public bottomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

/**

* 同TabLayout用法相似,需要与ViewPager进行绑定

*/

public void setViewPager(ViewPager viewPager, ArrayList botBeen,BottomPageChangeListener bottomPageChangeListener) {

if (viewPager == null) {

return;

}

vp = viewPager;

mBottomPageChangeListener = bottomPageChangeListener;

initTabView(botBeen);

//设置ViewPager的点击事件

vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){

@Override

public void onPageSelected(int position) {

for (int i = 0; i < getChildCount(); i++) {

getChildAt(i).setSelected((position == i ? true : false));

}

if (mBottomPageChangeListener != null) {

mBottomPageChangeListener.onPageChangeListener(position);

}

}

});

}

/**

* 初始化底部导航栏,ViewPager有多少页,就创建多少个图标

*/

public void initTabView(ArrayList botBeen) {

setGravity(HORIZONTAL);

for (int i = 0; i < botBeen.size(); i++) {

BotBean bean = botBeen.get(i);

TabView tabView = new TabView(getContext(), bean);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT

, ViewGroup.LayoutParams.WRAP_CONTENT);

params.weight = 1;

params.gravity = Gravity.CENTER;

tabView.setLayoutParams(params);

//为每个view设置点击事件,点击跳转过去

final int finalI = i;

tabView.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View view) {

vp.setCurrentItem(finalI);

}

});

//设置一开始选中状态

if (i == 0) {

tabView.setSelected(true);

//由于初始化时,onPageSelected()选中方法并没有的到执行,所以主动去调用回调方法

if (mBottomPageChangeListener != null) {

mBottomPageChangeListener.onPageChangeListener(i);

}

}

addView(tabView);

}

}

/**

* 提供接口回调方法,每次滑动都通知外界

*/

public interface BottomPageChangeListener{

void onPageChangeListener(int position);

}

}

3. 添加 图标自定义类, 该类封装着底部导航栏中每一个选项的的图标和文字,将该类型对象添加到集合中,用于给底部导航栏设置图标;

/**

* 底部导航栏的封装类,该类对象用于在底部导航栏添加对应图标和文字

* Created by MrZheng on 2017/8/2.

*/

public class BotBean {

String content;//图标名字

int uncheckedId;//未选中时的图标

public BotBean(String content, int uncheckedId) {

this.content = content;

this.uncheckedId = uncheckedId;

}

public String getContent() {

return content;

}

public void setContent(String content) {

this.content = content;

}

public int getUncheckedId() {

return uncheckedId;

}

public void setUncheckedId(int uncheckedId) {

this.uncheckedId = uncheckedId;

}

}

自定义View实现完成,在Fragment或Activity中使用该View:

1. 在布局文件中添加:

android:id="@+id/bottom"

android:layout_width="match_parent"

android:layout_height="60dp">

2. 在活动或碎片中添加:

public class MainActivity extends AppCompatActivity {

ArrayList mFragments;

ArrayList mItemIcon;//存放底部图标和文字

private TextView tv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mFragments = new ArrayList<>();

mItemIcon = new ArrayList<>();

mFragments.add(new TextFragment());

mFragments.add(new TextFragment());

mFragments.add(new TextFragment());

mFragments.add(new TextFragment());

mItemIcon.add(new BotBean("首页", R.mipmap.ic_home2));

mItemIcon.add(new BotBean("通讯录", R.mipmap.ic_study2));

mItemIcon.add(new BotBean("发现", R.mipmap.ic_found2));

mItemIcon.add(new BotBean("我的", R.mipmap.ic_me2));

ViewPager vp = (ViewPager) findViewById(R.id.vp);

vp.setAdapter(new FAdapter(getSupportFragmentManager()));

tv = (TextView) findViewById(R.id.tv);

bottomView bottom = (bottomView) findViewById(R.id.bottom);

bottom.setViewPager(vp, mItemIcon, new bottomView.BottomPageChangeListener() {

@Override

public void onPageChangeListener(int position) {

//滑动后的回调

tv.setText(mItemIcon.get(position).getContent());

}

});

}

/**

* 适配器

*/

class FAdapter extends FragmentPagerAdapter {

public FAdapter(FragmentManager fm) {

super(fm);

}

@Override

public Fragment getItem(int position) {

return mFragments.get(position);

}

@Override

public int getCount() {

return mFragments.size();

}

}

}

总结:该代码耦合度较高,有些代码可能不太合理;欢迎大牛们给出合理建议;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441595.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 2149】Public Sale (巴什博奕)

题干&#xff1a; 虽然不想&#xff0c;但是现实总归是现实&#xff0c;Lele始终没有逃过退学的命运&#xff0c;因为他没有拿到奖学金。现在等待他的&#xff0c;就是像FarmJohn一样的农田生涯。 要种田得有田才行&#xff0c;Lele听说街上正在举行一场别开生面的拍卖会&…

Android万能遥控菜单选择添加,Android万能遥控器小应用

在很久很久以前&#xff0c;手机是有红外功能的&#xff0c;后来随着蓝牙技术的成熟&#xff0c;红外逐渐被蓝牙取代&#xff0c;不再是标配了。红外本身还是有些优点&#xff0c;比如操作简便&#xff0c;成本低。要想在手机上添加红外功能&#xff0c;就要外接一个转换模块。…

android studio 库工程,Android Studio 添加已有工程方法

准备工作&#xff1a;修改 excluded-paths和android.iml&#xff0c;修改内容详见下图。(目的&#xff1a;过滤和优先在sourcefolder查找&#xff0c;若没有再到JAR包中查找)BorqsUI/LINUX/android/development/tools/idegen$ mm编译出来&#xff1a;[100% 3/3] Install: out/h…

【HDU - 3951】Coin Game (博弈,猜规律,对称博弈)

题干&#xff1a; After hh has learned how to play Nim game, he begins to try another coin game which seems much easier. The game goes like this: Two players start the game with a circle of n coins. They take coins from the circle in turn and every time…

android textview 白色,android – AutoCompleteTextview默认情况下,颜色设置为白色

我在我的Android应用程序中使用了一个AutoCompleteTextView&#xff0c;它正常工作。我唯一遇到的问题是&#xff0c;默认情况下&#xff0c;建议的颜色为白色&#xff0c;我无法看到任何建议。所以当我开始打字时&#xff0c;列表会以白色条目(不可见)扩展&#xff0c;但是当我…

【HDU - 1527】【POJ - 1067】取石子游戏 (威佐夫博弈)

题干&#xff1a; 有两堆石子&#xff0c;数量任意&#xff0c;可以不同。游戏开始由两个人轮流取石子。游戏规定&#xff0c;每次有两种不同的取法&#xff0c;一是可以在任意的一堆中取走任意多的石子&#xff1b;二是可以在两堆中同时取走相同数量的石子。最后把石子全部取…

android okgo参数,Android OkGo基本操作

或许不是很全面&#xff0c;也都是从网上粘的&#xff0c;自己记下以后使用方便点。如有不对的地方 欢迎指教。首先添加依赖&#xff1a;implementation com.lzy.net:okgo:3.0.4设置初始化和全局配置&#xff1a;public class App extends Application {Overridepublic void on…

【CodeForces - 334B】Eight Point Sets(水题模拟,有坑)

题干&#xff1a; Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines and three distinct integer horizontal straight lines, ex…

一加6怎么刷android p6,一加6秒速跟进安卓P 教你尝鲜速成开发者

今年5月份&#xff0c;谷歌在I/O开发者大会上发布了全新的Android P操作系统&#xff0c;而在Android P系统发布后的没多久&#xff0c;一加手机官方就公开承诺&#xff0c;年度旗舰一加手机6将会成为首批次升级谷歌Android P系统的机型。而现在&#xff0c;一加已经开始兑现此…

*【CodeForces - 1047A】Little C Loves 3 I (水题,构造,三元组问题)

题干&#xff1a; Little C loves number 3 very much. He loves all things about it. Now he has a positive integer nn. He wants to split nn into 33 positive integers a,b,ca,b,c, such that abcnabcn and none of the 33 integers is a multiple of 33. Help him to…

html语言音乐添加路径,HTML5简单实现添加背景音乐的几种方法

这里推荐两种方法&#xff0c;就是两个标签 或者常用 css布局 隐藏播放器 做网站比较实用&#xff01;html5添加音乐说明&#xff1a;1、src毫无疑问写路径.2、使用hidden"true"表示隐藏音乐播放按钮&#xff0c;相反使用hidden"false"表示开启音乐播放按钮…

【CodeForces - 1047B 】Cover Points (数学,构造,思维)

题干&#xff1a; There are nn points on the plane, (x1,y1),(x2,y2),…,(xn,yn)(x1,y1),(x2,y2),…,(xn,yn). You need to place an isosceles triangle with two sides on the coordinate axis to cover all points (a point is covered if it lies inside the triangle …

html字段隐藏,如何刮取动态隐藏的HTML字段(UuViewState)值?

我在代码中完全实现了请求&#xff0c;但在FormRequest中意识到VIEWSTATE和EVENTVALIDATION是动态的&#xff0c;它们随每个请求而变化。我想做的是刮去它们以便在下一个请求中提供它们。在这两个字段都在HTML代码中提供&#xff0c;但隐藏了&lt&#xff1b;the __VIEWSTAT…

【CodeForces - 1051A】Vasya And Password (构造,水题)

题干&#xff1a; Vasya came up with a password to register for EatForces — a string ss. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its u…

html5引擎笔试题,最新!HTML5经典面试题型(附答案)

HTML已更新至HTML5&#xff0c;那么HTML5的测试题您也应该知道&#xff0c;这篇文章可以作为您的参考。1.doctype有什么作用呢&#xff1f;如何区分其混合模式和标准模式&#xff1f;所有这些都意味着什么&#xff1f;Doctype的作用是告诉浏览器使用HTML规范的哪个版本来渲染文…

【CodeForces - 1051B】Relatively Prime Pairs (构造,思维,素数,水题)

题干&#xff1a; You are given a set of all integers from ll to rr inclusive, l<rl<r, (r−l1)≤3⋅105(r−l1)≤3⋅105and (r−l)(r−l) is always odd. You want to split these numbers into exactly r−l12r−l12 pairs in such a way that for each pair (i,…

html 弹出加载页面,magnific popup:将整个html页面加载到弹出窗口中

我想用弹出的插件在弹出窗口中加载一个完整的html页面。如果我尝试&#xff1a;Edit images$(#edit-images-btn).magnificPopup({type: ajax});它产生了这个&#xff1a;这在图形上非常符合我的要求&#xff0c;但问题是的内容直接插入到dom中&#xff0c;而不是放在保护性的if…

【CodeForces - 1042A】Benches (优先队列,思维模拟,maxmin问题)

题干&#xff1a; There are nn benches in the Berland Central park. It is known that aiai people are currently sitting on the ii-th bench. Another mm people are coming to the park and each of them is going to have a seat on some bench out of nn available. …

网页html 图片横向摆放,css实现多张图片横向居中显示的方法

先讲一下实现的步骤&#xff1a;最终效果2. 代码实现HTML部分分类小贴士CSS部分.main{width:100%;margin-top:40px;}.main .tag{margin:0 auto;width:200px;font-size:18px;border-bottom:1px solid #878787;text-align:center;margin-bottom:20px;}.main .images{margin:0 aut…

【CodeForces - 1042B】Vitamins(去重方法,二进制或stlmap,水题)

题干&#xff1a; Berland shop sells nn kinds of juices. Each juice has its price cici. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin "A", vitamin "B" and vitamin "C". Each juice ca…