课程地址

http://www.kgc.cn/android/28013.shtml

主要讲课内容,首页轮播和个人中心的自定义布局
效果图


课程源码

http://www.kgc.cn/bbs/post/154285.shtml

一。首页轮播的设置
二。自定义VIew实现个人中心,其实个人中心实现的方法很多的,这样实现也不知道是不是最好的,但是也是学习下


一。首页轮播

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"android:orientation="vertical"><RelativeLayout
        android:layout_width="match_parent"android:layout_height="@dimen/home_fragment_viewpager_h"><android.support.v4.view.ViewPager
            android:id="@+id/fragment_img_viewpager"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/color_bottom_layout_bg"/><LinearLayout
            android:id="@+id/fragment_point_subscript"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="@dimen/home_fragment_point_bottom"android:orientation="horizontal"/></RelativeLayout><TextView
        android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:text="首页界面"android:textColor="@color/colorPrimary"android:textSize="22sp"/>
</RelativeLayout>
<LinearLayout
            android:id="@+id/fragment_point_subscript"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="@dimen/home_fragment_point_bottom"android:orientation="horizontal"/>

代表加载的轮播点

public class HomeFragment extends BaseFragment {private View view;//根布局private ViewPager vp;private List<View> views;//轮播图展示图片viewprivate MViewpager vp_adapter;//viewpager适配器private Timer timer;//计时器private LinearLayout layout;//轮播图下标集合private int count = 0;//轮播图当前下标public final int GetImags = 1014;//获取广告图返回码private final int AnnFaild = 1011;//获取广告图失败返回码@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_home, container, false);LogUtils.i(TAG, "首页界面加载..........");initView();//初始化UI组件initdata();//初始化数据return view;}/*** 初始化控件加载*/private void initView() {vp = (ViewPager) view.findViewById(R.id.fragment_img_viewpager);layout = (LinearLayout) view.findViewById(R.id.fragment_point_subscript);}/*** 初始化数据展示*/private void initdata() {if (views == null) {views = new ArrayList<View>();}vp_adapter = new MViewpager();vp.setAdapter(vp_adapter);//添加界面滚动监听vp.addOnPageChangeListener(vp_adapter);//首页轮播图获取OkHttpManager.getInstance().getNet(Constant.Announcement, new OkHttpManager.ResultCallback() {@Overridepublic void onFailed(Request request, IOException e) {getAnnFailure();}@Overridepublic void onSuccess(String response) {getAnnSuccess(response);}});}/*** 从服务端获取公告信息失败* 此时展示数据库缓存数据*/private void getAnnFailure() {//从数据库中获取数据List<AnnImgs> imgs_dblist = DataSupport.findAll(AnnImgs.class);if (imgs_dblist != null) {updateAnnShow(imgs_dblist);}}/*** 从服务端获取公告信息成功*/private void getAnnSuccess(String resultImgs) {//服务端返回有效数据则展示,没有不做处理if (resultImgs != null && !"".equals(resultImgs)) {Gson gson = new Gson();AnnImageResult air = gson.fromJson(resultImgs, AnnImageResult.class);List<AnnImgs> imgs_list = air.getBody();if (imgs_list == null) {imgs_list = new ArrayList<AnnImgs>();}updateAnnShow(imgs_list);//更新缓存if (imgs_list.size() > 0) {//从数据库清除数据保存DataSupport.deleteAll(AnnImgs.class);//添加新数据到数据库DataSupport.saveAll(imgs_list);}}}/*** 根据公告图片地址动态更新界面** @param imgs_dblist*/private void updateAnnShow(List<AnnImgs> imgs_dblist) {views.clear();//动态创建轮播展示viewfor (int i = 0; i < imgs_dblist.size(); i++) {ImageView img = new ImageView(mActivity);img.setScaleType(ImageView.ScaleType.CENTER_CROP);//通过网络地址显示图片Picasso.with(mActivity).load(Constant.BaseUrl + imgs_dblist.get(i).getImgUrl()).into(img);views.add(img);}//更新界面显示vp_adapter.notifyDataSetChanged();//添加指示器下标点initPoint();//开启任务计时器if (timer == null) {timer = new Timer();timer.schedule(task, 0, 3000);}}//创建viewpager适配器class MViewpager extends PagerAdapter implements ViewPager.OnPageChangeListener {@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0 == arg1;}@Overridepublic int getCount() {return views.size();}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView(views.get(position));}@Overridepublic Object instantiateItem(ViewGroup container, int position) {container.addView(views.get(position));return views.get(position);}/*** viewpager滑动监听,动态更改指示下标的选中状态* @param position*/@Overridepublic void onPageSelected(int position) {for (int i = 0; i < layout.getChildCount(); i++) {ImageView image = (ImageView) layout.getChildAt(i);if (i == position) {image.setSelected(true);} else {image.setSelected(false);}}}@Overridepublic void onPageScrollStateChanged(int arg0) {}@Overridepublic void onPageScrolled(int arg0, float arg1, int arg2) {}}Handler mHandler = new Handler() {@Overridepublic void handleMessage(android.os.Message msg) {switch (msg.what) {case Constant.Scroll://接收滚动消息,并执行vp.setCurrentItem(count);break;default:break;}}};/*** 创建图片变化下标图标*/public void initPoint() {//清除所有指示下标layout.removeAllViews();for (int i = 0; i < views.size(); i++) {ImageView img = new ImageView(mActivity);//添加下标圆点参数LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);params.leftMargin = 5;params.rightMargin = 5;img.setLayoutParams(params);img.setImageResource(R.drawable.sns_v2_page_point);if (i == 0) {img.setSelected(true);}layout.addView(img);}}// 创建记时器发送图片轮播消息TimerTask task = new TimerTask() {@Overridepublic void run() {if (count == views.size()) {count = 0;} else {count = count + 1;}mHandler.sendEmptyMessage(Constant.Scroll);}};
}

