這個應用分五個部分
1.News類
2.內容碎片及佈局
3.內容活動及佈局
4.標題碎片及佈局
5.主活動及佈局
由於會用到recyclerview,所以先添加依賴庫
compile 'com.android.support:recyclerview-v7:24.2.1'
1.News類
是新聞實體類,封裝了標題和內容
package com.scx040407.fragmentbestpractice2;
/**
* Created by Administrator on 2017/4/9 0009.
*/
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.內容碎片及佈局
package com.scx040407.fragmentbestpractice2;
import android.os.Bundle;
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;
/**
* Created by Administrator on 2017/4/9 0009.
*/
public class NewsContentFragment extends Fragment {
@Nullable
@Override//onCreateView表示爲碎片創建視圖
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.news_content_frag,container,false);
return view;
}
//刷新方法 爲什麼要有刷新方法?因爲在平板中,點擊了新聞標題,在新聞內容出要相應的出現新聞內容,所以要有刷新方法隨時來響應標題的變化。在沒有點擊的時候不顯示內容。
public void refresh(String newsTitle,String newsContent){
View visibilityLayout= getView().findViewById(R.id.visibility_layout);
//將屬性改爲可見
visibilityLayout.setVisibility(View.VISIBLE);
TextView newsTitleText= (TextView) getView().findViewById(R.id.news_title);
TextView newsContentText= (TextView) getView().findViewById(R.id.news_content);
newsTitleText.setText(newsTitle);
newsContentText.setText(newsContent);
}
}
<?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:orientation="vertical"
android:visibility="invisible"//設置了不可見的屬性
android:id="@+id/visibility_layout">
<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:id="@+id/news_content"
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"/>
</RelativeLayout>
3.內容活動及佈局
這個活動是給手機用的,手機由於屏幕的原因標題和內容要分開展示,所以需要兩個活動,點擊標題會跳轉到內容活動中去。所以我們創建內容活動,佈局直接引用內容碎片。
package com.scx040407.fragmentbestpractice2;
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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<fragment //直接把內容碎片引入就行,包名當然不能省略
android:id="@+id/news_content_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.scx040407.fragmentbestpractice2.NewsContentFragment"/>
</LinearLayout>
4.標題碎片及佈局
package com.scx040407.fragmentbestpractice2;
import android.os.Bundle;
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;
/**
* Created by Administrator on 2017/4/9 0009.
*/
public class NewsTitleFragment extends Fragment {
private boolean isTwoPane;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.news_title_frag,container,false);
//recyclerview設置layoutmanager和adapter
RecyclerView newsTitleRecyclerView= (RecyclerView) view.findViewById(R.id.news_title_recycler_view);
LinearLayoutManager layoutManager=new LinearLayoutManager(getActivity());
newsTitleRecyclerView.setLayoutManager(layoutManager);
//參數是News類的集合,List<News>
NewsAdapter adapter=new NewsAdapter(getNews());
newsTitleRecyclerView.setAdapter(adapter);
return view;
}
//生成新聞標題和內容的方法
private List<News> getNews() {
List<News> newsList=new ArrayList<>();
for(int i=1 ; 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 s) {
Random random=new Random();
int length=random.nextInt(20)+1;
StringBuilder builder=new StringBuilder();
for(int i=0;i<length;i++){
builder.append(s);
}
return builder.toString();
}
@Override//確保與碎片相關聯的活動一定已經創建完畢時調用
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//判斷是平板還是手機,在當前活動中找內容佈局id,如果是平板的話就爲true否則false。佈局id請參考主活動及佈局。
if(getActivity().findViewById(R.id.news_content_layout)!=null){
isTwoPane=true;//平板
}else {
isTwoPane=false;//手機
}
}
//適配器,繼承了recyclerview的Adapter類,泛型是內部類ViewHolder繼承了recyclerview的ViewHolder類。
class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.ViewHolder>{
List<News> mNewsList;//所有的消息類的集合
class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
//拿到子項佈局中需要改變顯示內容的控件
textView= (TextView) itemView.findViewById(R.id.news_item);
}
}
//構造方法,參數是所有消息類的集合,用我們定義的集合來接收。
public NewsAdapter(List<News> newsList){
mNewsList=newsList;
}
@Override//必須重寫的方法之一
public NewsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//加載子項佈局
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item,parent,false);
//獲取實例
final ViewHolder holder=new ViewHolder(view);
//給視圖添加點擊監聽,目的是將News類傳到內容碎片中去展示。
//這裏有個問題,爲什麼不把監聽放到onBindViewHolder中去呢?
//那樣的話News類更方便獲取。
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//holder.getAdapterPosition獲取索引。
//mNewsList .get(int position)意思是獲取指定索引的內容,即News類。
News news=mNewsList.get(holder.getAdapterPosition());
if(isTwoPane){
//這裏是適配器,相當於活動,想拿到碎片要藉助於FragmentManager。
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(NewsAdapter.ViewHolder holder, int position) {
//這裏的position代表什麼?如圖是index,系統自動爲每個子項分配索引。
//將List索引處的內容賦給news。
News news=mNewsList.get(position);
//在這裏設置了每一個子項應該展示的內容,即新聞標題news.getTitle()。
holder.textView.setText(news.getTitle());
}
@Override//必須重寫的方法之三
public int getItemCount() {
//返回的值代表什麼?如圖是5,是列表中子項的個數。
return mNewsList.size();
}
}
}
//標題碎片的佈局,只有一個recyclerview
<?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">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
//recyclerview的子項佈局
<?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_item"
android:maxLines="1"
android:ellipsize="end"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp"
android:paddingBottom="15dp"
android:textSize="18sp">
</TextView>
5.主活動及佈局
主活動中什麼都不用寫
package com.scx040407.fragmentbestpractice2;
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);
}
}
手機中,主佈局引用標題碎片
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/news_title_fragment"
android:name="com.scx040407.fragmentbestpractice2.NewsTitleFragment"/>
</FrameLayout>
平板中主佈局引用標題碎片和內容碎片,放到一起展示
這裏要注意,如圖在res文件夾中新建layout-sw600dp文件夾,把佈局文件放進去,系統會自動根據設備屏幕的大小進行選擇,如果屏幕大於600dp就啓用這個佈局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/news_title_fragment"
android:name="com.scx040407.fragmentbestpractice2.NewsTitleFragment"/>
<FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:id="@+id/news_content_layout"
>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.scx040407.fragmentbestpractice2.NewsContentFragment"
android:id="@+id/news_content_fragment"/>
</FrameLayout>
</LinearLayout>