fragement 完成 简易版本的新闻页面
显示效果
手机页面显示流程(单页面,显示新闻标题,点击跳转到内容)
主Activity对应的XML文件在 layout文件夹下面。
平板页面显示流程 (双页面,同时显示新闻标题以及内容)
主Activity对应的XML文件在layout-sw600dp文件夹下面
整体流程
- 首先加载主Activity,与之对应的XML文件里面加入静态fragement片段,用来显示新闻的标题。
单页面:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/news_title_fragment"
android:name="com.example.testcomplexfragment.fragment.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
双页面:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/news_title_fragment"
android:name="com.example.testcomplexfragment.fragment.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="3">
<fragment
android:id="@+id/news_content_fragment"
android:name="com.example.testcomplexfragment.fragment.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
2.编写NewsTitleFragement ,继承Fragement(暂时先不写)。编写该NewsTitleFragement对应的XML文件。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
- NewsContentFragement 编写 以及 对应XML文件。
package com.example.testcomplexfragment.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.testcomplexfragment.R;
public class NewsContentFragment extends Fragment {
private View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable 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.news_content_layout);
visibilityLayout.setVisibility(View.VISIBLE);
TextView newsTitleText = view.findViewById(R.id.news_title);
TextView newsContentText = view.findViewById(R.id.news_content);
newsTitleText.setText(newsTitle); // 刷新 新闻 的标题
newsContentText.setText(newsContent); // 刷新 新闻的内容
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/news_content_layout"
android:visibility="invisible"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<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"/>
<androidx.viewpager2.widget.ViewPager2
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>
4.在NewsTitleFragement 中解析对应的XML文件,获取里面的recyclerView,为该recyclerView设置布局管理器以及添加适配器。根据当前activity里面组件id来判断是单页面还是双页面,如果 是 单页面 模式 就 直接传递数据然后 打开 NewsContentActivity(对应的XML文件里面绑定了静态的NewsContentFragement片段,用来显示内容,NewsContentFragement片段对应的XML文件是news_content_frag),如果 是 双页面 模式 更新 NewsContentFragement中的内容。
package com.example.testcomplexfragment.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.testcomplexfragment.NewsContentActivity;
import com.example.testcomplexfragment.R;
import com.example.testcomplexfragment.bean.News;
import java.util.ArrayList;
import java.util.List;
public class NewsTitleFragment extends Fragment {
private boolean isTwoPane;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_title_frag,container,false);
RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(linearLayoutManager);
NewsAdapter adapter = new NewsAdapter(getNews());
recyclerView.setAdapter(adapter);
return view;
}
private List<News> getNews() {
ArrayList<News> list = new ArrayList<>();
for (int i = 0; i <= 50; i++) {
News news = new News();
news.setTitle(" This is news title "+i);
news.setContent("This is news content"+i+". ");
list.add(news);
}
return list;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (getActivity().findViewById(R.id.news_content) != null){
isTwoPane = true; // 是 双页面 模式
}else {
isTwoPane = false; // 单页面 模式
}
}
class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{
private List<News> mNewsList;
public NewsAdapter(List<News> mNewsList) {
this.mNewsList = mNewsList;
}
class ViewHolder extends RecyclerView.ViewHolder{
TextView newsTitleText;
public ViewHolder(@NonNull View itemView) {
super(itemView);
newsTitleText = itemView.findViewById(R.id.news_title);
}
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item,parent,false);
final RecyclerView.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 =(NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);
newsContentFragment.refresh(news.getTitle(),news.getContent());
}else{
// 如果 是 单页面 模式 就 直接 打开 NewsContentActivity
NewsContentActivity.actionStart(getActivity(),news.getTitle(),news.getContent());
}
}
});
return (ViewHolder) holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
News news = mNewsList.get(position);
holder.newsTitleText.setText(news.getTitle());
}
@Override
public int getItemCount() {
return mNewsList.size();
}
}
}
NewsContentActivity.java文件
package com.example.testcomplexfragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import com.example.testcomplexfragment.fragment.NewsContentFragment;
public class NewsContentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_content);
String newsTitle=getIntent().getStringExtra("news_title");//获取传入的新闻标题
String newsContent=getIntent().getStringExtra("news_content");//获取传入的新闻内容
//在活动中调用碎片的方法,能够在活动中得到相应碎片的实例,然后就可以调用碎片里面的方法
//这里是获得了碎片单页模式碎片的实例
//getSupportFragmentManager()所得到的是所在fragment 的父容器的管理器
NewsContentFragment newsContentFragment=(NewsContentFragment)
getSupportFragmentManager().findFragmentById(R.id.news_content_fragment);
//刷新NewsContentFragment界面
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);
}
}
News.java文件:
package com.example.testcomplexfragment.bean;
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;
}
}
news_content.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/news_content_fragment"
android:name="com.example.testcomplexfragment.fragment.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
news_item.xml文件:
<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:singleLine="true"
android:ellipsize="end"
android:textSize="18sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"/>
MainActivity.java文件:
package com.example.testcomplexfragment;
import androidx.appcompat.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);
}
}