同时兼容手机和平板。
进入应用后先显示新闻列表,当在手机上使用时,使用单页模式,单击列表项会打开新的页面。
当在平板上使用时,使用双页模式,单击左侧列表项时直接更新右侧新闻内容页。
MainActivity.java
package com.example.newsdemos;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
NewsContentActivity.java
package com.example.newsdemos;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
public class NewsContentActivity extends AppCompatActivity {
public static void actionStart(Context context,String title,String content){
Intent intent = new Intent(context,NewsContentActivity.class);
intent.putExtra("news_title",title);
intent.putExtra("news_content",content);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_content);
Intent intent = getIntent();
String newsTitle = intent.getStringExtra("news_title");
String newsContent = intent.getStringExtra("news_content");
FragmentManager fm = getSupportFragmentManager();
NewsContentFragment fragment =(NewsContentFragment) fm.findFragmentById(R.id.news_content_fragment);
fragment.refresh(newsTitle,newsContent);
}
}
NewsContentFragment.java
package com.example.newsdemos;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
/**
* A simple {@link Fragment} subclass.
*/
public class NewsContentFragment extends Fragment {
private View fragment_view;
public NewsContentFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
fragment_view = inflater.inflate(R.layout.fragment_news_content, container, false);
return fragment_view;
}
public void refresh(String newsTitle,String newsContent){
View visibilityLayout = fragment_view.findViewById(R.id.visibility_layout);
visibilityLayout.setVisibility(View.VISIBLE);
TextView titleView = (TextView)fragment_view.findViewById(R.id.news_title);
TextView contentView = (TextView)fragment_view.findViewById(R.id.news_content);
titleView.setText(newsTitle);
contentView.setText(newsContent);
}
}
NewsTitleFragment.java
package com.example.newsdemos;
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.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* A simple {@link Fragment} subclass.
*/
public class NewsTitleFragment extends Fragment {
private List<News> newsList;
private Boolean isTwoPane;
public NewsTitleFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_news_title, container, false);
//view.RecyclerView组件
RecyclerView newsTitleRecyclerView =(RecyclerView) view.findViewById(R.id.news_title_recycler_view);
//准备子项布局news_item.xml
//准备新闻数据
newsList = getNews();
//准备Adapter
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
newsTitleRecyclerView.setLayoutManager(layoutManager);
NewsAdapter adapter = new NewsAdapter(newsList);
newsTitleRecyclerView.setAdapter(adapter);
return view;
}
@Nullable
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(getActivity().findViewById(R.id.news_content_layout)!=null){
isTwoPane = true;
}else {
isTwoPane = false;
}
}
private List<News> getNews(){
List<News> newsList = new ArrayList<>();
for(int i = 1 ;i<=20;i++){
News news = new News();
news.setTitle("This is news title" +i);
news.setContent(getRandomLengthContent("This is news title" + i + "."));
newsList.add(news);
}
return newsList;
}
private String getRandomLengthContent(String s){
Random random = new Random();
int length = random.nextInt(20)+1;
StringBuilder builder = new StringBuilder();
for(int i = 1;i<=length;i++)
{
builder.append(s);
}
return builder.toString();
}
class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{
private List<News> mnewsList;
class ViewHolder extends RecyclerView.ViewHolder{
TextView titleView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
titleView = (TextView)itemView.findViewById(R.id.news_title);
}
}
public NewsAdapter(List<News> newsList) {
mnewsList = newsList;
}
//创建子项布局的视图,itemView
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
View itemView = mInflater.inflate(R.layout.news_title_item,parent,false);
final ViewHolder holder = new ViewHolder(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
News news = mnewsList.get(holder.getAdapterPosition());
if(isTwoPane)//双页模式
{
FragmentManager fm = getFragmentManager();
NewsContentFragment fragment = (NewsContentFragment) fm.findFragmentById(R.id.news_content_fragment);
fragment.refresh(news.getTitle(),news.getContent());
}else {
// News news = mnewsList.get(holder.getAdapterPosition());
NewsContentActivity.actionStart(getActivity(),news.getTitle() ,news.getContent());
}
}
});
return holder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
News news = mnewsList.get(position);
holder.titleView.setText(news.getTitle());
}
@Override
public int getItemCount() {
return mnewsList.size();
}
}
}
News.java
package com.example.newsdemos;
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;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.newsdemos.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
activity_main.xml(land)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.newsdemos.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="2">
<fragment
android:id="@+id/news_content_fragment"
android:name = "com.example.newsdemos.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
activity_news_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".NewsContentActivity">
<fragment
android:id="@+id/news_content_fragment"
android:name = "com.example.newsdemos.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
fragment_news_content.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NewsContentFragment">
<!-- TODO: Update blank fragment layout -->
<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:id="@+id/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="14sp" />
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#f00"/>
</FrameLayout>
fragment_news_title.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NewsTitleFragment">
<!-- TODO: Update blank fragment layout -->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/news_title_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
news_title_item.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
>
</TextView>