总结篇!
吭哧吭哧了三天,最近不断研究《第一行代码:第二版》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();
}
}
}