[2017-7-27]Android Learning Day5
总结篇!
吭哧吭哧了三天,最近不断研究《第一行代码:第二版》170多页的那个新闻实践项目,虽然也没有用到数据库和一些Web爬虫的知识,新闻数据都是随机生成的字符串。。。。。。
但还是很开心啊!!!!!!!!!!!!
成功的分离了大尺寸和小尺寸的设备的显示。
总而言之,练习了碎片,活动,最最基本的组件,还有RecycleView。
其中最让我头疼的就是活动、活动布局、碎片、碎片布局的联系了有没有!
哦对,还有更头疼的,就是那个RecycleView的用法,简直爆炸。话不多说,快记录一下,不然一会又忘了。。。。。。
一、活动、活动布局、碎片、碎片布局的联系
就是这个图!嘿嘿,自己总结的,很明白,转载请注明!
1.活动通过布局文件名调用布局
public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
2.3.布局文件上面注明碎片,且<碎片></碎片>引用碎片类。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/news_title_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"> <fragment
android:id="@+id/news_title_fragment"
android:name="com.liwenchi.fragmentbestpractice.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" /> </FrameLayout>
4.碎片类引用碎片布局文件
public class NewsContentFragment extends Fragment { private View view; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.news_content_frag, container, false);
return view;
}
}
恩,就是这样滴。
二、RecycleView的用法
public class NewsTitleFragment extends Fragment { private boolean isTwoPane; public List<News> getNews() { List<News> newsList = new ArrayList<>(); for(int i=1;i<=50;i++)
{
News news = new News();
news.setTitle("This is news title "+i);
news.setContent(getRandomLengthContent("This is 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+"\n");
}
return builder.toString();
} //真正开始的代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_title_frag, container, false);
//加载RecycleView
RecyclerView newsTitleRecyclerView = (RecyclerView) view.findViewById(R.id.news_title_recycler_view);
newsTitleRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
newsTitleRecyclerView.setAdapter(new NewsAdapter(getNews())); return view;
} @Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(getActivity().findViewById(R.id.news_content_layout) != null)
{
isTwoPane = true;
}
else
{
isTwoPane = false;
}
} class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> { private List<News> mNewList; //构造方法
public NewsAdapter(List<News> newsList) {
mNewList = newsList;
} //自定义RecycleView的内容
class ViewHolder extends RecyclerView.ViewHolder {
//有一个Textview
TextView newsTitleText; public ViewHolder(View view) {
super(view);
newsTitleText = (TextView) view.findViewById(R.id.news_title);
}
}
//实例化每一项
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
//把每一项view(TextView)实例化为一个ViewHolder
final ViewHolder holder = new ViewHolder(view);
//为每一个view绑定监听事件
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
News news = mNewList.get(holder.getAdapterPosition());
if(isTwoPane)
{
NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);
newsContentFragment.refresh(news.getTitle(), news.getContent());
}
else
{
NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent());
}
}
});
return holder;
}
//绑定每一个ViewHolder,因为这里有position参数。
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
News news = mNewList.get(position);
holder.newsTitleText.setText(news.getTitle());
}
//RecycleView项的数量
@Override
public int getItemCount() {
return mNewList.size();
}
}
}
[2017-7-27]Android Learning Day5的更多相关文章
- 团队作业4——第一次项目冲刺(Alpha版本)2017.4.27
2017.04.27 天气阴沉 小雨. 时间:上午 9:35 ---10:10分 地点:陆大314实验室 会议内容:每天充分利用好大课间的时间,今天对昨天的的细节问题进行了讨论及方法更正.时间不等人这 ...
- Loj #6073.「2017 山东一轮集训 Day5」距离
Loj #6073.「2017 山东一轮集训 Day5」距离 Description 给定一棵 \(n\) 个点的边带权的树,以及一个排列$ p\(,有\)q $个询问,给定点 \(u, v, k\) ...
- 「2017 山东一轮集训 Day5」苹果树
「2017 山东一轮集训 Day5」苹果树 \(n\leq 40\) 折半搜索+矩阵树定理. 没有想到折半搜索. 首先我们先枚举\(k\)个好点,我们让它们一定没有用的.要满足这个条件就要使它只能和坏 ...
- Android开发环境的配置2017.05.27
关于配置Android开发环境请参考此链接:http://blog.chinaunix.net/uid-25434387-id-461933.html
- Android Learning:数据存储方案归纳与总结
前言 最近在学习<第一行android代码>和<疯狂android讲义>,我的感触是Android应用的本质其实就是数据的处理,包括数据的接收,存储,处理以及显示,我想针对这几 ...
- [2017-8-02]Android Learning Day8
自定义动画效果 新建一个customAnim类 package com.liwenchi.myapplication; import android.view.animation.Animation; ...
- [2017-7-26]Android Learning Day4
RecycleView 恩,学习Fragment的过程中的一个小实践居然用到了RecycleView!坑了我好久有木有!!好气哦,从昨晚到现在.(现在也还是一头雾水,不过照搬也会用了) 这是第一版的代 ...
- Android Learning:微信第三方登录
这两天,解决了微信第三方授权登录的问题,作为一个新手,想想也是一把辛酸泪.我想着,就把我的遇到的坑给大家分享一下,避免新手遇到我这样的问题能够顺利避开. 步骤一 微信开发者平台 我开始的解决思路是,去 ...
- Android Learning:多线程与异步消息处理机制
在最近学习Android项目源码的过程中,遇到了很多多线程以及异步消息处理的机制.由于之前对这块的知识只是浅尝辄止,并没有系统的理解.但是工程中反复出现让我意识到这个知识的重要性.所以我整理出这篇博客 ...
随机推荐
- rest-framework序列化
快速实例 Quickstart 序列化 开篇介绍: ---- 一切皆是资源,操作只是请求方式 ----book表增删改查 /books/ books /books/add/ addbook /book ...
- semantic-ui 容器与栅格
semantic中可以指定one-sixteen这16个单词来指定网格column所占的长度.也就是说,在网页中,一行最多只有16个column,超过16个之后,自动移到下一行. 栅格可以使用i,di ...
- PAT L2-007 家庭房产
https://pintia.cn/problem-sets/994805046380707840/problems/994805068539215872 给定每个人的家庭成员和其自己名下的房产,请你 ...
- 分布式文件系统FastDFS
fastdfs_百度百科https://baike.baidu.com/item/fastdfs/5609710 用FastDFS一步步搭建文件管理系统 - bojiangzhou - 博客园http ...
- 【Python3练习题 017】 两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比。请编程序找出三队赛手的名单。
import itertools for i in itertools.permutations('xyz'): if i[0] != 'x' and i[2] != 'x' and i[ ...
- vue嵌套路由
父组件 (注:to="/Flow/moban_a"这里不是文件加路径,是父组件路由+子组件路由) 路由配置
- IIS下载地址
https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=1038
- 校园电商项目(1) 基于SSM
第一步:搭好环境 我这里使用Eclipse做本次的项目,tomcat.maven啥的怎么弄就跳过了ヾ(o・ω・)ノ 第二步:创建工程 我们首先创建一个maven项目,选择最后一个,创建完之后发现报错, ...
- Appscanner实验还原code3
# Author: Baozi #-*- codeing:utf-8 -*- import _pickle as pickle from sklearn import ensemble import ...
- python学习笔记(6)--条件分支语句
if xxxx: coding if xxxx: coding else: coding if xxxx: coding elif xxx: coding …… else: coding 或者一种简洁 ...