热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android仿百度壁纸客户端之搭建主框架(一)

这篇文章主要介绍了Android仿百度壁纸客户端,搭建主框架,自定义Tab+ViewPager+Fragment,具有一定的实用性,感兴趣的小伙伴们可以参考一下

这是个不错的教程,自己学完了之后就拿出来分享了,本来想一个帖子写完,但是发现这样对自己写博客的效率有点出入,为了让大家看的舒服点,所以分开来写,我们先开看下百度壁纸的客户端是什么样子的

这里写图片描述

我们先来写个主页的框架,我们新建一个项目——BaiDuWallPaper 

写个Item
layout_tab_item

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 

 

 


 



然后我们再写个布局 

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 

 

 

 

 




这样我们就可以自定义组合控件了
 MyBottomLayout

package com.lgl.baiduwallpaper.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lgl.baiduwallpaper.R;

/**
 * 底部布局
 * Created by lgl on 16/3/31.
 */
public class MyBottomLayout extends LinearLayout {

 //跟布局是RelativeLayout
 private RelativeLayout homeLayout, selectLayout, searchLayout, locationLayout, settingLayout;
 //布局加载
 private LayoutInflater inflater;

 //构造方法
 public MyBottomLayout(Context context, AttributeSet attrs) {
 super(context, attrs);

 initView();
 }

 /**
 * 初始化
 */
 private void initView() {
 inflater = LayoutInflater.from(getContext());
 View view = inflater.inflate(R.layout.layout_bottom, this);
 findView(view);
 initData();
 setonClick();
 }

 /**
 * 初始化数据
 */
 private void initData() {
 homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_home_down);
 TextView tvHome = (TextView) homeLayout.findViewById(R.id.tabText);
 tvHome.setText("首页");
 tvHome.setTextColor(Color.BLUE);

 selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_search);
 TextView tvSelect = (TextView) selectLayout.findViewById(R.id.tabText);
 tvSelect.setText("精选");
 tvSelect.setTextColor(Color.WHITE);

 searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_find);
 TextView tvSearch = (TextView) searchLayout.findViewById(R.id.tabText);
 tvSearch.setText("搜索");
 tvSearch.setTextColor(Color.WHITE);

 locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_manage);
 TextView tvLoaction = (TextView) locationLayout.findViewById(R.id.tabText);
 tvLoaction.setText("本地");
 tvLoaction.setTextColor(Color.WHITE);

 settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_more);
 TextView tvSetting = (TextView) settingLayout.findViewById(R.id.tabText);
 tvSetting.setText("设置");
 tvSetting.setTextColor(Color.WHITE);
 }

 /**
 * 找到控件的方法
 *
 * @param view
 */
 private void findView(View view) {
 homeLayout = (RelativeLayout) view.findViewById(R.id.homeLayout);
 selectLayout = (RelativeLayout) view.findViewById(R.id.selectLayout);
 searchLayout = (RelativeLayout) view.findViewById(R.id.searchLayout);
 locatiOnLayout= (RelativeLayout) view.findViewById(R.id.locationLayout);
 settingLayout = (RelativeLayout) view.findViewById(R.id.settingLayout);
 }

 /**
 * 控件的点击事件
 */
 private void setonClick() {
 homeLayout.setOnClickListener(new lister());
 selectLayout.setOnClickListener(new lister());
 searchLayout.setOnClickListener(new lister());
 locationLayout.setOnClickListener(new lister());
 settingLayout.setOnClickListener(new lister());
 }

 /**
 * 点击接口
 */
 private class lister implements OnClickListener {

 /**
 * 点击后改变点击状态
 * 切换页面
 *
 * @param v
 */
 @Override
 public void onClick(View v) {
 switch (v.getId()) {
 case R.id.homeLayout:
  initPix(0);
  break;
 case R.id.selectLayout:
  initPix(1);
  break;
 case R.id.searchLayout:
  initPix(2);
  break;
 case R.id.locationLayout:
  initPix(3);
  break;
 case R.id.settingLayout:
  initPix(4);
  break;
 }
 iCallbackListener.clic(v.getId());
 }
 }

 /**
 * 切换卡的位置
 */
 public void initPix(int i) {
 switch (i) {
 case 0:
 homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_home_down);
 TextView tvHome0 = (TextView) homeLayout.findViewById(R.id.tabText);
 tvHome0.setTextColor(Color.BLUE);

 selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_search);
 TextView tvSelect0 = (TextView) selectLayout.findViewById(R.id.tabText);
 tvSelect0.setTextColor(Color.WHITE);

 searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_find);
 TextView tvSearch0 = (TextView) searchLayout.findViewById(R.id.tabText);
 tvSearch0.setTextColor(Color.WHITE);

 locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_manage);
 TextView tvLocation0 = (TextView) locationLayout.findViewById(R.id.tabText);
 tvLocation0.setTextColor(Color.WHITE);

 settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_more);
 TextView tvSetting0 = (TextView) settingLayout.findViewById(R.id.tabText);
 tvSetting0.setTextColor(Color.WHITE);

 break;
 case 1:

 homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_home);
 TextView tvHome1 = (TextView) homeLayout.findViewById(R.id.tabText);
 tvHome1.setTextColor(Color.WHITE);

 selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_search_down);
 TextView tvSelect1 = (TextView) selectLayout.findViewById(R.id.tabText);
 tvSelect1.setTextColor(Color.BLUE);

 searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_find);
 TextView tvSearch1 = (TextView) searchLayout.findViewById(R.id.tabText);
 tvSearch1.setTextColor(Color.WHITE);

 locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_manage);
 TextView tvLocation1 = (TextView) locationLayout.findViewById(R.id.tabText);
 tvLocation1.setTextColor(Color.WHITE);

 settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_more);
 TextView tvSetting1 = (TextView) settingLayout.findViewById(R.id.tabText);
 tvSetting1.setTextColor(Color.WHITE);

 break;
 case 2:

 homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_home);
 TextView tvHome2 = (TextView) homeLayout.findViewById(R.id.tabText);
 tvHome2.setTextColor(Color.WHITE);

 selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_search);
 TextView tvSelect2 = (TextView) selectLayout.findViewById(R.id.tabText);
 tvSelect2.setTextColor(Color.WHITE);

 searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_find_down);
 TextView tvSearch2 = (TextView) searchLayout.findViewById(R.id.tabText);
 tvSearch2.setTextColor(Color.BLUE);

 locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_manage);
 TextView tvLocation2 = (TextView) locationLayout.findViewById(R.id.tabText);
 tvLocation2.setTextColor(Color.WHITE);

 settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_more);
 TextView tvSetting2 = (TextView) settingLayout.findViewById(R.id.tabText);
 tvSetting2.setTextColor(Color.WHITE);


 break;
 case 3:

 homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_home);
 TextView tvHome3 = (TextView) homeLayout.findViewById(R.id.tabText);
 tvHome3.setTextColor(Color.WHITE);

 selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_search);
 TextView tvSelect3 = (TextView) selectLayout.findViewById(R.id.tabText);
 tvSelect3.setTextColor(Color.WHITE);

 searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_find);
 TextView tvSearch3 = (TextView) searchLayout.findViewById(R.id.tabText);
 tvSearch3.setTextColor(Color.WHITE);

 locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_manage_down);
 TextView tvLocation3 = (TextView) locationLayout.findViewById(R.id.tabText);
 tvLocation3.setTextColor(Color.BLUE);

 settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_more);
 TextView tvSetting3 = (TextView) settingLayout.findViewById(R.id.tabText);
 tvSetting3.setTextColor(Color.WHITE);

 break;
 case 4:

 homeLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_home);
 TextView tvHome4 = (TextView) homeLayout.findViewById(R.id.tabText);
 tvHome4.setTextColor(Color.WHITE);

 selectLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_search);
 TextView tvSelect4 = (TextView) selectLayout.findViewById(R.id.tabText);
 tvSelect4.setTextColor(Color.WHITE);

 searchLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_find);
 TextView tvSearch4 = (TextView) searchLayout.findViewById(R.id.tabText);
 tvSearch4.setTextColor(Color.WHITE);

 locationLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_manage);
 TextView tvLocation4 = (TextView) locationLayout.findViewById(R.id.tabText);
 tvLocation4.setTextColor(Color.WHITE);

 settingLayout.findViewById(R.id.tabImg).setBackgroundResource(R.mipmap.image_tabbar_button_more_down);
 TextView tvSetting4 = (TextView) settingLayout.findViewById(R.id.tabText);
 tvSetting4.setTextColor(Color.BLUE);

 break;
 }
 }
}

我们运行一下

这里写图片描述

接下来我们让他可以切换选项卡,我们定义一个接口

 /**
 * 切换页面的接口
 */
 public interface ICallbackListener {
 public void clic(int id);
 }

 ICallbackListener iCallbackListener = null;

 public void setonCallbackListener(ICallbackListener iCallbackListener) {
 this.iCallbackListener = iCallbackListener;
 }

接着初始化数据

 /**
 * 设置默认的第一页数据
 */
 private void initPagerContent(android.app.Fragment fragment) {
 FragmentManager manager = getFragmentManager();
 android.app.FragmentTransaction ft = manager.beginTransaction();
 ft.replace(R.id.myContent,fragment);
 ft.commit();
 }

