界面
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/news_title_layout"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/news_title_fragment" android:name="com.example.ken.fragmentbestpractice.NewsTitleFragment"/> </FrameLayout>
2.平板模式下的activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:name="com.example.ken.fragmentbestpractice.NewsTitleFragment" android:id="@+id/news_title_fragment" /> <FrameLayout android:layout_width="0dp" android:layout_height="match_parent" android:id="@+id/news_content_layout" android:layout_weight="3"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/news_content_fragment" android:name="com.example.ken.fragmentbestpractice.NewsContentFragment"/> </FrameLayout> </LinearLayout>
3.news_item.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/news_title" android:singleLine="true" android:ellipsize="end" android:textSize="18sp" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="15dp" android:paddingBottom="15dp" />
4.news_title_frag.xml
<?xml version="1.0" encoding="utf-8"?> <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:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/news_title_recycler_view"/> </LinearLayout>
5.news_content.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/news_content_fragment" android:name="com.example.ken.fragmentbestpractice.NewsContentFragment"/> </LinearLayout>
6.news_content_frag.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/visibility_layout" android:orientation="vertical" android:visibility="invisible"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/news_title" android:gravity="center" android:padding="10dp" android:textSize="20sp"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="#000" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/news_content" 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>
JAVA
1.MainActivity.java
package com.example.ken.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); } }
2.News.java
package com.example.ken.fragmentbestpractice; public class News { private String title; private String content; public String getTitle() { return title; } public String getContent() { return content; } public void setTitle(String title){ this.title = title; } public void setContent(String content) { this.content = content; } }
3.NewsContentActivity.java
package com.example.ken.fragmentbestpractice; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class NewsContentActivity extends AppCompatActivity { 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); String newsTitle = getIntent().getStringExtra("news_title"); String newsContent = getIntent().getStringExtra("news_content"); NewsContentFragment newsContentFragment = (NewsContentFragment) getSupportFragmentManager().findFragmentById(R.id.news_content_fragment); newsContentFragment.refresh(newsTitle, newsContent); } }
4.NewsContentFrag.java
package com.example.ken.fragmentbestpractice; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; 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 visibilityLayoyt = view.findViewById(R.id.visibility_layout); visibilityLayoyt.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); } }
5.NewsTitleFragment.java
package com.example.ken.fragmentbestpractice; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import java.util.Random; 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 newsTitleRecyclerView = (RecyclerView) view.findViewById(R.id.news_title_recycler_view); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); newsTitleRecyclerView.setLayoutManager(layoutManager); NewsAdapter adapter = new NewsAdapter(getNews()); newsTitleRecyclerView.setAdapter(adapter); return view; } private List<News> getNews(){ List<News> newsList = new ArrayList<>(); for (int i =0; 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(); } @Override public void onActivityCreated(@Nullable 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; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.news_item, viewGroup,false); final ViewHolder holder = new ViewHolder(view); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { News news = mNewsList.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; } @Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) { News news = mNewsList.get(i); viewHolder.newsTitleText.setText(news.getTitle()); } @Override public int getItemCount() { return mNewsList.size(); } } }
手机的实现效果是这样的: