android 资讯阅读器

时间:2022-05-10 22:29:05

最近找申请到了一个不错的接口 , 非常适合拿来写一个资讯类的app.

现在着手写,随写随更.也算是抛砖引玉.烂尾请勿喷.╭(╯^╰)╮

android 资讯阅读器

第一阶段目标样式(滑动切换标签 , 侧滑菜单):

android 资讯阅读器  android 资讯阅读器

      图1                图2

首先我使用的ide 是android studio 2.0 已经集成了侧滑菜单的module

新建的时候选一下就好了.过程就不细说了.这个很简单.

下面就是实现滑动切换tab的效果啦.其实我这个写的并不是很好看,网上还有很多开源的滑动切寒tab的案例

盆友们可以自己去集成下.下面说下我的tab的实现效果(参考自:http://www.imooc.com/learn/264  大神鸿洋的慕课)

实现方法是viewpage+Fragment (单单用fragment并没有实现滑动效果).

如果你跟我一样用的是 图2 的模板 那么就要在content_main.xml下做如下修改

content_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.zfls.didainf.MainActivity"
tools:showIn="@layout/app_bar_main"
android:orientation="vertical"> <include layout="@layout/topbar"/> <android.support.v4.view.ViewPager
android:id="@+id/id_viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</android.support.v4.view.ViewPager> </LinearLayout>

使用的viewpage千万不要引入错咯.

topbar.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:orientation="horizontal"> <LinearLayout
android:id="@+id/topical"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"> <TextView
android:id="@+id/topical_text_bg"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:gravity="center"
android:text="热点"
android:textColor="#fff"
android:textSize="15dp" />
</LinearLayout> <LinearLayout
android:id="@+id/entertainment"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"> <TextView
android:id="@+id/entertainment_text_bg"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:gravity="center"
android:text="娱乐"
android:textColor="#fff"
android:textSize="15dp" />
</LinearLayout> <LinearLayout
android:id="@+id/lettres"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"> <TextView
android:id="@+id/lettres_text_bg"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:gravity="center"
android:text="美文"
android:textColor="#fff"
android:textSize="15dp" />
</LinearLayout> <LinearLayout
android:id="@+id/funny"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"> <TextView
android:id="@+id/funny_text_bg"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:gravity="center"
android:text="搞笑"
android:textColor="#fff"
android:textSize="15dp" />
</LinearLayout> <LinearLayout
android:id="@+id/jokes"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"> <TextView
android:id="@+id/jokes_text_bg"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:gravity="center"
android:text="段子"
android:textColor="#fff"
android:textSize="15dp" />
</LinearLayout> <LinearLayout
android:id="@+id/games"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"> <TextView
android:id="@+id/games_text_bg"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:gravity="center"
android:text="游戏"
android:textColor="#fff"
android:textSize="15dp" />
</LinearLayout> </LinearLayout>

很好理解, topbar 就是做一个顶部的布局,然后放到整体的布局中去.

效果如下(还不错 O(∩_∩)O哈!):

android 资讯阅读器

放进整体布局后就如 图1 的样子.

下面再创建一个布局文件用来做每个模块的容器

tab_topcial.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swiperefreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"> <ListView
android:id="@+id/tab_topical_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>

这里说明下:

 <android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swiperefreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">   ... </android.support.v4.widget.SwipeRefreshLayout>

SwipeRefreshLayout 是谷歌提供的下拉刷新模块.用它包裹listview 可以实现下拉刷新的效果(具体用法可以自行百度一下,或者等我再发一篇博客 嘿嘿)

下面是java代码了.

MainActivity.java :

package com.zfls.didainf;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,View.OnClickListener { private ViewPager mViewPager;
private FragmentPagerAdapter mAdapter;
private List<Fragment> mFragments; //tab
private LinearLayout topical;
private LinearLayout entertainment;
private LinearLayout lettres;
private LinearLayout funny;
private LinearLayout jokes;
private LinearLayout games; private TextView topical_t;
private TextView entertainment_t;
private TextView lettres_t;
private TextView funny_t;
private TextView jokes_t;
private TextView games_t; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initside(); initView(); initWork(); setSelect(0);
}

   //统一监听
private void initWork() { topical.setOnClickListener(this);
entertainment.setOnClickListener(this);
lettres.setOnClickListener(this);
funny.setOnClickListener(this);
jokes.setOnClickListener(this);
games.setOnClickListener(this); }
   //声明相关控件
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.id_viewpager); topical = (LinearLayout) findViewById(R.id.topical);
entertainment = (LinearLayout) findViewById(R.id.entertainment);
lettres = (LinearLayout) findViewById(R.id.lettres);
funny = (LinearLayout) findViewById(R.id.funny);
jokes = (LinearLayout) findViewById(R.id.jokes);
games = (LinearLayout) findViewById(R.id.games); topical_t = (TextView) findViewById(R.id.topical_text_bg);
entertainment_t = (TextView) findViewById(R.id.entertainment_text_bg);
lettres_t = (TextView) findViewById(R.id.lettres_text_bg);
funny_t = (TextView) findViewById(R.id.funny_text_bg);
jokes_t = (TextView) findViewById(R.id.jokes_text_bg);
games_t = (TextView) findViewById(R.id.games_text_bg); mFragments = new ArrayList<Fragment>();
Fragment mTab01 = new TopicalFragment();
Fragment mTab02 = new EntertainmentFragment();
Fragment mTab03 = new LetteresFragment();
Fragment mTab04 = new FunnyFragment();
Fragment mTab05 = new JokesFragment();
Fragment mTab06 = new GamesFragment();
mFragments.add(mTab01);
mFragments.add(mTab02);
mFragments.add(mTab03);
mFragments.add(mTab04);
mFragments.add(mTab05);
mFragments.add(mTab06); mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) { @Override
public android.support.v4.app.Fragment getItem(int position) {
return mFragments.get(position);
} @Override
public int getCount() {
return mFragments.size();
}
};
mViewPager.setAdapter(mAdapter); mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override
public void onPageSelected(int position) {
int currentItem = mViewPager.getCurrentItem();
setTab(currentItem);
} @Override
public void onPageScrollStateChanged(int state) { }
}); } //侧滑
private void initside() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState(); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
} @Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
} @SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId(); if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
} @Override
public void onClick(View v) {
switch (v.getId()){
case R.id.topical:
//热点
setSelect(0);
break;
case R.id.entertainment:
//娱乐
setSelect(1);
break;
case R.id.lettres:
//美文
setSelect(2);
break;
case R.id.funny:
//搞笑
setSelect(3);
break;
case R.id.jokes:
//段子
setSelect(4);
break;
case R.id.games:
//游戏
setSelect(5);
break; default:
break;
}
} private void setSelect(int i) {
setTab(i);
mViewPager.setCurrentItem(i);
}    //修改被选中的tab 文字的背景颜色(R.color.color.colorAccent 在 res/value/colors.xml 下)
private void setTab(int i) { changbg();
switch (i){
case 0:
topical_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorAccent));
break;
case 1:
entertainment_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorAccent));
break;
case 2:
lettres_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorAccent));
break;
case 3:
funny_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorAccent));
break;
case 4:
jokes_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorAccent));
break;
case 5:
games_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorAccent));
break;
}
}
   //将所有文字背景色改成未选中状态