其实网上还有很多写轮播图的框架,感觉应该比自己写的坑少一点吧


二。自定义View
1.先创建一个androidlib
在values中创建attrs
创建自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="SetItemView"><attr name="leftText" format="string"/><attr name="leftIcon" format="integer"/><attr name="rightIcon" format="integer"/><attr name="textSize" format="float"/><attr name="textColor" format="color"/><attr name="isShowUnderLine" format="boolean"/></declare-styleable>
</resources>
SetItemView
public class SetItemView extends RelativeLayout {//页面对象private View mView;//页面跟布局private RelativeLayout mRootLayout;//左侧文字private TextView mTvLeftText;//左侧图标private ImageView mIvLeftIcon;//右侧图标private ImageView mIvRightIcon;//下划线private View mUnderLine;//显示文字private String mText;//文字颜色private int mTextColor;//文字大小private int mTextSize;//左侧图标private Drawable mLeftIcon;// 右侧图标private Drawable mRightIcon;//左侧边距private int mMarginLeft;//右侧边距private int mMarginRight;//定义监听器private OnSetItemClick mOnSetItemClick;public SetItemView(Context context) {this(context, null);}public SetItemView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public SetItemView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initView(context);getCustomStyle(context, attrs);//设置点击监听事件mRootLayout.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (null != mOnSetItemClick) {mOnSetItemClick.click();}}});}/*** 初始化控件自定义属性信息** @param context* @param attrs*/private void getCustomStyle(Context context, AttributeSet attrs) {TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SetItemView);int num = typedArray.getIndexCount();for (int i = 0; i < num; i++) {int arr = typedArray.getIndex(i);if (arr == R.styleable.SetItemView_leftText) {mText = typedArray.getString(arr);mTvLeftText.setText(mText);} else if (arr == R.styleable.SetItemView_leftIcon) {mLeftIcon = typedArray.getDrawable(arr);mIvLeftIcon.setImageDrawable(mLeftIcon);} else if (arr == R.styleable.SetItemView_rightIcon) {mRightIcon = typedArray.getDrawable(arr);mIvRightIcon.setImageDrawable(mRightIcon);} else if (arr == R.styleable.SetItemView_textSize) {// 默认设置为16spfloat textSize = typedArray.getFloat(arr, 16);mTvLeftText.setTextSize(textSize);} else if (arr == R.styleable.SetItemView_textColor) {//文字默认灰色mTextColor = typedArray.getColor(arr, Color.GRAY);mTvLeftText.setTextColor(mTextColor);} else if (arr == R.styleable.SetItemView_isShowUnderLine) {boolean flag = typedArray.getBoolean(arr, true);if (!flag) {mUnderLine.setVisibility(View.GONE);}}}typedArray.recycle();}/*** 初始化控件对象*/private void initView(Context context) {mView = View.inflate(context, R.layout.settingitem, this);mRootLayout = (RelativeLayout) mView.findViewById(R.id.rootLayout);mTvLeftText = (TextView) mView.findViewById(R.id.tv_lefttext);mIvLeftIcon = (ImageView) mView.findViewById(R.id.iv_lefticon);mIvRightIcon = (ImageView) mView.findViewById(R.id.iv_righticon);mUnderLine = mView.findViewById(R.id.underline);}public void setmOnSetItemClick(OnSetItemClick mOnSetItemClick) {this.mOnSetItemClick = mOnSetItemClick;}public interface OnSetItemClick {void click();}
}
settingitem.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/rootLayout"android:layout_width="match_parent"android:layout_height="56dp"android:background="@drawable/bg"android:clickable="true"android:gravity="center_vertical"><ImageView
        android:id="@+id/iv_lefticon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="23dp"android:src="@drawable/clearcache"/><TextView
        android:id="@+id/tv_lefttext"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginLeft="10dp"android:layout_toRightOf="@id/iv_lefticon"android:gravity="center_vertical"android:textSize="16sp"/><ImageView
        android:id="@+id/iv_righticon"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_gravity="center"android:layout_marginRight="23dp"android:src="@drawable/task_arrow"/><View
        android:id="@+id/underline"android:layout_width="match_parent"android:layout_height="1px"android:layout_alignParentBottom="true"android:layout_marginLeft="16dp"android:layout_marginRight="16dp"android:background="#99999999"/>
