ll_address.setOnClickListener(this);
ll_friend.setOnClickListener(this);
ll_setting.setOnClickListener(this);
ll_set.setOnClickListener(this);
//设置ViewPager滑动监听
viewPager.setOnPageChangeListener(this);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// 底部菜单5个Linearlayout
this.ll_home = findViewById(R.id.ll_home);
this.ll_address = findViewById(R.id.ll_address);
this.ll_friend = findViewById(R.id.ll_friend);
this.ll_setting = findViewById(R.id.ll_setting);
this.ll_set= findViewById(R.id.ll_set);
// 底部菜单5个菜单标题
this.tv_home = findViewById(R.id.tv_home);
this.tv_address = findViewById(R.id.tv_address);
this.tv_friend = findViewById(R.id.tv_friend);
this.tv_setting = findViewById(R.id.tv_setting);
this.tv_set= findViewById(R.id.tv_set);
// 中间内容区域ViewPager
this.viewPager = findViewById(R.id.vp_content);
// 适配器
View page_01 = View.inflate(HomeActivity.this, R.layout.page_01, null);
View page_02 = View.inflate(HomeActivity.this, R.layout.page_02, null);
View page_03 = View.inflate(HomeActivity.this, R.layout.page_03, null);
View page_04 = View.inflate(HomeActivity.this, R.layout.page_04, null);
View page_05 = View.inflate(HomeActivity.this, R.layout.page_05, null);
views = new ArrayList<>();
views.add(page_01);
views.add(page_02);
views.add(page_03);
views.add(page_04);
views.add(page_05);
this.adapter = new ContentAdapter(views);
viewPager.setAdapter(adapter);
initEvent();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.ll_home:
tv_home.setTextColor(0xff1B940A);
viewPager.setCurrentItem(0);
break;
case R.id.ll_address:
tv_address.setTextColor(0xff1B940A);
viewPager.setCurrentItem(1);
break;
case R.id.ll_friend:
tv_friend.setTextColor(0xff1B940A);
viewPager.setCurrentItem(2);
break;
case R.id.ll_setting:
tv_setting.setTextColor(0xff1B940A);
viewPager.setCurrentItem(3);
break;
case R.id.ll_set:
tv_set.setTextColor(0xff1B940A);
viewPager.setCurrentItem(4);
default:
break;
}
}
private void restartBotton() {
// TextView置为白色
tv_home.setTextColor(0xffffffff);
tv_address.setTextColor(0xffffffff);
tv_friend.setTextColor(0xffffffff);
tv_setting.setTextColor(0xffffffff);
tv_set.setTextColor(0xffffffff);
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
restartBotton();
//当前view被选择的时候,改变底部菜单图片,文字颜色
switch (position) {
case 0:
tv_home.setTextColor(0xff1B940A);
break;
case 1:
tv_address.setTextColor(0xff1B940A);
break;
case 2:
tv_friend.setTextColor(0xff1B940A);
break;
case 3:
tv_setting.setTextColor(0xff1B940A);
break;
case 4:
tv_set.setTextColor(0xff1B940A);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
}
有报红的先不用理它。
(2)activity_home.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=“com.example.myclient.HomeActivity”>
<LinearLayout
android:layout_width=“wrap_content”
android:layout_height=“match_parent”
android:gravity=“center”
android:orientation=“vertical”>
<androidx.viewpager.widget.ViewPager
android:id=“@+id/vp_content”
android:layout_width=“match_parent”
android:background=“#ffffff”
android:layout_height=“0dp”
android:layout_weight=“1” >
</androidx.viewpager.widget.ViewPager>
(3)activity_bottom.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“35dp”
android:background=“#CBB4F5” >
<LinearLayout
android:id=“@+id/ll_home”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:orientation=“vertical” >
<ImageView
android:id=“@+id/iv_home”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
/>
<TextView
android:id=“@+id/tv_home”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“页面1”
android:textSize=“20dp”
android:textColor=“#1B940A”
android:textStyle=“bold” />
<LinearLayout
android:id=“@+id/ll_address”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:orientation=“vertical” >
<ImageView
android:id=“@+id/iv_address”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
/>
<TextView
android:id=“@+id/tv_address”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“页面2”
android:textColor=“#ffffff”
android:textSize=“20dp”
android:textStyle=“bold” />
<LinearLayout
android:id=“@+id/ll_friend”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:orientation=“vertical” >
<ImageView
android:id=“@+id/iv_friend”
android:layout_width=“53dp”
android:layout_height=“match_parent”
/>
<TextView
android:id=“@+id/tv_friend”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“页面3”
android:textSize=“20dp”
android:textColor=“#ffffff”
android:textStyle=“bold” />
<LinearLayout
android:id=“@+id/ll_setting”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:orientation=“vertical” >
<ImageView
android:id=“@+id/iv_setting”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
/>
<TextView
android:id=“@+id/tv_setting”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“页面4”
android:textColor=“#ffffff”
android:textSize=“20dp”
android:textStyle=“bold” />
<LinearLayout
android:id=“@+id/ll_set”
android:layout_width=“0dp”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:gravity=“center”
android:orientation=“vertical” >
<ImageView
android:id=“@+id/iv_set”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
/>
<TextView
android:id=“@+id/tv_set”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“页面5”
android:textColor=“#ffffff”
android:textSize=“20dp”
android:textStyle=“bold” />
(4)回到HomeActivity.java,会看到ContentAdapter报红,这里新建一个ContentAdapter.java类
public class ContentAdapter extends PagerAdapter {
private List views;
public ContentAdapter(List views) {
this.views = views;
}
@Override
public int getCount() {
return views.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
View view = views.get(position);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(views.get(position));
}
}
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
}
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-GUfBa8GI-1715896255682)]
[外链图片转存中…(img-d8aZPy24-1715896255684)]
[外链图片转存中…(img-3KtBdKou-1715896255685)]
[外链图片转存中…(img-nYT0oUvQ-1715896255686)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!