适配设备的简易新闻浏览器
同时兼容手机和平板。
进入应用后先显示新闻列表,当在手机上使用时,使用单页模式,单击列表项会打开新的页面。
当在平板上使用时,使用双页模式,单击左侧列表项时直接更新右侧新闻内容页。
MainActivity.java
package com.example.newsdemos;import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}
}
NewsContentActivity.java
package com.example.newsdemos;import android.content.Context;
import android.content.Intent;
import android.os.Bundle;import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;public class NewsContentActivity extends AppCompatActivity {public static void actionStart(Context context,String title,String content){Intent intent = new Intent(context,NewsContentActivity.class);intent.putExtra("news_title",title);intent.putExtra("news_content",content);context.startActivity(intent);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_news_content);Intent intent = getIntent();String newsTitle = intent.getStringExtra("news_title");String newsContent = intent.getStringExtra("news_content");FragmentManager fm = getSupportFragmentManager();NewsContentFragment fragment =(NewsContentFragment) fm.findFragmentById(R.id.news_content_fragment);fragment.refresh(newsTitle,newsContent);}
}
NewsContentFragment.java
package com.example.newsdemos;import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;import androidx.fragment.app.Fragment;/*** A simple {@link Fragment} subclass.*/
public class NewsContentFragment extends Fragment {private View fragment_view;public NewsContentFragment() {// Required empty public constructor}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentfragment_view = inflater.inflate(R.layout.fragment_news_content, container, false);return fragment_view;}public void refresh(String newsTitle,String newsContent){View visibilityLayout = fragment_view.findViewById(R.id.visibility_layout);visibilityLayout.setVisibility(View.VISIBLE);TextView titleView = (TextView)fragment_view.findViewById(R.id.news_title);TextView contentView = (TextView)fragment_view.findViewById(R.id.news_content);titleView.setText(newsTitle);contentView.setText(newsContent);}}
NewsTitleFragment.java
package com.example.newsdemos;import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;import java.util.ArrayList;
import java.util.List;
import java.util.Random;/*** A simple {@link Fragment} subclass.*/
public class NewsTitleFragment extends Fragment {private List<News> newsList;private Boolean isTwoPane;public NewsTitleFragment() {// Required empty public constructor}@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentView view = inflater.inflate(R.layout.fragment_news_title, container, false);//view.RecyclerView组件RecyclerView newsTitleRecyclerView =(RecyclerView) view.findViewById(R.id.news_title_recycler_view);//准备子项布局news_item.xml//准备新闻数据newsList = getNews();//准备AdapterLinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());newsTitleRecyclerView.setLayoutManager(layoutManager);NewsAdapter adapter = new NewsAdapter(newsList);newsTitleRecyclerView.setAdapter(adapter);return view;}@Nullable@Overridepublic void onActivityCreated(@Nullable Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);if(getActivity().findViewById(R.id.news_content_layout)!=null){isTwoPane = true;}else {isTwoPane = false;}}private List<News> getNews(){List<News> newsList = new ArrayList<>();for(int i = 1 ;i<=20;i++){News news = new News();news.setTitle("This is news title" +i);news.setContent(getRandomLengthContent("This is news title" + i + "."));newsList.add(news);}return newsList;}private String getRandomLengthContent(String s){Random random = new Random();int length = random.nextInt(20)+1;StringBuilder builder = new StringBuilder();for(int i = 1;i<=length;i++){builder.append(s);}return builder.toString();}class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{private List<News> mnewsList;class ViewHolder extends RecyclerView.ViewHolder{TextView titleView;public ViewHolder(@NonNull View itemView) {super(itemView);titleView = (TextView)itemView.findViewById(R.id.news_title);}}public NewsAdapter(List<News> newsList) {mnewsList = newsList;}//创建子项布局的视图,itemView@NonNull@Overridepublic ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {LayoutInflater mInflater = LayoutInflater.from(parent.getContext());View itemView = mInflater.inflate(R.layout.news_title_item,parent,false);final ViewHolder holder = new ViewHolder(itemView);itemView.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {News news = mnewsList.get(holder.getAdapterPosition());if(isTwoPane)//双页模式{FragmentManager fm = getFragmentManager();NewsContentFragment fragment = (NewsContentFragment) fm.findFragmentById(R.id.news_content_fragment);fragment.refresh(news.getTitle(),news.getContent());}else {// News news = mnewsList.get(holder.getAdapterPosition());NewsContentActivity.actionStart(getActivity(),news.getTitle() ,news.getContent());}}});return holder;}@Overridepublic void onBindViewHolder(@NonNull ViewHolder holder, int position) {News news = mnewsList.get(position);holder.titleView.setText(news.getTitle());}@Overridepublic int getItemCount() {return mnewsList.size();}}}
News.java
package com.example.newsdemos;public class News {private String title;private String content;public String getTitle() {return title;}public String getContent() {return content;}public void setTitle(String title) {this.title = title;}public void setContent(String content) {this.content = content;}
}
activity_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"tools:context=".MainActivity"><fragmentandroid:id="@+id/news_title_fragment"android:name="com.example.newsdemos.NewsTitleFragment"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>
activity_main.xml(land)
<?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"tools:context=".MainActivity"><fragmentandroid:id="@+id/news_title_fragment"android:name="com.example.newsdemos.NewsTitleFragment"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1" /><FrameLayoutandroid:id="@+id/news_content_layout"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="2"><fragmentandroid:id="@+id/news_content_fragment"android:name = "com.example.newsdemos.NewsContentFragment"android:layout_width="match_parent"android:layout_height="match_parent"/></FrameLayout></LinearLayout>
activity_news_content.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"android:orientation="vertical"tools:context=".NewsContentActivity"><fragmentandroid:id="@+id/news_content_fragment"android:name = "com.example.newsdemos.NewsContentFragment"android:layout_width="match_parent"android:layout_height="match_parent"/></LinearLayout>
fragment_news_content.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"tools:context=".NewsContentFragment"><!-- TODO: Update blank fragment layout --><LinearLayoutandroid:id="@+id/visibility_layout"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:visibility="invisible"><TextViewandroid:id="@+id/news_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:padding="10dp"android:textSize="20sp" /><Viewandroid:id="@+id/view"android:layout_width="match_parent"android:layout_height="1dp"android:background="#000" /><TextViewandroid:id="@+id/news_content"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:padding="15dp"android:textSize="14sp" /></LinearLayout><Viewandroid:layout_width="2dp"android:layout_height="match_parent"android:background="#f00"/></FrameLayout>
fragment_news_title.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"tools:context=".NewsTitleFragment"><!-- TODO: Update blank fragment layout --><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/news_title_recycler_view"android:layout_width="match_parent"android:layout_height="match_parent" />
</FrameLayout>
news_title_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/news_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:singleLine="true"android:ellipsize="end"android:textSize="18sp"android:paddingLeft="10dp"android:paddingRight="10dp"android:paddingTop="15dp"android:paddingBottom="15dp"></TextView>
相关文章:

this.$router.push、replace、go的区别
1.this.$router.push() 描述:跳转到不同的url,但这个方法会向history栈添加一个记录,点击后退会返回到上一个页面。 用法: 2.this.$router.replace() 描述:同样是跳转到指定的url,但是这个方法不会向histor…

jQuery 实现图片的特效1[原]
用jQuery实现图片的动画效果非常简单.以下演示 jQuery里面所用到的参数 HIDE SHOW FADEOUT FADEIN 的不同. 在线演示:单击演示 代码分析: //hide and show fadeout and fadein $("input:eq(0)").click(function(){ $("img").fadeOut(3000); }); …

【设计模式】 模式PK:策略模式VS状态模式
1、概述 行为类设计模式中,状态模式和策略模式是亲兄弟,两者非常相似,我们先看看两者的通用类图,把两者放在一起比较一下。 策略模式(左)和状态模式(右)的通用类图。 两个类图非常相…

vs2008与IIS 7.0使用在vista上时出现的问题及解决方法(Internet Explorer 无法显示该页面)(VS2008: IE Cannot Display Web Page)...
我的系统是Vista Ultimate SP1,先安装了vs2008 ,然后再安装了IIS7.0之后就出现了一系列的问题。 问题:通过vs2008启动程序调试时报错。错误提示为:Internet Explorer 无法显示该页面 解决方法: 首先是安装一些必要的附件程序。 1.打开控制面板…

云服务中IaaS、PaaS、SaaS的区别
越来越多的软件,开始采用云服务。 云服务只是一个统称,可以分成三大类。 IaaS:基础设施服务,Infrastructure-as-a-servicePaaS:平台服务,Platform-as-a-serviceSaaS:软件服务,Softwa…

Android项目框架综合实例
综合使用ViewPager、Fragment、RecycleView等,实现类似“网易新闻浏览器 ”的项目综合框架,要求实现: 底部导航,分别是“首页”,“视频”,“讲讲”,“我的”;底部导航不要求滑动翻页…

配置Windows Server 2003 的RADIUS Server的方法
配置Windows Server 2003 的RADIUS Server的方法1、安装Windows 2003操作系统;2、添加角色(须插网线);3、添加组件->网络服务、证书服务;4、管理工具->域安全策略->帐户策略->密码策略;&#x…

Y15BeTa蜂鸣器唱歌程序-演奏版
最优版,自由演奏你的音乐! 每天进步一点点! 2018-12-09最新版 #include<bits/stdc.h> #include<windows.h> using namespace std; int md[8]{0,262,294,330,349,392,440,494}, mz[8]{0,523,587,659,698,784,880,988}, mg[8]{0,10…

实验6 触发器的使用
实验6 触发器的使用 实验目的 掌握触发器的创建、修改和删除操作。 掌握触发器的触发执行。 掌握触发器与约束的不同。二、实验要求 1.创建触发器。 2.触发器执行触发器。 3.验证约束与触发器的不同作用期。 4.删除新创建的触发器。 三、实验内容 (一&#x…

神经网络二(Neural Network)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ __author__ wlc __mtime__ 2017/9/04 """ import numpy as np import randomclass Network(object):def __init__(self,sizes):#size神经元个数list[3,2,4]self.num_layers l…

要想成功 需要了解的东西
凭我工作的经历来看 在it界要想成功必须要做到以下几点。 1 基本的开发语言不一定精通,但是一定要熟练的使用。 2 对公的主营业务一定要熟悉,不但要熟悉,而且要烂熟于心。如果不能做到这一点,那么起码对自己负责的工作要做到烂熟…

合并下载的Solaris镜像为DVD文件的方法
有很多朋友想安装solaris10操作系统,但是没有系统盘或者在官方网站下载之后不会合成。经过多次试验之后现在把正确的方法写下,以方便大家的学习之用。1。先到官方网站下载最新的系统包,下载之后的软件包为:sol-10-u4-ga-x86-dvd-i…

oracle测试环境表空间清理
测试场景下,使用的oralce遇到表空间的占用超大,可以采用如下的方式进行空间的清理 首先使用sqlplus连接数据库sqlplus sys/passwordorcl as sysdba 之类进行数据库的连接没然后进行如下的操作 ##创建表空间对于自己的测试库和表等最好都建立自己的表空间…

Google Chrome(谷歌浏览器) 发布下载
Google Chrome 下载地址:http://www.google.com/chrome 刚刚装上,还没怎么用,说一下大概印象,整体非常简洁,只有两个菜单选项。访问上明显感觉很快,比 Firefox 快,也比 IE7快;对网页…

实验 5 数据的完整性管理
实验 5 数据的完整性管理 一、实验目的 掌握实体完整性的实现方法。掌握用户定义完整性的实现方法。掌握参照完整性的方法。二、实验内容 数据库的完整性设置。三、实验步骤 可视化界面的操作方法:实体完整性 将 student 表的“sno”字段设为主键:在表…

16-acrobat por 简单使用指南
用于pdf编辑,这里我主要讲下图片的切割和保存,以及合并: 切割选中区域双击 合并的话,在编辑界面选中对象,复制,在另一个pdf的编辑界面粘贴,并挪动位置: 转载于:https://www.cnblogs.…

可突破任意ARP防火墙,以限制流量为目标的简单网络管理软件
以下消息来自幻影论坛[Ph4nt0m]邮件组软件说明:可突破任意ARP防火墙,以限制流量为目标的简单网络管理软件。使用方法:1.在参数设置中选择好工作网卡;2.检查网关信息和本机信息是否正确,如果不正确,请手动输…

OpenCV 学习笔记03 boundingRect、minAreaRect、minEnclosingCircle、boxPoints、int0、circle、rectangle函数的用法...
函数中的代码是部分代码,详细代码在最后 1 cv2.boundingRect 作用:矩形边框(boundingRect),用于计算图像一系列点的外部矩形边界。 cv2.boundingRect(array) -> retval 参数: array - 灰度图像ÿ…

实验1 应用SQL Server进行数据定义和管理
实验1 应用SQL Server进行数据定义和管理 【实验目的】 1)熟悉SQL Server的配置和管理。 2)掌握数据库的定义和修改方法。 3)掌握表的定义和修改方法。 4)掌握使用SQL语句进行数据管理的方法。 【实验环境】 SQL Server 20…

谷歌Chrome浏览器发布
谷歌已提前启用了浏览器Google Chrome的官方网站gears.google.com/chrome/,今天该浏览器的Windows版本首发。在此以前,谷歌与微软之间的斗争更象是“冷战”,大多局限于谷歌开发小型的、基于网络的软件,与微软占主导地位的Word、Po…

【bzoj1853】[Scoi2010]幸运数字 容斥原理+搜索
题目描述 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是“幸运号码”!但是这种“幸运号码”…

Creating a LINQ Enabled ASP.NET Web application template using C#.[转]
原文地址:http://www.wwwcoder.com/Weblogs/tabid/283/EntryID/839/Default.aspx其他相关地址:Building and using a LINQ for SQL Class Library with ASP.NET 2.0 1. Install Visual Studio 2005 RTM. 2. Download and install "…

深入理解Java线程池:ThreadPoolExecutor
线程池介绍 在web开发中,服务器需要接受并处理请求,所以会为一个请求来分配一个线程来进行处理。如果每次请求都新创建一个线程的话实现起来非常简便,但是存在一个问题: 如果并发的请求数量非常多,但每个线程执行的时间…

[zt]petshop4.0 详解之八(PetShop表示层设计)
代码中,InsertUser()方法就是负责用户的创建,而在之前则需要判断创建的用户是否已经存在。InsertUser()方法的定义如下: privatestaticboolInsertUser(OracleTransaction transaction, intuserId, stringemail, stringpassword, intpassForma…

Install Java 8 Ubuntu
sudo add-apt-repository ppa:webupd8team/javasudo apt-get -y update sudo apt-get -y install oracle-java8-installer sudo vim /etc/environment Add this at the end of the file JAVA_HOME"/usr/lib/jvm/java-8-oracle" source /etc/environment转载于:https:…

实验2 使用T-SQL编写程序
实验2 使用T-SQL编写程序 【实验目的】 1)掌握常用函数的使用方法。 2)掌握流程控制语句的使用方法。 【实验环境】 SQL Server 2012 Express(或SQL Server 2017 Express) 【实验重点及难点】 1&…

超酷flash光芒光线特效
http://thefwa.com/ 一个不错的英文设计展示站点 超酷flash光芒光线特效 http://www.zcool.com.cn/flash/light/page_1.html

实验3 数据库综合查询
实验3 数据库综合查询 一、实验目的 掌握SELECT语句的基本语法和查询条件表示方法;掌握查询条件种类和表示方法;掌握连接查询的表示及使用;掌握嵌套查询的表示及使用;了解集合查询的表示及使用。 二、实验环境 已安装SQL Serv…

Find Large Files in Linux
https://www.rosehosting.com/blog/find-large-files-linux/转载于:https://www.cnblogs.com/WCFGROUP/p/10328469.html

Linux统计行数命令wc(转)
Linux wc命令用于计算字数。 利用wc指令我们可以计算文件的Byte数、字数、或是列数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据。 语法 wc [-clw][--help][--version][文件...] 参数 -c或--bytes或--chars …