</RelativeLayout>

MeFragment

public class MeFragment extends BaseFragment {private View mView;private SetItemView mMeItem;private SetItemView mAboutItem;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {//加载布局mView = inflater.inflate(R.layout.fragment_me, container, false);initView();return mView;}/*** 初始化控件信息*/private void initView() {mMeItem = (SetItemView) mView.findViewById(R.id.rl_me);mAboutItem = (SetItemView) mView.findViewById(R.id.rl_about);mMeItem.setmOnSetItemClick(new SetItemView.OnSetItemClick() {@Overridepublic void click() {Toast.makeText(mActivity, "点击了个人资料", Toast.LENGTH_SHORT).show();}});mAboutItem.setmOnSetItemClick(new SetItemView.OnSetItemClick() {@Overridepublic void click() {Toast.makeText(mActivity, "点击了关于", Toast.LENGTH_SHORT).show();}});}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:id="@+id/root_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@color/white"android:orientation="vertical"><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#f2f2f2"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"android:orientation="vertical"><ImageViewandroid:id="@+id/iv_header"android:layout_width="@dimen/width_120"android:layout_height="@dimen/height_120"android:layout_marginTop="@dimen/margin_10"android:src="@drawable/defaulthead"/><TextViewandroid:id="@+id/tv_name"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="@dimen/margin_7"android:gravity="center"android:text=""android:textColor="#110d0a"android:textSize="@dimen/textsize_16"/><TextViewandroid:id="@+id/tv_job"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:gravity="center"android:padding="@dimen/margin_13"android:text=""android:textColor="@color/text_color_9"android:textSize="@dimen/textsize_14"/></LinearLayout><com.feng.settingitemlibrary.SetItemViewandroid:id="@+id/rl_me"android:layout_width="match_parent"android:layout_height="wrap_content"app:isShowUnderLine="false"app:leftIcon="@drawable/medata"app:leftText="@string/fragment_me_tv_me"app:rightIcon="@drawable/task_arrow"app:textColor="@color/text_color_6"app:textSize="16"/><com.feng.settingitemlibrary.SetItemViewandroid:id="@+id/rl_clear"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="14dp"app:leftIcon="@drawable/clearcache"app:leftText="@string/fragment_me_tv_clear"app:rightIcon="@drawable/task_arrow"app:textColor="@color/text_color_6"app:textSize="16"/><com.feng.settingitemlibrary.SetItemViewandroid:id="@+id/rl_feedback"android:layout_width="match_parent"android:layout_height="wrap_content"app:leftIcon="@drawable/appfeedback"app:leftText="@string/fragment_me_tv_feedback"app:rightIcon="@drawable/task_arrow"app:textColor="@color/text_color_6"app:textSize="16"/><com.feng.settingitemlibrary.SetItemViewandroid:id="@+id/rl_about"android:layout_width="match_parent"android:layout_height="wrap_content"app:leftIcon="@drawable/about"app:leftText="@string/fragment_me_tv_about"app:rightIcon="@drawable/task_arrow"app:textColor="@color/text_color_6"app:textSize="16"/><com.feng.settingitemlibrary.SetItemViewandroid:id="@+id/rl_version"android:layout_width="match_parent"android:layout_height="wrap_content"app:leftIcon="@drawable/newversion"app:leftText="@string/fragment_me_tv_version"app:rightIcon="@drawable/task_arrow"app:textColor="@color/text_color_6"app:textSize="16"/><com.feng.settingitemlibrary.SetItemViewandroid:id="@+id/rl_exit"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="30dp"android:layout_marginTop="14dp"app:isShowUnderLine="false"app:leftIcon="@drawable/appexit"app:leftText="@string/fragment_me_tv_exit"app:rightIcon="@drawable/task_arrow"app:textColor="@color/text_color_6"app:textSize="16"/></LinearLayout></ScrollView></RelativeLayout>

课工场 “微服私访”项目学习(五)相关推荐

  1. 课工场 “微服私访”项目学习(六)