private void changbg()
{
topical_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorWrite));
entertainment_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorWrite));
lettres_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorWrite));
funny_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorWrite));
jokes_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorWrite));
games_t.setTextColor(ContextCompat.getColor(MainActivity.this , R.color.colorWrite));
}
}

看起来挺多挺吓人,其实还好不算太多.

附件里面有鸿洋大神的demo,可以拿来移植一下.嘿嘿嘿

百度云链接 : http://pan.baidu.com/s/1boHCUbt

提取码:nhn2

移植完了以后就可以在手机上看下效果了.

android 资讯阅读器的更多相关文章

  1. android 资讯阅读器&lpar;二&rpar;

    接着上次的博客,上次移植完了tab以后整个app的框架就算是定下来了. 本次目标: 1.数据的获取与展示(ListView) 2.官方的下拉刷新效果(SwipeRefreshLayout) 3.数据接 ...

  2. android rss阅读器开发一点小技巧

    这几天一直在学习开发Rss阅读器,遇到一个很坑的问题,InputSource这里总是出错.弄了好久,终于让我找到一个解决方法----看代码: new Thread(){ @Override publi ...

  3. Android NurReaderView 阅读器 (字符串-&period;txt文件)

    有些地方还没配置好.2/3天后在更新.... 功能 支持字符串和<.txt>文件 文字自动分各个页面 支持从右到左-(从右边开始的语言.比如*语哈扎克语...外国的阿拉伯语等) 支持自 ...

  4. android优化中国风应用、完整NBA客户端、动态积分效果、文件传输、小说阅读器等源码

    Android精选源码 android拖拽下拉关闭效果源码 一款优雅的中国风Android App源码 EasySignSeekBar一个漂亮而强大的自定义view15 android仿蘑菇街,蜜芽宝 ...

  5. 电子书及阅读器Demo

    电子书阅读器(Kindle,电子纸技术.LCD.电子墨水技术等: 亚马逊/当当网站)  电子书产业可分5大环节:内容供应商.数字格式制作商.内容流通服务平台.传输平台以及终端阅读器产品. 全球电子书市 ...

  6. Android IT资讯网络阅读器应用源码

    这个是Android IT资讯网络阅读器应用,也是一款通过jsoup解析Html获取内容的网络阅读器,和前面的其实是类似的,也是大学时期闲暇完成,对照CSDN的Web页面元素设计进行解析提取内容,核心 ...

  7. Android 上的代码阅读器 CoderBrowserHD 修改支持 go 语言代码

    我在Android上的代码阅读器用的是 https://github.com/zerob13/CoderBrowserHD 改造的版本,改造后的版本我放在 https://github.com/ghj ...

  8. 将批量下载的博客导入到手机后,通过豆约翰博客阅读器APP(Android手机)进行浏览,白字黑底,保护眼睛,图文并茂。

    首先下面演示的博文来自于以下地址:http://www.douban.com/note/423939291/ 需要先通过博客备份专家将导出的博文导入到手机(还不会用的朋友请先阅读http://www. ...

  9. (android高仿系列)今日头条 --新闻阅读器 (三) 完结 、总结 篇

    从写第一篇今日头条高仿系列开始,到现在已经过去了1个多月了,其实大体都做好了,就是迟迟没有放出来,因为我觉得,做这个东西也是有个过程的,我想把这个模仿中一步一步学习的过程,按照自己的思路写下来,在根据 ...

随机推荐

  1. Android 自动安装脚本

    建立一个install.bat,写入下面 adb install -r %1PAUSE 把apk拖拽到install.bat上

  2. springmvc2 一个控制器写多个方法(非注解方式)

    出处:http://blog.csdn.net/xuewenke/article/details/23895999 springmvc2 一个控制器写多个方法(非注解方式) 分类: spring 20 ...

  3. python之变量篇

    列表:a=['a',112,'bss']元组:只读列表,不能二次赋值str=('s','t',1.5)元字典:用"{ }"标识,键值对存储dic={}dic['s']='test' ...

  4. dg

    package excel; import java.util.Scanner; public class doExcel { public static void main(String args[ ...

  5. LR参数化设置(转)

    LR学习笔记---参数设置 2010-10-20 14:58:55|  分类: 默认分类|举报|字号 订阅     LR在录制程序运行的过程中,VuGen(脚本生成器) 自动生成了包含录制过程中实际用 ...

  6. 面向对象五大原则(SRP、OCP、LSP、DIP、ISP)

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt173 OO的五大原则是指 1. SRP(Single Responsibil ...

  7. python中根据字符串导入模块module

    python中根据字符串导入模块module 需要导入importlib,使用其中的import_module方法 import importlib modname = 'datetime' date ...

  8. 新建node工程

    之前各种node工程都是东抄抄,西抄抄的.  用ng的cli之后,发现非常舒服.所以把node新建工程的种种记录一下. node+babel, 直接按es6标准写就好了,  不需要一定写ts再转码了( ...

  9. LintCode&colon; Number of Airplanes in the Sky

    C++ (1)把interval数组中的所有start和所有end放在同一个数组中,然后进行排序,遇到start就起飞一架飞机,遇到一架end就降落一架飞机,所以start有个+1属性,end有个-1 ...

  10. Required String parameter &&num;39&semi; &&num;39&semi; is not present

    Required String parameter ' ' is not present 报错原因: url中的参数错误. 解决方法: 1.修正url中的参数的值. 2.在Controller层中的@ ...