1.概述
本文将介绍一个android版新闻类app模板,是本人整合了网上各类资源后整理出来的,希望对大家有所帮助!
iOS版新闻类app模板详见:
本模板app截图如下:
2.具体实现
该模板主体是一个FragmentActivity,布局文件activity_main.xml如下:
<span style="font-size:12px;"><span style="font-size:12px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/main_head" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40.0dip"
android:background="#fff3f3f3"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/rl_column"
android:layout_width="match_parent"
android:layout_height="40.0dip" >
<com.latu.qingcheng123.widget.ColumnHorizontalScrollView
android:id="@+id/mColumnHorizontalScrollView"
android:layout_width="match_parent"
android:layout_height="40.0dip"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/mRadioGroup_content"
android:layout_width="fill_parent"
android:layout_height="40.0dip"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip" />
</com.latu.qingcheng123.widget.ColumnHorizontalScrollView>
<ImageView
android:id="@+id/shade_left"
android:layout_width="10.0dip"
android:layout_height="40.0dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="@string/img_content_desc"
android:background="@drawable/channel_leftblock"
android:visibility="visible" />
<ImageView
android:id="@+id/shade_right"
android:layout_width="10.0dip"
android:layout_height="40.0dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:contentDescription="@string/img_content_desc"
android:background="@drawable/channel_rightblock"
android:visibility="visible" />
</RelativeLayout>
</LinearLayout>
<View
android:id="@+id/category_line"
android:layout_width="fill_parent"
android:layout_height="0.5dip"
android:background="#ffdddddd" />
<android.support.v4.view.ViewPager
android:id="@+id/mViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout></span></span>
其中:
a. <include layout="@layout/main_head" />是titlebar;
b. <com.latu.qingcheng123.widget.ColumnHorizontalScrollView>定义了左右滑动、展现新闻类型(头条、咨询、招聘等)的控件;
c. <android.support.v4.view.ViewPager>定义了展现各类新闻列表的viewpager,同样响应左右滑动操作,包含顶部滚动广告栏。
对应的MainActivity文件如下:
<span style="font-size:12px;"><span style="font-size:12px;">package com.latu.qingcheng123.activity;
import java.util.ArrayList;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.latu.qingcheng123.R;
import com.latu.qingcheng123.adapter.CategoryFragmentPagerAdapter;
import com.latu.qingcheng123.fragment.CategoryFragment;
import com.latu.qingcheng123.util.BaseTools;
import com.latu.qingcheng123.vo.CategoryItem;
import com.latu.qingcheng123.widget.ColumnHorizontalScrollView;
import com.latu.qingcheng123.widget.DrawerView;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
private ColumnHorizontalScrollView mColumnHorizontalScrollView;
private LinearLayout mRadioGroup_content;
private RelativeLayout rl_column;
private ViewPager mViewPager;
/** 左阴影部分 */
public ImageView shade_left;
/** 右阴影部分 */
public ImageView shade_right;
/** head 头部 的左侧菜单 按钮 */
private ImageView top_head;
protected SlidingMenu side_drawer;
/** 屏幕宽度 */
private int mScreenWidth = 0;
/** Item宽度 */
private int mItemWidth = 0;
private long mExitTime;
/** 当前选中的栏目 */
private int columnSelectIndex = 0;
private ArrayList<CategoryItem> m_lCategories = new ArrayList<CategoryItem>();
private ArrayList<Fragment> m_fragments = new ArrayList<Fragment>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initSlidingMenu();
}
/**
* 初始化layout控件
*/
private void initView() {
mScreenWidth = BaseTools.getWindowsWidth(this);
mItemWidth = mScreenWidth / 7;// 一个Item宽度为屏幕的1/7
mColumnHorizontalScrollView = (ColumnHorizontalScrollView) findViewById(R.id.mColumnHorizontalScrollView);
mRadioGroup_content = (LinearLayout) findViewById(R.id.mRadioGroup_content);
rl_column = (RelativeLayout) findViewById(R.id.rl_column);
mViewPager = (ViewPager) findViewById(R.id.mViewPager);
shade_left = (ImageView) findViewById(R.id.shade_left);
shade_right = (ImageView) findViewById(R.id.shade_right);
top_head = (ImageView) findViewById(R.id.top_head);
top_head.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (side_drawer.isMe