Fragment应用实践-----简易新闻应用布局(可同时兼容手机与平板)
1、在app/build.gradle添加依赖库
compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
2、新建News新闻实体类
public class News {
private String title; //新闻标题
private String content; //新闻内容
public String getContent() {
return content;
}
public String getTitle() {
return title;
}
public void setContent(String content) {
this.content = content;
}
public void setTitle(String title) {
this.title = title;
}
}
3、新建news_content_frag.xml布局。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/visibility_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="invisible">
<TextView
android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:textSize="20sp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000"/>
<TextView
android:id="@+id/news_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="15dp"
android:textSize="18sp"/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#000"/>
</RelativeLayout>
新闻内容布局共分为两个部分,头部显示新闻标题,正文显示新闻内容,中间用一条细线分隔开。其中细线用高或宽为1dp的View实现。
4、新建NewContentFragment类,继承自Fragment。
public class NewContentFragment extends Fragment {
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//加载布局
view = inflater.inflate(R.layout.news_content_flag, container, false);
return view;
}
// refresh()方法用于将新闻标题与内容显示在界面上。
public void refresh(String newsTitle, String newsContent){
View visibilityLayout = view.findViewById(R.id.visibility_layout);
visibilityLayout.setVisibility(View.VISIBLE);
TextView newTitleText = view.findViewById(R.id.news_title);
TextView newContentText = view.findViewById(R.id.news_content);
newTitleText.setText(newsTitle);
newContentText.setText(newsContent);
}
}
5、新建一个活动NewsContentActivity,并将布局设定为news_content.xml。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//代码的复用性,直接在不居中引用NewContentFragment,相当于直接加载了news_content_flag布局。
<fragment
android:id="@+id/news_content_fragment"
android:name="com.launcher.fragmentbestpractice.NewContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
public class NewsContentActivity extends AppCompatActivity {
//更简洁,更整齐的打开另一Activity的编码方式
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);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_content);
//通过Intent传入新闻标题与新闻内容
String newsTitle = getIntent().getStringExtra("news_title");
String newsContent = getIntent().getStringExtra("news_content");
//从布局文件中获取Fragment实例
NewContentFragment newContentFragment = (NewContentFragment)
getSupportFragmentManager().findFragmentById(R.id.news_content_fragment);
//调用refresh()方法将新闻标题与内容显示在界面上。
newContentFragment.refresh(newsTitle,newsContent);
}
}
6、创建用于显示新闻列表的布局,新建news_title_flag.xml,加载RecyclerView布局。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/news_title_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
7、新建news_item.xml,作为RecyclerView的子项布局其中包含一个TextView。
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:ellipsize="end"
android:textSize="18sp"
android:paddingBottom="10dp"
android:paddingEnd="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
</TextView>
android:padding:表示给控件周围加上补白,以免文本内容紧靠边缘。
android:ellipsize:设定文本超出控件宽度时,文本的缩略方式。
8、新建NewsTitleFragment作为展示新闻列表的碎片。
public class NewsTitleFragment extends Fragment {
private boolean isTwoPage;
//加载news_title_flag布局
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_title_flag, container, false);
return view;
}
//通过判断能否找到id为news_content_layout的View来判断当前是双页模式还是单页模式。
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(getActivity().findViewById(R.id.news_content_layout)!=null){
isTwoPage = true;
}else {
isTwoPage = false;
}
}
}
9、修改activity_main.xml代码,单页模式。
<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.launcher.fragmentbestpractice.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
10、新建layout-sw600dp文件夹,再新建一个activity_main.xml布局,双页模式。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/news_title_fragment"
android:name="com.launcher.fragmentbestpractice.NewsTitleFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/news_content_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<fragment
android:id="@+id/news_content_fragment"
android:name="com.launcher.fragmentbestpractice.NewContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
11、在NewsTitleFragment中新建NewsAdapter内部类作为RecyclerView的适配器,将新闻列表通过RecyclerView展示出来。
内部类便于直接访问NewsTitleFragment类的实例。
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 = view.findViewById(R.id.news_title);
}
}
public NewsAdapter(List<News> mNewsList) {
this.mNewsList = mNewsList;
}
@Override
public 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 view) {
//点击项的News实例
News news = mNewsList.get(holder.getAdapterPosition());
//通过isTwoPage判断为单页还是双页模式
if(isTwoPage){
NewContentFragment newContentFragment = (NewContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);
newContentFragment.refresh(news.getTitle(),news.getContent());
}else{
NewsContentActivity.actionStart(getContext(),news.getTitle(),news.getContent());
}
}
});
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
News news = mNewsList.get(position);
holder.newsTitleText.setText(news.getTitle());
}
@Override
public int getItemCount() {
return mNewsList.size();
}
}
12、改写NewsTiltleFragment中的onCreateView()方法,使用RecyclerView,并向RecyclerView填充数据。
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_title_flag, container, false);
RecyclerView newTitleRecyclerView = view.findViewById(R.id.news_title_recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
newTitleRecyclerView.setLayoutManager(linearLayoutManager);
NewsAdapter adapter = new NewsAdapter(getNews());
newTitleRecyclerView.setAdapter(adapter);
return view;
}
private List<News> getNews() {
List<News> newsList = new ArrayList<>();
for(int i=1; i<=50; i++){
News news =new News();
news.setTitle("This is a new Title"+i);
news.setContent("This is a new Content"+i);
newsList.add(news);
}
return newsList;
}