    本身上周五,就应该写好了,磨磨唧唧到了这周一才把这个闹完, 今天的内容呢,首页的fragment中加入两个recycyleView 其实挺简单的,就是recycleView的是简单使用(我也不知道任务 ...

  2. 课工场:深化协同育人产教融合,创新基地合作新模式

    2019独角兽企业重金招聘Python工程师标准>>> IFTNews:当前,以人工智能为代表的智能革命浪潮下,我国企业转型及产业升级亟需大量的新科技人才.人才作为第一生产力,高等教 ...

  3. 课工场获批教育部产学合作协同育人项目

    2019独角兽企业重金招聘Python工程师标准>>> 12月12日,教育部高等教育司公布了<有关企业支持的产学合作协同育人项目申报指南(2018年第二批)的函>,课工场 ...

  4. 课工场与河南质量工程职业学院就“物联网” 校企合作项目展开深入交流

    11月23日,课工场与到访的河南质量工程职业学院代表团就对方共同关心的校企共建合作项目交易进行了友好的会谈,特别是在物联网等新兴技术方向,双方就教育和产业发展进行了深入的交流. 课工场相关负责人介绍, ...

  5. 医学计算机应用基础项目五思维导图,【羊五小•项目学习】“一千零一夜+思维导图”学习成果发布会(五年级)...

    原标题:[羊五小•项目学习]"一千零一夜+思维导图"学习成果发布会(五年级) 在"思维导图"的世界里遇到最美好的自己 题记 学校借助美国项目学习理念,围绕&qu ...

  6. 韦东山 数码相框 项目学习(五)libjpeg-turbo的移植

    韦东山 数码相框 项目学习(五)libjpeg-turbo的移植 效果图 能够在100ask STM32MP157上显示一张JPG图片 一.下载源码 首先去libjpeg-turbo官网下载源码,这里 ...

  7. “中国软件杯”西北大学宣讲开启 课工场人工智能专家深度讲解解题策略

    IFTNews:第八届"中国软件杯"大学生软件设计大赛高校宣讲巡展活动在西北大学正式启动.大赛首场宣讲活动选在西北大学,并邀请到了西北大学信息科学与技术学院副院长冯筠,中国航空航天 ...

  8. 课工场AI尖端人才班首期答辩会将举行 特邀人工智能专家现场评审

    IFTNews:当前,人工智能已经成为全球新一轮产业变革的核心驱动力,工业自动化正逐步进入智能化时代,物联网将进化为智能网,各行各业正尝试孕育不同的智能化产品.教育机构作为为社会培养输送社会所需对口专 ...

  9. java 论坛_武汉课工场JAVA培训:“真AI、超智能”人工智能大咖论坛解读

    "真AI.超智能"人工智能大咖论坛解读! 2020年8月3日,高薪工作,匠心课程,创新服务--课工场2020年夏季课程发布系列的第1场"真AI.超智能"人工智能 ...

最新文章

  1. Linux下boost库的编译、安装详解
  2. DFS求连通块数目(深搜)
  3. python websocet回调_python – 线程,非阻塞websocket客户端
  4. IDEA中多行注释及取消注释的快捷键分享
  5. Java线程:创建与启动
  6. 令牌验证 token
  7. matlab调用python_从MATLAB调用Python函数
  8. Bootstrap3 表单
  9. 尝试使用Java6API读取java代码
  10. k8s创建pod加入容器_K8S容器编排之POD健康检测(2)
  11. cublas中执行矩阵乘法运算的函数 首先要注意的是cublas使用的是以列为主的存储方式,和c/c++中的以行为主的方式是不一样的。处理方法可参考下面的注释代码
  12. require-ensure
  13. php 显示外链图片,php绘图之加载外部图片的方法
  14. 产品体验报告-美团APP
  15. 32位java jre_64位的jre和32位的jre
  16. java 四分位算法_四分位数怎么算
  17. 打败主力的神奇划线法
  18. 2022-2028全球与中国立式填模封口机市场现状及未来发展趋势
  19. 国庆放假前一周,再学这些技术,安安稳稳过假期
  20. rust 飞天指令_腐蚀RUST代码大全 腐蚀RUST指令代码一览

热门文章

  1. (HDU)1283 -- 最简单的计算机
  2. ei加声调怎么加_复韵母如何标声调
  3. 【目标检测】RCNN系列
  4. [ERP]LRP与MRP的最大差异
  5. 三相同步发电机的平衡方程式
  6. c++练习1)口袋中有红,黄,蓝,白,黑五种颜色的球若干个,每次从口袋中取出3个不同颜色的球,有多少种取法?
  7. Steam 2023新品节免费试玩 开发团队下场直播
  8. 张飞硬件设计与开发 第四部
  9. 欧洲之星Fotona 4dpro的效果好吗,多久做一次
  10. 如何才能管理好一个项目群