/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.newsreader;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.SpinnerAdapter;
/**
* Main activity: shows headlines list and articles, if layout permits.
*
* This is the main activity of the application. It can have several different layouts depending
* on the SDK version, screen size and orientation. The configurations are divided in two large
* groups: single-pane layouts and dual-pane layouts.
*
* In single-pane mode, this activity shows a list of headlines using a {@link HeadlinesFragment}.
* When the user clicks on a headline, a separate activity (a {@link ArticleActivity}) is launched
* to show the news article.
*
* In dual-pane mode, this activity shows a {@HeadlinesFragment} on the left side and an
* {@ArticleFragment} on the right side. When the user selects a headline on the left, the
* corresponding article is shown on the right.
*
* If an Action Bar is available (large enough screen and SDK version 11 or up), navigation
* controls are shown in the Action Bar (whether to show tabs or a list depends on the layout).
* If an Action Bar is not available, a regular image and button are shown in the top area of
* the screen, emulating an Action Bar.
*/
public class NewsReaderActivity extends FragmentActivity
implements HeadlinesFragment.OnHeadlineSelectedListener,
CompatActionBarNavListener,
OnClickListener {
// Whether or not we are in dual-pane mode
boolean mIsDualPane = false;
// The fragment where the headlines are displayed
HeadlinesFragment mHeadlinesFragment;
// The fragment where the article is displayed (null if absent)
ArticleFragment mArticleFragment;
// The news category and article index currently being displayed
int mCatIndex = 0;
int mArtIndex = 0;
NewsCategory mCurrentCat;
// List of category titles
final String CATEGORIES[] = { "Top Stories", "Politics", "Economy", "Technology" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
// find our fragments
mHeadlinesFragment = (HeadlinesFragment) getSupportFragmentManager().findFragmentById(
R.id.headlines);
mArticleFragment = (ArticleFragment) getSupportFragmentManager().findFragmentById(
R.id.article);
// Determine whether we are in single-pane or dual-pane mode by testing the visibility
// of the article view.
View articleView = findViewById(R.id.article);
mIsDualPane = articleView != null && articleView.getVisibility() == View.VISIBLE;
// Register ourselves as the listener for the headlines fragment events.
mHeadlinesFragment.setOnHeadlineSelectedListener(this);
// Set up the Action Bar (or not, if one is not available)
int catIndex = savedInstanceState == null ? 0 : savedInstanceState.getInt("catIndex", 0);
setUpActionBar(mIsDualPane, catIndex);
// Set up headlines fragment
mHeadlinesFragment.setSelectable(mIsDualPane);
restoreSelection(savedInstanceState);
// Set up the category button (shown if an Action Bar is not available)
Button catButton = (Button) findViewById(R.id.categorybutton);
if (catButton != null) {
catButton.setOnClickListener(this);
}
}
/** Restore category/article selection from saved state. */
void restoreSelection(Bundle savedInstanceState) {
if (savedInstanceState != null) {
setNewsCategory(savedInstanceState.getInt("catIndex", 0));
if (mIsDualPane) {
int artIndex = savedInstanceState.getInt("artIndex", 0);
mHeadlinesFragment.setSelection(artIndex);
onHeadlineSelected(artIndex);
}
}
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
restoreSelection(savedInstanceState);
}
/** Sets up Action Bar (if present).
*
* @param showTabs whether to show tabs (if false, will show list).
* @param selTab the selected tab or list item.
*/
public void setUpActionBar(boolean showTabs, int selTab) {
if (Build.VERSION.SDK_INT < 11) {
// No action bar for you!
// But do not despair. In this case the layout includes a bar across the
// top that looks and feels like an action bar, but is made up of regular views.
return;
}
android.app.ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
// Set up a CompatActionBarNavHandler to deliver us the Action Bar nagivation events
CompatActionBarNavHandler handler = new CompatActionBarNavHandler(this);
if (showTabs) {
actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_TABS);
int i;
for (i = 0; i < CATEGORIES.length; i++) {
actionBar.addTab(actionBar.newTab().setText(CATEGORIES[i]).setTabListener(handler));
}
actionBar.setSelectedNavigationItem(selTab);
}
else {
actionBar.setNavigationMode(android.app.ActionBar.NAVIGATION_MODE_LIST);
SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.actionbar_list_item,
CATEGORIES);
actionBar.setListNavigationCallbacks(adap, handler);
}
// Show logo instead of icon+title.
actionBar.setDisplayUseLogoEnabled(true);
}
@Override
public void onStart() {
super.onStart();
setNewsCategory(0);
}
/** Sets the displayed news category.
*
* This causes the headlines fragment to be repopulated with the appropriate headlines.
*/
void setNewsCategory(int categoryIndex) {
mCatIndex = categoryIndex;
mCurrentCat = NewsSource.getInstance().getCategory(categoryIndex);
mHeadlinesFragment.loadCategory(categoryIndex);
// If we are displaying the article on the right, we have to update that too
if (mIsDualPane) {
mArticleFragment.displayArticle(mCurrentCat.getArticle(0));
}
// If we are displaying a "category" button (on the ActionBar-less UI), we have to update
// its text to reflect the current category.
Button catButton = (Button) findViewById(R.id.categorybutton);
if (catButton != null) {
catButton.setText(CATEGORIES[mCatIndex]);
}
}
/** Called when a headline is selected.
*
* This is called by the HeadlinesFragment (via its listener interface) to notify us that a
* headline was selected in the Action Bar. The way we react depends on whether we are in
* single or dual-pane mode. In single-pane mode, we launch a new activity to display the
* selected article; in dual-pane mode we simply display it on the article fragment.
*
* @param index
没有合适的资源?快使用搜索试试~ 我知道了~
NewsReader Android的屏幕适配方案
共55个文件
png:23个
xml:18个
java:10个
需积分: 9 10 下载量 183 浏览量
2017-03-11
12:01:57
上传
评论
收藏 327KB ZIP 举报
温馨提示
Android 提供的多屏幕适配方案(能适配平板),可能有点旧,但对新手学习还是不错哦的项目
资源推荐
资源详情
资源评论
收起资源包目录
NewsReader.zip (55个子文件)
newsreader
res
drawable-ldpi
icon.png 2KB
tab_bg_selected.9.png 377B
button_pressed.9.png 2KB
logo.png 7KB
tab_bg_normal.9.png 137B
values-v11
layouts.xml 857B
style.xml 1KB
values-sw600dp-port
layouts.xml 857B
values-xlarge-port
layouts.xml 864B
values-sw600dp-land
layouts.xml 857B
drawable-hdpi
button_normal.9.png 5KB
icon.png 6KB
tab_bg_selected.9.png 598B
button_pressed.9.png 7KB
logo.png 16KB
tab_bg_normal.9.png 246B
drawable-xhdpi
button_normal.9.png 7KB
icon.png 5KB
tab_bg_selected.9.png 389B
button_pressed.9.png 7KB
logo.png 13KB
tab_bg_normal.9.png 429B
drawable
button_bg.xml 1KB
tab_bg.xml 906B
values
layouts.xml 866B
strings.xml 808B
style.xml 1000B
drawable-mdpi
button_normal.9.png 2KB
icon.png 2KB
tab_bg_selected.9.png 250B
button_pressed.9.png 3KB
logo.png 9KB
tab_bg_normal.9.png 168B
layout
headline_item.xml 962B
twopanes_narrow.xml 1KB
onepane_with_bar.xml 2KB
twopanes.xml 1KB
onepane.xml 1KB
actionbar_list_item.xml 961B
values-xlarge-land
layouts.xml 857B
proguard.cfg 1KB
src
com
example
android
newsreader
NewsCategory.java 2KB
ArticleActivity.java 3KB
ArticleFragment.java 2KB
HeadlinesFragment.java 4KB
NonsenseGenerator.java 7KB
NewsSource.java 2KB
CompatActionBarNavHandler.java 3KB
NewsArticle.java 2KB
CompatActionBarNavListener.java 1KB
NewsReaderActivity.java 10KB
project.properties 361B
AndroidManifest.xml 2KB
_index.html 605B
libs
android-support-v4.jar 219KB
共 55 条
- 1
资源评论
默尛铭
- 粉丝: 46
- 资源: 19
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- tesseract安装包
- 1_32陀螺仪舵机.zip
- HITJ0302MP-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- XILINXFPGA源码PCIExpress标准概述
- HITJ0204MP-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- XILINXFPGA源码PCIExpressIP核应用参考设计
- XILINXFPGA源码LCD12864在Spartan-3E实现代码
- XILINXFPGA源码LCDIPCORE
- G6402-VB一款SOT23封装P-Channel场效应MOS管
- XILINXFPGA源码IPcamera的开源系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功