一、新聞串列使用RecyclerView,添加依賴庫
步驟如下:





二、新建新聞物體類

package com.example.fragmentbestpractice;
public class News {
/*
* 欄位是:新聞標題、新聞內容
* 生成get set方法
* */
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;
}
}
三、新建布局檔案,作為新聞內容的布局

<?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: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>
四、新建NewsContentFragment類,創建好新聞內容的碎片

package com.example.fragmentbestpractice;
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 org.w3c.dom.Text;
public class NewsContentFragment extends Fragment {
private View view;
/*
* 在onCreateView方法中加載news_content_frag布局
* */
@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;
}
/*
* refresh方法用于將新聞的標題和內容顯示在界面上:通過findViewById方法獲取新聞標題和內容的控制元件
* */
public void refresh(String newsTitle,String newsContent){
View visibilityLayout=view.findViewById(R.id.visibility_layout);
visibilityLayout.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);
}
}
五、新建一個活動,用于在單頁模式中使用

修改布局檔案中的代碼

<?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">
<!--
通過代碼的復用性,在此布局檔案中直接引入NewsContentFragment相當于把news_content_frag
布局的內容自動加了進來
-->
<fragment
android:id="@+id/news_content_fragment"
android:name="com.example.fragmentbestpractice.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
六、修改NewsContentActivity中的代碼

package com.example.fragmentbestpractice;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
public class NewsContentActivity extends AppCompatActivity {
/*
* 完成Intent的構建
* */
public static void antionStart(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獲取傳入的新聞標題和新聞內容
* 呼叫FragmentManager的findFragmentById得到NewsContentFragment的實體
* 呼叫他的 refresh方法將新聞標題 和 新聞內容傳入
* */
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);
/*
* 重繪NewsContentFragment界面
* */
}
}
七、創建用于顯示新聞串列的布局

<?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">
<!--用于顯示新聞串列的RecyclerView-->
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/news_title_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
八、新建RecyclerView子項的布局

<?xml version="1.0" encoding="utf-8"?>
<!--android:padding表示給控制元件的周圍加上補白
android:maxLines="1"表示這個TextView只能單行顯示
android:ellipsize設定當文本內容超出控制元件寬度時,文本的縮略方式,在尾部進行縮略-->
<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:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"/>
九、新建NewsTitleFragment類用于展示新聞串列的碎片

package com.example.fragmentbestpractice;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class NewsTitleFragment extends Fragment {
private boolean isTwoPane;
/*
* 在onCreateView中加載news_title_frag布局
* */
@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);
return view;
}
/*
* onActivityCreated方法通過在活動中能夠找到id為news_content_layout的View來判斷當前是單頁還是雙頁模式
* 要求:讓id為news_content_layout的View只在雙頁模式中出現,借助限定符
* */
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
/*
* 可以找到news_content_layout布局時,為雙頁模式,找不到時為單頁模式
* */
if (getActivity().findViewById(R.id.news_content_layout)!=null){
isTwoPane=true;
}else{
isTwoPane=false;
}
}
}
十、修改activity_main.xml中的代碼,使得在單頁模式下,只會加載一個新聞標題的碎片

<?xml version="1.0" encoding="utf-8"?>
<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.example.fragmentbestpractice.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
十一、新建檔案夾以及布局檔案,使得雙頁模式下引入兩個碎片

<?xml version="1.0" encoding="utf-8"?>
<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.example.fragmentbestpractice.NewsTitleFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<!--將新聞內容的碎片放在FrameLayout布局下,此布局ID正好是news_content_layout
因此能找到此id就是雙頁模式-->
<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.fragmentbestpractice.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>
十二、在NewsTitleFragment中通過RecyclerView將新聞串列展示出來,需要在 NewsTitleFragment中新建內部類NewsAdapter作為RecyclerView的配接器
/*
* 將配接器寫成內部類:可以直接訪問NewsTitleFragment的變數,例如isTwoPane
* */
class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{
private List<News> mNewsList;
class ViewHolder extends RecyclerView.ViewHolder {
TextView newsTitleText;
public ViewHolder(@NonNull View itemView) {
super(itemView);
newsTitleText=(TextView) itemView.findViewById(R.id.news_title);
}
}
public NewsAdapter(List<News> newsList){
mNewsList=newsList;
}
@NonNull
@Override
public NewsAdapter.ViewHolder onCreateViewHolder(@NonNull 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() {
/*
* 該點擊事件首先獲取點擊項的News實體,然后通過isTwoPane變數來判斷當前是單頁還是雙頁模式,
* 單頁模式就啟動新的活動去顯示新聞內容,
* 雙頁模式就更新新聞內容碎片里的資料
* */
@Override
public void onClick(View v) {
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 NewsAdapter.ViewHolder holder, int position) {
News news=mNewsList.get(position);
holder.newsTitleText.setText(news.getTitle());
}
@Override
public int getItemCount() {
return mNewsList.size();
}
}
十三、向RecyclerView中填充資料,繼續修改NewsTitleFragment的代碼:

在onCreateView方法中新增代碼
/*
* 在碎片中使用RecyclerView,
* */
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;
}
/*
* getNews方法用于初始化50條模擬新聞資料
* */
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;
}
/*
* getRandomLengthContent隨機生成新聞內容的長度
* */
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();
}
作業完成分別在手機模擬器和平板模擬器上運行:

可以看到許多條新聞的標題,點擊第一條新聞,會啟動一個新的活動來顯示新聞的內容

將程式 在平板模擬器上運行

同樣點擊第一條新聞:

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/208834.html
標籤:其他
上一篇:RN環境搭建
