碎片的最佳实践 《第一行代码Android》
从14:00到20:00一直在做这个实践,毕竟自己也是刚刚开始学习Android 开发,实践过程中也是一直遇到各种各样的问题。下图是最终该一个结果,因为我用的是真机,因此只用手机测试了一下。
最终的项目结构就是这样。总的说这个的思路并不是很难,但是由于它是一部分一部分教的,因此需要按照步骤一步步进行,不行写错地方,或者少写了一些函数,或者是直接粘贴复制。说说自己遇到的问题吧:
1.一开始写的项目有很多错误,于是我重新创建了一个项目,原本图省事,没想到粘贴复制过来的代码系统不识别,很多关键词直接报错,只好重新写那些关键词并导入对应的包
2.一定要按照书本上的原原本本的进行操作!!!否则你会非常痛苦,除非你思路很清晰。我和书本上xml文件的命名方法有几个不同,导致最后在引用对应的xml中的id的时候,需要思考很长时间....这段代码到底要实现什么功能...然后去找xml文件中的进行对应...累
3.一个很容易遇到的问题是Fragment的问题!!!!就像书上说的那样,可能会有两个不同包下的Fragment供你选择,一个是系统内置的android.app.Fragment,另一个是support-v4库中的android.support.v4.app.Fragment.书本强烈建议使用后者,因为它可以让碎片在所有Android系统版本中保持功能一致性。也就是说对于这个问题的处理方法可以是以下所示的(仅供参考)
将import android.app.Fragment; 改为import android.support.v4.app.Fragment;以及将getSupportFragmentManager()改为getFragmentManager()如下图所示
如果你在建class的时候没有extends Fragment可会出现这个问题...直接加上就好
4.因为这本书是按照步骤一步步教我们实践的,因此很可能做到后面我们会把代码写错地方,这时候它会提示“...method cannot applied to...”诸如此类,此时一定要有耐心,重新看着书本整理一下代码出现的顺序.
5.超级不喜欢Android studio 中的提示,我总以为它的提示是正确的,but...最后很多书写错误都是因为它“善意的”提示。
不管怎么说,祝大家好运,一定要相信:是你的代码写错了而不是书本有问题!!!!
//MainActivity
package com.example.hui.fragmentbestpractice;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
//News
package com.example.hui.fragmentbestpractice; /** * Created by hui on 2018/1/24. */ public class News { private String title; private String content; public String getTitle(){ return title; } public void setTitle(String title){ this.title = title; } public String getContent(){ return content; } public void setContent(String content){ this.content = content;
}}
//NewsContentActivity
package com.example.hui.fragmentbestpractice; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class NewsContentActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_news_content); String newsTitle = getIntent().getStringExtra("news_title"); String newsContent = getIntent().getStringExtra("activity_news_content"); NewsContentFragment newsContentFragment= (NewsContentFragment)getSupportFragmentManager().findFragmentById(R.id.news_content_fragment); newsContentFragment.refresh(newsTitle,newsContent); } public static void actionStart(Context context, String newsTitle, String newsContent){ Intent intent = new Intent(context,NewsContentActivity.class); intent.putExtra("news_title",newsTitle); intent.putExtra("news_content",newsContent); context.startActivity(intent); } }//NewsContentFragment
package com.example.hui.fragmentbestpractice;
import android.support.v4.app.Fragment;
//import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by hui on 2018/1/24.
*/
public class NewsContentFragment extends Fragment {
private View view;
public View onCreatView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
view = inflater.inflate(R.layout.news_content_frag,container,false);
return view;
}
public void refresh(String newsTitle,String newsContent){
View visibilityLayout = view.findViewById(R.id.visibility_layout);
visibilityLayout.setVisibility(View.VISIBLE);
TextView newsTitleText = (TextView)view.findViewById(R.id.news_title);
TextView newsContentText = (TextView)view.findViewById(R.id.news_content);
newsTitleText.setText(newsTitle);
newsContentText.setText(newsContent);
}
}
//NewsTitleFragment
package com.example.hui.fragmentbestpractice; import android.support.v4.app.Fragment; import android.content.Context; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.app.Activity; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import java.util.Random; import static android.content.ContentValues.TAG; /** * Created by hui on 2018/1/24. */ public class NewsTitleFragment extends Fragment{ private boolean isTwoPane; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ View view = inflater.inflate(R.layout.news_title_frag,container,false); RecyclerView newsTitleRecyclerView = (RecyclerView)view.findViewById(R.id.news_title_recycle_view); LinearLayoutManager LayoutManger = new LinearLayoutManager(getActivity()); newsTitleRecyclerView.setLayoutManager(LayoutManger); NewsAdapter adapter = new NewsAdapter(getNews()); newsTitleRecyclerView.setAdapter(adapter); return view; } 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> mNewsList; class ViewHolder extends RecyclerView.ViewHolder { TextView newsTitleText; public ViewHolder(View view) { super(view); newsTitleText = (TextView) view.findViewById(R.id.news_title); } } public NewsAdapter(List<News> newsList) { mNewsList = newsList; } public NewsAdapter.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); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { News news = mNewsList.get(holder.getAdapterPosition()); if (isTwoPane) { NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment); //NewsContentFragment newsContentFragment =(NewsContentFragment)getSupportFragmentManager().findFragmentById(R.id.news_content_fragment); newsContentFragment.refresh(news.getTitle(), news.getContent()); } else { NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent()); } } }); return holder; } @Override public void onBindViewHolder(NewsAdapter.ViewHolder holder, int position) { News news = mNewsList.get(position); holder.newsTitleText.setText(news.getTitle()); } @Override public int getItemCount() { return mNewsList.size(); } } private 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); } return builder.toString(); } }