Android实现网易新闻客户端首页效果
作者:shineflowers 发布时间:2023-06-09 02:21:06
关于实现网易新闻客户端的界面,以前写过很多博客,请参考:
Android实现网易新闻客户端效果
Android实现网易新闻客户端侧滑菜单(一)
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
猜你喜欢
- The java.io.Writer.flush() method flushes the stream. If the stream ha
- 本文实例为大家分享了Android内存中存储用户名和密码的方法,供大家参考,具体内容如下首先是配置文件:<LinearLayout x
- 关于SQLiteSQLite是一款轻型的嵌入式的遵守ACID的关系型数据库管理系统,诞生已有15个年头了。随着移动互联的发展,现在得到了更广
- 有一天,你写了好多好多带“形参”的构造函数(就是“方法”,同义),而且需要向这些构造函数里传递同样的“实参”,然后你就憨憨地一个一个函数的调
- 读取文本文件中的内容此示例读取文本文件的内容以使用 System.IO.File 选件类的静态方法 ReadAllText 和 ReadAl
- 废话不多说,先给大家贴代码,具体代码如下所示:import net.sf.json.JSONArray; import net.sf.jso
- 今天看见一个Android 扑克卡片翻转效果的帖子,于是手痒想学一学,由于接触过的Animation动画等比较少,所以感觉很新奇。首先,说一
- 项目需要对接外部接口,将图片文件流发送到外部接口,下面代码就是HttpsURLConnection如何上传文件流:/** *
- mybatis实体类字段大小写问题 字段获取不到值由于前期设计问题,项目中需要用到的一个字段 rootpath,所以我再实体层加了这么一个字
- 经常用 MyBatis 的的都知道,使用这个框架存在一个非常不友善的问题就是,就是每操作一个单表就需要自己手写一个 xml 文件,虽然说可以
- 模式介绍桥接模式(Bridge模式)是指:将实现与抽象放在两个不同的类层次中,使两个层次可以独立改变。是一种结构型设计模式。Bridge模式
- 我就废话不多说了,大家还是直接看代码吧~<?xml version="1.0" encoding="UT
- 1 什么是条件变量condition_variable是一个类,常和mutex搭配使用。condition_variable类是一个同步原语
- 现在我们上网会用百度或者谷歌搜索信息,当我们在输入框里输入一两个字后,就会自动提示我们想要的信息,这种效果在Android 是通过
- 引言在之前的文章里,我们聊到了 Java 标准库中 HashMap 与 LinkedHashMap 的实现原理。HashMap 是一个标准的
- 一、继承1、继承的概念继承机制:是面向对象程序设计是代码可以复用的最重要手段,允许程序员在保持原有类特性的基础上进行扩展,增加新的功能,产生
- Kotlin开发Android应用实例详解我们简单的知道了Kotlin这门新语言的优势,也接触了一些常见的语法及其简单的使用,相信你会对它有
- PrintStream 介绍PrintStream 是打印输出流,它继承于FilterOutputStream。PrintStream 是用
- 一.线程池简介线程池的概念线程池就是首先创建一些线程,它们的集合称为线程池,使用线程池可以很好的提高性能,线程池在系统启动时既创建大量空闲的
- 在实现蓝牙接收时,网上的资料很多,使用起来也很简单,但是我觉得还是有必要把这些知识总结下来。蓝牙开发需要用到一个第三方的库InTheHand