设为首页 加入收藏

TOP

Android 自定义控件 轻松实现360软件详情页(一)
2015-02-25 16:15:22 来源: 作者: 【 】 浏览:68
Tags:Android 定义 控件 轻松 实现 360 软件 详情

最近有不少朋友私聊问应用宝、360软件助手之类的软件详情页怎么做,刚好,最近有时间就模仿360软件助手详情页给大家做个Demo,供大家参考。嗯,关于实现呢,我写了两种方式:


1、ScrollView内嵌软件介绍+ViewPager+ViewPager中是ScrollView,这种方式呢,纯原生,没有涉及到自定义控件,但是这样嵌套呢,涉及到测量以及事件的冲突处理,大家可以自己尝试去做一下,想像起来蛮容易的,做起来其实还是挺费劲的,代码我会给出,核心代码不多,大家自行参考。本文将重点分析第二种方式。


2、将做外层的ScrollView改为了自定义的一个控件,继承自LinearLayout,叫做StickyNavLayout,这里感谢小七的命名,同时本方式感谢二群暖暖提供的源码。


最后看下效果图,第一张是360的,第二张是我们的:


360:



擦,别问我为什么这么模糊,尽力了。。。


我们的效果图:



?


上面我们也说了,之所以有第二种方式,完全是为了考虑使用的容易度。


values/ids_sticky_nav_Llayout.xml




? ?
? ?
? ?
? ?



定义了几个id资源,主要是为了方便使用了,供声明布局时使用的,看名字应该能猜出来吧,猜不出来没事,接下来我就贴布局文件了。这个其实不属于使用方式了,但是下文会见到,所以提前贴出来。


? ? ? ? ? ? android:id="@id/id_stickynavlayout_topview"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="300dp"
? ? ? ? android:background="#4400ff00" >


? ? ? ? ? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="match_parent"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:text="软件介绍"
? ? ? ? ? ? android:textSize="30sp"
? ? ? ? ? ? android:textStyle="bold" />
? ?


? ? ? ? ? ? android:id="@id/id_stickynavlayout_indicator"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="50dp"
? ? ? ? android:background="#ffffffff" >
? ?


? ? ? ? ? ? android:id="@id/id_stickynavlayout_viewpager"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:background="#44ff0000" >
? ?



最外层是我们的自定义的控件StickyNavLayout,然后是顶部内容区域,Vp的指示器,ViewPager。按照效果图,去写就ok,注意部分id使用我们预设定的id资源。因为我们的StickyNavLayout需要通过id找到该控件,去进行一些计算。


然后在我们的MainActivity中,对ViewPager进行初始化即可。


没有什么复杂的代码,主要就是初始化我们的Vp;


我们的Vp中每个页面为一个Fragment,Fragment的代码我们就不贴了,布局就是ScrollView为根布局,内部随意填充,具体可参考源码。


介绍完了用法有木有一点小激动,基本按照常规去写布局就ok,效果自动实现。


贴一下我们的Fragment代码:


package com.zhy.sample.StickyNavLayout;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class TabFragment extends Fragment
{
?public static final String TITLE = "title";
?private String mTitle = "Defaut Value";
?private TextView mTextView;


?@Override
?public void onCreate(Bundle savedInstanceState)
?{
? super.onCreate(savedInstanceState);
? if (getArguments() != null)
? {
? ?mTitle = getArguments().getString(TITLE);
? }
?}


?@Override
?public View onCreateView(LayoutInflater inflater, ViewGroup container,
? ?Bundle savedInstanceState)
?{
? View view = inflater.inflate(R.layout.fragment_tab, container, false);
? mTextView = (TextView) view.findViewById(R.id.id_info);
? mTextView.setText(mTitle);
? return view;


?}


?public static TabFragment newInstance(String title)
?{
? TabFragment tabFragment = new TabFragment();
? Bundle bundle = new Bundle();
? bundle.putString(TITLE, title);
? tabFragment.setArguments(bundle);
? return tabFragment;
?}


}


?


? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:id="@id/id_stickynavlayout_innerscrollview"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:scrollbars="none" >


? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:background="#eee"
? ? ? ? android:orientation="vertical"
? ? ? ? android:padding="5dp" >


? ? ? ? ? ? ? ? ? ? android:id="@+id/id_info"
? ? ? ? ? ? android:layout_width="match_parent"
?

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android 属性动画(Property Anima.. 下一篇Android 教你打造炫酷的ViewPager..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: