Android实现网易新闻客户端首页效果
关于实现网易新闻客户端的界面,以前写过很多博客,请参考:
今天用viewpager + fragmentadapter + viewpagerindicator来实现。
viewpagerindicator是一款分页指标小部件兼容viewpager,封装上做得非常不错,目前已为众多知名应用所使用。具体api的使用,大家可以下载官方demo示例研究研究就知道啦!
下载地址:https://github.com/jakewharton/viewpagerindicator
sample是提供给我们的例子,library是库工程,我们需要将其作为我们自己项目的依赖库,我们新建一个android工程,将library导入工程我就不介绍了。
注:
eclipse: 如果你新建的项目libs目录下面有android-support-v4.jar,你要将其删除,因为viewpageindicator里面有这个库,我们项目中不允许两个android-support-v4.jar,不删除我们的项目不能编译的。
android studio: 直接import module就行。
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <include layout="@layout/activity_top" /> <com.viewpagerindicator.tabpageindicator android:id="@+id/indicator" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/transparent" > </com.viewpagerindicator.tabpageindicator> <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#eef3fa"> </android.support.v4.view.viewpager> </linearlayout>
布局很简单,顶部引入了一个activity_top.xml的布局,具体内容可以自己定义。
mainactivity.java
package com.jackie.neteasenews; import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.view.viewpager; import com.viewpagerindicator.tabpageindicator; import java.util.arraylist; import java.util.list; public class mainactivity extends fragmentactivity { private tabpageindicator mtabpageindicator; private viewpager mviewpager; private viewpagerindicatoradapter madapter; private headlinefragment mheadlinefragment; private enjoyfragment menjoyfragment; private hotspotfragment mhotspotfragment; private sportfragment msportfragment; private housefragment mhousefragment; private list<fragment> mfragmentlist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); } private void initview() { mtabpageindicator = (tabpageindicator) findviewbyid(r.id.indicator); mviewpager = (viewpager) findviewbyid(r.id.viewpager); mheadlinefragment = new headlinefragment(); menjoyfragment = new enjoyfragment(); mhotspotfragment = new hotspotfragment(); msportfragment = new sportfragment(); mhousefragment = new housefragment(); mfragmentlist = new arraylist<>(); mfragmentlist.add(mheadlinefragment); mfragmentlist.add(menjoyfragment); mfragmentlist.add(mhotspotfragment); mfragmentlist.add(msportfragment); mfragmentlist.add(mhousefragment); madapter = new viewpagerindicatoradapter(getsupportfragmentmanager(), mfragmentlist); mviewpager.setadapter(madapter); //实例化tabpageindicator然后设置viewpager与之关联 mtabpageindicator.setviewpager(mviewpager, 1); } }
viewpagerindicatoradapter.java
package com.jackie.neteasenews; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import java.util.list; public class viewpagerindicatoradapter extends fragmentpageradapter { private list<fragment> mfragmentlist; public static string[] titles = new string[] { "头条", "娱乐", "热点", "体育", "房产" }; public viewpagerindicatoradapter(fragmentmanager fm, list<fragment> fragmentlist) { super(fm); this.mfragmentlist = fragmentlist; } @override public fragment getitem(int position) { return mfragmentlist.get(position); } @override public int getcount() { return mfragmentlist.size(); } @override public charsequence getpagetitle(int position) { return titles[position]; } }
代码很简单,但是有一点,上面的indicator是系统默认的,不太好看,所以还需要在styles.xml添加下面的样式:
<style name="styledtabpageindicator" parent="@android:style/theme.light"> <item name="vpitabpageindicatorstyle">@style/customtabpageindicator</item> <item name="android:windownotitle">true</item> <item name="android:animationduration">5000</item> <item name="android:windowcontentoverlay">@null</item> </style> <style name="customtabpageindicator" parent="widget"> <item name="android:gravity">center</item> <item name="android:background">@drawable/tab_indicator</item> <!--<item name="android:background">@drawable/vpi__tab_indicator</item>--> <item name="android:paddingleft">22dip</item> <item name="android:paddingright">22dip</item> <item name="android:paddingtop">8dp</item> <item name="android:paddingbottom">8dp</item> <item name="android:textappearance">@style/customtabpageindicator.text</item> <item name="android:textsize">16sp</item> <item name="android:maxlines">1</item> </style> <style name="customtabpageindicator.text" parent="widget"> <item name="android:textcolor">@drawable/tab_text</item> </style>
注意:开发中过程中跟fragment相关的类,导入包时会提示两个包android.app 和 android.support.v4.app,切记,要保证所有类都导入同一个包下的,否则会编译报错。
效果图如下:
附上源码地址:https://github.com/shineflower/neteasenews.git
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: python出现