Android开发(1) | Fragment 的应用——新闻应用
文章目录
- Item:标题子项
- 布局文件
- Java代码
- 标题碎片
- 布局文件
- Java代码
- 新闻内容碎片
- 布局文件
- Java代码
- 新闻内容活动
- 布局文件
- Java代码
- 首界面
- 布局文件
- Java代码
Item:标题子项
布局文件
news_item.xml
:
<TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/news_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:lines="1"android:ellipsize="end"android:textSize="18sp"android:paddingLeft="10dp"android:paddingRight="10dp"android:paddingTop="15dp"android:paddingBottom="15dp"></TextView>
- padding:给控件周围加上补白,避免文本内容紧靠边缘
- lines:规定文本内容只能单行显示
- ellipsize:设定当文本内容超出控件宽度时,文本的略缩方式
Java代码
public class News {private String title; // 标题private String content; // 内容public void setTitle(String title) {this.title = title;}public String getTitle() {return title;}public void setContent(String content) {this.content = content;}public String getContent() {return content;}
}
标题碎片
布局文件
news_title_frag.xml
:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/news_title_recycler_view"android:layout_width="match_parent"android:layout_height="match_parent"/>
</LinearLayout>
Java代码
public class NewsTitleFragment extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View view = inflater.inflate(R.layout.news_title_frag, container, false);// 滚动控件实例RecyclerView newsTitleRecyclerView = view.findViewById(R.id.news_title_recycler_view);// 布局方式LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());// 设置RecyclerView布局方式为线性布局newsTitleRecyclerView.setLayoutManager(layoutManager);// 为布局添加适配器newsTitleRecyclerView.setAdapter(new NewsAdapter(getNews()));return view;}// 创建一个List<News>并返回private List<News> getNews(){List<News> newsList = new ArrayList<>();for(int i=1; i<=50; i++){News news = new News();news.setTitle("News Title" + i);news.setContent(getRandomLengthContent("News Content"+i+"."));newsList.add(news);}return newsList;}// 随机生成新闻内容private String getRandomLengthContent(String content){Random random = new Random();int length = random.nextInt(20)+1;StringBuilder builder = new StringBuilder();for(int i=0; i<length; i++){builder.append(content);}return builder.toString();}// 以内部类形式实现自定义适配器class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{private List<News> NewsList;class ViewHolder extends RecyclerView.ViewHolder {TextView newsTitleText;public ViewHolder(View itemView) {super(itemView);newsTitleText = itemView.findViewById(R.id.news_title);}}public NewsAdapter(List<News> newsList){NewsList = newsList;}@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);final ViewHolder holder = new ViewHolder(view);// 点击RecyclerView中的新闻标题时,启动NewsContentActiviryview.setOnClickListener((View v)->{// 获取点击项的News实例News news = NewsList.get(holder.getAdapterPosition());/* 单页模式 */// 启动新的活动显示新闻内容NewsContentActiviry.actionStart(getActivity(), news.getTitle(), news.getContent());/* 双页模式 *//*NewsContentFragment newsContentFragment = (NewsContentFragment) getParentFragmentManager().findFragmentById(R.id.news_content_fragment);newsContentFragment.refresh(news.getTitle(), news.getContent());*/});return holder;}@Overridepublic void onBindViewHolder(ViewHolder holder, int position) {News news = NewsList.get(position);holder.newsTitleText.setText(news.getTitle());}@Overridepublic int getItemCount() {return NewsList.size();}}
}
新闻内容碎片
布局文件
news_content_frag.xml
:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/visible_layout"android:orientation="vertical"android:visibility="invisible"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/news_title"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:textSize="20sp"android:padding="10dp"/><Viewandroid: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:textSize="18sp"android:padding="15dp"/></LinearLayout><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:layout_alignParentLeft="true"android:background="@color/black" /></RelativeLayout>
- 内嵌的 LinearLayout 布局用于管理新闻内容碎片的布局。
- 两个 TextView 分别表示新闻标题与新闻内容。
- 两个 View 是两条黑色分割线,第一个用于在新闻内容碎片中分割新闻标题与新闻内容;第二个用于在双页模式下分割新闻内容碎片和标题碎片。
- 外层使用 RelativeLayout 布局是为了便于布置第二个 View。(
android:layout_alignParentLeft
属性)
单页模式:
双页模式:
PS:竖直黑色分割线左侧属于标题碎片,跟新闻内容碎片无关。
Java代码
public class NewsContentFragment extends Fragment {private View view;// 加载布局@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.news_content_frag, container, false);return view;}// 将新闻标题和内容显示到界面上public void refresh(String newTitle, String newContent){View view1 = view.findViewById(R.id.visible_layout);view1.setVisibility(View.VISIBLE);TextView title = view.findViewById(R.id.news_title);TextView content = view.findViewById(R.id.news_content);title.setText(newTitle); // 刷新新闻标题content.setText(newContent); // 刷新新闻内容}
}
新闻内容活动
布局文件
在单页模式下,点击标题碎片中的标题子项时,会根据点击的新闻标题跳转到具体的新闻内容活动,
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/news_content_fragment"android:layout_width="match_parent"android:layout_height="match_parent"android:name="com.example.activitytest.Fragment.NewsContentFragment"/></LinearLayout>
Java代码
public class NewsContentActiviry extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView((R.layout.news_content));String newsTitle = getIntent().getStringExtra("news_title");String newsContent = getIntent().getStringExtra("news_content");NewsContentFragment newsContentFragment = (NewsContentFragment) getSupportFragmentManager().findFragmentById(R.id.news_content_fragment);newsContentFragment.refresh(newsTitle, newsContent); // 刷新NewsContentFragment界面}public static void actionStart(Context context, String newsTitle, String newsContent){Intent intent = new Intent(context, NewsContentActiviry.class);intent.putExtra("news_title", newsTitle);intent.putExtra("news_content", newsContent);context.startActivity(intent);}
}
有关 actionStart 详见本文
首界面
布局文件
news_layout.xml
:注释部分解除之后即从单页布局变为双页布局
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"><fragmentandroid:id="@+id/news_title_fragment"android:name="com.example.activitytest.Fragment.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="3"><fragmentandroid:id="@+id/news_content_fragment"android:name="com.example.activitytest.Fragment.NewsContentFragment"android:layout_width="match_parent"android:layout_height="match_parent"/></FrameLayout>-->
</LinearLayout>
单页布局:
双页布局:
Java代码
public class NewsActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.news_layout);}
}
Android开发(1) | Fragment 的应用——新闻应用相关推荐
- android实现新闻内容显示功能,Android开发实现自定义新闻加载页面功能实例
本文实例讲述了Android开发实现自定义新闻加载页面功能.分享给大家供大家参考,具体如下: 一.概述: 1.效果演示: 2.说明:在新闻页面刚加载的时候,一般会出现五种状态 未知状态(STATE_U ...
- android viewpager fragment传值,Android开发中如何解决Fragment +Viewpager滑动页面重复加载的问题...
前言 之前在做一个Viewpager上面加载多个Fragment时总会实例化已经创建好的Fragmnet对象类似 viewPager.setAdapter(new FragmentPagerAdapt ...
- Android开发实例-Android平台手机新闻客户端
Android开发实例<Android平台手机新闻客户端>是基于Android4.0及以上平台的一款新闻类手机应用,应用运行效果如下所示: Android开发实例课程主要介绍运行于Andr ...
- Android开发之漫漫长途 XII——Fragment 详解
作者:忘了12138 地址:http://www.cnblogs.com/wangle12138/p/8351907.html 声明:本文是 忘了12138 原创投稿,转发请联系原作者授权 该文章是一 ...
- Android开发之漫漫长途 XII——Fragment详解
该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...
- Android开发帮助技巧(适用于入门)(第一部分-高效地构建项目的准备工作和Activity与Fragment的交互介绍)
平台: windows11 Android Studio 4.2.2 Build #AI-202.7660.26.42.7486908, built on June 24, 2021 Runtime ...
- Android开发-Fragment嵌套Fragment
Android开发-Fragment嵌套Fragment 前言 使用依赖 远程仓库地址 布局实现 使用控件 xml代码 Java实现 效果图 项目地址 前言 在大多数公司中,他们会尽量少写Activi ...
- Android开发——项目实例(五)集新闻、音乐、电影于一体的软件(带打包源码)
主页面 1.写界面 很明显,这个主界面采用了ViewPager和TabLayout实现界面滑动切换,在使用TabLayout之前记得导包,TabLayout需要导入的包. <?xml versi ...
- Android开发最近新闻和功能都在这里了
在Android开发领域,12月是相对安静的月份,Android Developers博客上的帖子数量比往常少很多.根据道客优从外媒看的说法是许多其他网站在假期期间会休息,开发人员也需要休息! 尽管如 ...
- Android开发笔记(一百二十)两种侧滑布局
SlidingPaneLayout SlidingPaneLayout是Android在android-support-v4.jar中推出的一个可滑动面板的布局,在前面< Android开发笔记 ...
最新文章
- 深入理解C系列:不同类型变量的变量名和内存间的关系
- 《Java程序员,上班那点事儿》书名的由来
- iOS中创建动态库及调用方法
- 程序员如何保护好自己的发际线
- 系统分析师-论文准备
- SAN存储交换机配置
- ttest求pvalue_TTEST 在EXCEL计算出的结果是t还是p值
- 特斯拉是如何训练自动驾驶的?
- 团队的Kick off
- MacOs 查看本地IP和Mac地址
- 【转摘】图像思维与意识观念的统一——读毕建勋《子非鱼》有感(初稿)[1]
- 给定一系列正整数,请设计一个尽可能高效的算法,查找倒数第K个位置上的数字
- MATLAB的基本用法
- Oracle异常自我总结
- 【翻译】使用Sencha Ext JS创建美丽的图画(1)
- 802.11--802.11ac协议
- CUDA9.1、cuDNN7在Ubuntu16.04上的安装
- java应用 cpu占用过高问题分析及解决方法
- 银行卡识别-自动识别银行卡号
- 【学习笔记】CSW网络目录服务
热门文章
- python数据可视化工具 pandas_Pandas数据可视化工具——Seaborn用法整理(下)
- Tecplot如何保存frame style 并调用frame style
- 在飞音G801上运行OpenWRT+Asterisk
- bitnamigitlab_Bitnami Gitlab 修改端口
- uln2003驱动蜂鸣器_让蜂鸣器发声
- ArcGIS如何在一个矢量上用不同颜色进行标注
- 【转】ABP源码分析十五:ABP中的实用扩展方法
- 【转】.NET Remoting
- 【转】SOAR平台初探(一)
- LINQ TO SQL和Entity Framework 的关系 你了解多少?