总结篇!
吭哧吭哧了三天,最近不断研究《第一行代码:第二版》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的用法
1 public class NewsTitleFragment extends Fragment { 2 3 private boolean isTwoPane; 4 5 public List<News> getNews() { 6 7 List<News> newsList = new ArrayList<>(); 8 9 for(int i=1;i<=50;i++) 10 { 11 News news = new News(); 12 news.setTitle("This is news title "+i); 13 news.setContent(getRandomLengthContent("This is news content "+ i)); 14 newsList.add(news); 15 } 16 return newsList; 17 } 18 19 private String getRandomLengthContent(String content) { 20 Random random = new Random(); 21 int length = random.nextInt(20) + 1; 22 StringBuilder builder = new StringBuilder(); 23 for(int i=0;i<length;i++) 24 { 25 builder.append(content+" "); 26 } 27 return builder.toString(); 28 } 29 30 //真正开始的代码 31 @Override 32 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 33 View view = inflater.inflate(R.layout.news_title_frag, container, false); 34 //加载RecycleView 35 RecyclerView newsTitleRecyclerView = (RecyclerView) view.findViewById(R.id.news_title_recycler_view); 36 newsTitleRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 37 newsTitleRecyclerView.setAdapter(new NewsAdapter(getNews())); 38 39 return view; 40 } 41 42 @Override 43 public void onActivityCreated(Bundle savedInstanceState) { 44 super.onActivityCreated(savedInstanceState); 45 if(getActivity().findViewById(R.id.news_content_layout) != null) 46 { 47 isTwoPane = true; 48 } 49 else 50 { 51 isTwoPane = false; 52 } 53 } 54 55 class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder> { 56 57 private List<News> mNewList; 58 59 //构造方法 60 public NewsAdapter(List<News> newsList) { 61 mNewList = newsList; 62 } 63 64 //自定义RecycleView的内容 65 class ViewHolder extends RecyclerView.ViewHolder { 66 //有一个Textview 67 TextView newsTitleText; 68 69 public ViewHolder(View view) { 70 super(view); 71 newsTitleText = (TextView) view.findViewById(R.id.news_title); 72 } 73 } 74 //实例化每一项 75 @Override 76 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 77 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false); 78 //把每一项view(TextView)实例化为一个ViewHolder 79 final ViewHolder holder = new ViewHolder(view); 80 //为每一个view绑定监听事件 81 view.setOnClickListener(new View.OnClickListener() { 82 @Override 83 public void onClick(View v) { 84 News news = mNewList.get(holder.getAdapterPosition()); 85 if(isTwoPane) 86 { 87 NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment); 88 newsContentFragment.refresh(news.getTitle(), news.getContent()); 89 } 90 else 91 { 92 NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent()); 93 } 94 } 95 }); 96 return holder; 97 } 98 //绑定每一个ViewHolder,因为这里有position参数。 99 @Override 100 public void onBindViewHolder(ViewHolder holder, int position) { 101 News news = mNewList.get(position); 102 holder.newsTitleText.setText(news.getTitle()); 103 } 104 //RecycleView项的数量 105 @Override 106 public int getItemCount() { 107 return mNewList.size(); 108 } 109 } 110 }