然后我们引用的时候就可以直接new了

 /**
 * 切换接口
 */
 private class MyCallbackListener implements MyBottomLayout.ICallbackListener {

 @Override
 public void clic(int id) {
 switch (id) {
 case R.id.homeLayout:
  initPagerContent(new HomeFragment());

  break;
 case R.id.selectLayout:
  initPagerContent(new SelectFragment());

  break;
 case R.id.searchLayout:
  initPagerContent(new SearchFragment());

  break;
 case R.id.locationLayout:
  initPagerContent(new LoactionFragment());

  break;
 case R.id.settingLayout:
  initPagerContent(new SettingFragment());

  break;

 }
 }
 }

 我们在运行一下 

这里写图片描述

但是有一点我们要知道,我们还要实现滑动,这样的话,我们就要使用viewpager了
 layout_main.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>



 

 


具体的,我就直接把MainActivity的代码贴上吧

 package com.lgl.baiduwallpaper;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.lgl.baiduwallpaper.fragment.HomeFragment;
import com.lgl.baiduwallpaper.fragment.LoactionFragment;
import com.lgl.baiduwallpaper.fragment.SearchFragment;
import com.lgl.baiduwallpaper.fragment.SelectFragment;
import com.lgl.baiduwallpaper.fragment.SettingFragment;
import com.lgl.baiduwallpaper.view.MyBottomLayout;

/**
 * 主界面
 */
public class MainActivity extends FragmentActivity {

 private MyBottomLayout myBottomLayout;
 private ViewPager viewpager;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 initView();
 }

 /**
 * 初始化
 */
 private void initView() {
// initPagerContent(new HomeFragment());
 findView();
 setOnclick();
 }

// /**
// * 设置默认的第一页数据
// */
// private void initPagerContent(android.app.Fragment fragment) {
// FragmentManager manager = getFragmentManager();
// android.app.FragmentTransaction ft = manager.beginTransaction();
// ft.replace(R.id.myContent,fragment);
// ft.commit();
// }

 /**
 * 点击事件
 */
 private void setOnclick() {
 myBottomLayout.setonCallbackListener(new MyCallbackListener());
 }

 /**
 * 找寻控件
 */
 private void findView() {
 myBottomLayout = (MyBottomLayout) findViewById(R.id.myBottomLayout);

 viewpager = (ViewPager) findViewById(R.id.myViewPager);
 viewpager.setAdapter(new MyFragmentAdapter(getSupportFragmentManager()));
 //页面监听
 viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
 @Override
 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

 }

 @Override
 public void onPageSelected(int position) {
 myBottomLayout.initPix(position);
 }

 @Override
 public void onPageScrollStateChanged(int state) {

 }
 });
 }

 /**
 * 切换接口
 */
 private class MyCallbackListener implements MyBottomLayout.ICallbackListener {

 @Override
 public void clic(int id) {
 switch (id) {
 case R.id.homeLayout:
//  initPagerContent(new HomeFragment());
  viewpager.setCurrentItem(0);
  break;
 case R.id.selectLayout:
//  initPagerContent(new SelectFragment());
  viewpager.setCurrentItem(1);
  break;
 case R.id.searchLayout:
//  initPagerContent(new SearchFragment());
  viewpager.setCurrentItem(2);
  break;
 case R.id.locationLayout:
//  initPagerContent(new LoactionFragment());
  viewpager.setCurrentItem(3);
  break;
 case R.id.settingLayout:
//  initPagerContent(new SettingFragment());
  viewpager.setCurrentItem(4);
  break;

 }
 }
 }

 /**
 * viewpager的adapter
 */
 private class MyFragmentAdapter extends FragmentPagerAdapter {

 public MyFragmentAdapter(FragmentManager fm) {
 super(fm);
 }

 @Override
 public Fragment getItem(int position) {
 switch (position) {
 case 0:
  return new HomeFragment();
 case 1:
  return new SelectFragment();
 case 2:
  return new SearchFragment();
 case 3:
  return new LoactionFragment();
 case 4:
  return new SettingFragment();
 }
 return null;
 }

 @Override
 public int getCount() {
 //5个页面
 return 5;
 }
 }
}

主要是你切换的时候setCurrentItem(id);同时监听viewpager的滑动,就可以自由切换了,我们运行一下

这里写图片描述

源码下载: Android仿百度壁纸客户端

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


推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 本文是关于自学Android的笔记,包括查看类的源码的方法,活动注册的必要性以及布局练习的重要性。通过学习本文,读者可以了解到在自学Android过程中的一些关键点和注意事项。 ... [详细]
  • 本文介绍了在Mac上安装Xamarin并使用Windows上的VS开发iOS app的方法,包括所需的安装环境和软件,以及使用Xamarin.iOS进行开发的步骤。通过这种方法,即使没有Mac或者安装苹果系统,程序员们也能轻松开发iOS app。 ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • 本文介绍了Android中的assets目录和raw目录的共同点和区别,包括获取资源的方法、目录结构的限制以及列出资源的能力。同时,还解释了raw目录中资源文件生成的ID,并说明了这些目录的使用方法。 ... [详细]
author-avatar
wbklzh
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有