先看效果
1.首先,要实现上面的效果,就必须得有两个字段,用来显示新闻的标题和内容,所以新建一个新闻类(News)
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;
}
}
2.创建用于显示新闻内容的布局
可以看到做左边是可以实现滑动效果的,所以我们新建一个news_title_frag.xml布局来实现它
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/news_title_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
接着再新建一个news_item.xml布局作为上面RecyclerView要展示的内容
<?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:maxLines="2"
android:ellipsize="end"
android:textSize="18sp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp">
</TextView>
右边的布局就如下展示
<?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:id="@+id/visibility_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="invisible">
<TextView
android:gravity="center"
android:padding="10dp"
android:textSize="20sp"
android:id="@+id/news_title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"/>
<TextView
android:padding="15dp"
android:textSize="18sp"
android:id="@+id/news_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
<View
android:layout_width="10dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#000" />
</RelativeLayout>
3.接着在res目录下新建layout-600dp文件夹,在这里面新建一个activity_main.xml文件,并对他添加以下代码,那么我们的布局就已经完成了,接下来就是往布局里添加具体内容了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_weight="1"
android:id="@+id/news_title_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:name="com.example.demo_fragment2.NewsTitleFragment" />
<FrameLayout
android:id="@+id/news_content_layout"
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="match_parent">
<fragment
android:id="@+id/news_content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.demo_fragment2.NewsContentFragment"/>
</FrameLayout>
</LinearLayout>
4.接着分别创建NewsTitleFragment类和NewsContentFragment类继承Fragment
package com.example.demo_fragment2;
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 java.util.ArrayList;
import java.util.List;
public class NewsTitleFragment extends Fragment {
//新建一个内部类来作为RecyclerView的适配器
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> newsList){
mNewsList=newsList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.new_item, parent, false);
final ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder( ViewHolder holder, int position) {
News news = mNewsList.get(position);
holder.newstitleText.setText(news.getTitle());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
NewsContentFragment fragment= (NewsContentFragment) getFragmentManager()
.findFragmentById(R.id.news_content_fragment);
fragment.refresh(news.getTitle(),news.getContent());
}
});
}
@Override
public int getItemCount() {
return mNewsList.size();
}
}
@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 news_title_recycler_view = view.findViewById(R.id.news_title_recycler_view);
LinearLayoutManager linearLayout = new LinearLayoutManager(getActivity());
news_title_recycler_view.setLayoutManager(linearLayout);
NewsAdapter adapter=new NewsAdapter(getNews());
news_title_recycler_view.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("这是一个 news title"+i);
news.setContent("这是一个 news Content"+i+"****");
newsList.add(news);
}
return newsList;
}
}
package com.example.demo_fragment2;
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;
public class NewsContentFragment extends Fragment {
private View view;
@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 newTitle,String newContent){
View visibility_layout = view.findViewById(R.id.visibility_layout);
visibility_layout.setVisibility(View.VISIBLE);
TextView title = view.findViewById(R.id.news_title);
TextView content = view.findViewById(R.id.news_content);
title.setText(newTitle);
content.setText(newContent);
}
}