编写利用Fragment创建新闻列表
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_item.xml,用于新闻列表子项的布局
<?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" >
<TextView
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:paddingTop="15dp"
android:paddingRight="10dp"
android:paddingBottom="15dp"
/>
</LinearLayout>
3、新建适配器NewsAdapter,代码如下:
public class NewsAdapter extends ArrayAdapter<News> {
private int resourceId;
public NewsAdapter(Context context, int textViewResourceId, List<News> objects) {
super(context, textViewResourceId, objects);
this.resourceId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
News news = getItem(position);
View view;
if (convertView == null) {
view = LayoutInflater.from(getContext()).inflate(resourceId, null);
} else {
view = convertView;
}
TextView newsTitleText = (TextView) view.findViewById(R.id.news_title);
newsTitleText.setText(news.getTitle());
return view;
}
}
4、新建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: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_titles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20sp"
android:gravity="center"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#ff666666"
/>
<TextView
android:id="@+id/news_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textSize="18sp"
android:padding="15dp"
/>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#ff666666"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
5、新建NewsContentFragment,代码如下:
public class NewsContentFragment extends Fragment{
private View rootView ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.news_content_frag, container, false);
return rootView;
}
public void reflush(String newsTitle, String newsContent){
View visibilityLayout = rootView.findViewById(R.id.visibility_layout);
visibilityLayout.setVisibility(View.VISIBLE);
TextView title = (TextView) rootView.findViewById(R.id.news_titles);
TextView content = (TextView) rootView.findViewById(R.id.news_content);
title.setText(newsTitle);//刷新新闻的标题
content.setText(newsContent);//刷新新闻的内容
}
}
6、新建new_content.xml
<?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:name="com.example.studytest.part4.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
7、新建NewsContentActivity,代码如下:
public class NewsContentActivity extends Activity{
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);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.news_content);
String newsTitle = getIntent().getStringExtra("news_title");//获取传入的标题
String newsContent = getIntent().getStringExtra("news_content");//获取传入的内容
NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);
newsContentFragment.reflush(newsTitle, newsContent);
}
}
8、新建news_title_frag.xml
<?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" >
<ListView
android:id="@+id/title_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
9、新建NewsTitleFragment
public class NewsTitleFragment extends Fragment {
private ListView newsTitleListView;
private List<News> news_list;
private NewsAdapter newsAdapter;
private Boolean isTwoPane;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
news_list = getNews();
newsAdapter = new NewsAdapter(activity, R.layout.news_item, news_list);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.new_title_frag, container, false);
newsTitleListView = (ListView) rootView.findViewById(R.id.title_list_view);
newsTitleListView.setAdapter(newsAdapter);
newsTitleListView.setOnItemClickListener(oicl);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(getActivity().findViewById(R.id.news_content_layout) != null){
isTwoPane = true;
}else {
isTwoPane = false;
}
}
private OnItemClickListener oicl = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
News news = news_list.get(position);
if(isTwoPane){
NewsContentFragment newsContentFragment = (NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment);
newsContentFragment.reflush(news.getTitle(), news.getContent());
}else {
//单页模式,直接启动NewsContentActivity
NewsContentActivity.actionStart(getActivity(), news.getTitle(), news.getContent());
}
}
};
private List<News> getNews() {
List<News> newsList = new ArrayList<News>();
News news1 = new News();
news1.setTitle("hello world");
news1.setContent(
"iadh do fj kjew skjf jflf jlfa flepr oiirrl ;l;ijr' llrklwe lrew klwpur 9ieokk ;ajht lfj jkrhl jlrejlpsfyey yurehsjf fejw jjrw jlsfj ");
newsList.add(news1);
News news2 = new News();
news2.setTitle("hello world2");
news2.setContent(
"22222 22 iadh do fj kjew skjf jflf jlfa flepr oiirrl ;l;ijr' llrklwe lrew klwpur 9ieokk ;ajht lfj jkrhl jlrejlpsfyey yurehsjf fejw jjrw jlsfj ");
newsList.add(news2);
return newsList;
}
}
10、新建activity_main.xml
<?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:id="@+id/news_title_fragment"
android:name="com.example.studytest.part4.NewsTitleFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
上面的代码表示在单页模式下,只会加载一个新闻标题的碎片,然后新建layout-sw600dp文件夹,在这个文件夹下再新建activity_main.xml
11、新建layout-sw600dp,并新建activity_main.xml
<?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:id="@+id/news_title_fragment"
android:name="com.example.studytest.part4.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="3" >
<fragment
android:id="@+id/news_content_fragment"
android:name="com.example.studytest.part4.NewsContentFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout >
可以看出,在双页模式下可以同时引入两个碎片,并将新闻内容的碎片放在了一个FrameLayout布局下, 而这个布局的id正是news_content_layout,因此能够找到这个id就是双页模式,否则就是单页模式。