有了Fragment后主要是操作Fragment,activity里面没有太多的操作。
小屏幕的设备会使用2个xml文件
1.activity_main.xml
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment
android:id="@+id/leftfragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:name="com.example.fragment.LeftFragment"/>
</LinearLayout>
2.activity_second.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<fragment
android:id="@+id/rightfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.fragment.RightFragment"/>
</LinearLayout>
大的屏幕的设备只是用一个xml文件
activity_main.xml
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
>
<fragment
android:id="@+id/leftfragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:name="com.example.fragment.LeftFragment"/>
<fragment
android:id="@+id/rightfragment"
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent"
android:name="com.example.fragment.RightFragment"/>
</LinearLayout>
LeftFragment.java
package com.example.fragment;
import java.util.ArrayList;
import java.util.List;
import com.example.adapter.NewsAdapter;
import com.example.entity.News;
import com.example.testsometing.R;
import com.example.testsometing.Second_activity;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class LeftFragment extends Fragment{
List<News> newsList;
ListView newsListView;
boolean isTwoPane;
RightFragment rightFragment;
/*
初始化数据
*/
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
initNews();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container,false);
newsListView =(ListView)view.findViewById(R.id.newsListView);
NewsAdapter newsAdapter =new NewsAdapter(getActivity(),R.layout.news_title, newsList);
newsListView.setAdapter(newsAdapter);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
/*
下面这个getActivity().findViewById(R.id.rightfragment)不能放到onAttach()
*/
if(getActivity().findViewById(R.id.rightfragment)==null){
isTwoPane=false;
}
else{
isTwoPane=true;
rightFragment=(RightFragment) getFragmentManager().findFragmentById(R.id.rightfragment);
/*在activity里面实例化fragment要使用
getSupportFragmentManager().findFragmentById(R.id.rightfragment);*/
}
newsListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if(isTwoPane){
/*
双页模式,刷新Fragment
*/ rightFragment.refresh(newsList.get(arg2).getTitle(),newsList.get(arg2).getContent());
}
else{
/*
单页模式,直接跳转页面
*/ Second_activity.onActionStart(getActivity(), newsList.get(arg2).getTitle(), newsList.get(arg2).getContent());
}
}
});
}
public void initNews(){
newsList = new ArrayList<News>();
for(int i=0;i<10;i++){
News news=new News();
news.setTitle("title_"+i);
news.setContent("content_"+i);
newsList.add(news);
}
}
}
left_fragment.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/newsListView"
/>
</LinearLayout>
实体类News
package com.example.entity;
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;
}
}
NewsAdapter.java(新闻列表的适配器)
package com.example.adapter;
import java.util.List;
import com.example.entity.News;
import com.example.testsometing.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class NewsAdapter extends ArrayAdapter<News>{
int resourceId;
View view;
NewsViewHolder viewHolder;
public class NewsViewHolder{
TextView title;
}
public NewsAdapter(Context context, int resource, List<News> objects) {
super(context, resource, objects);
resourceId=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
view=LayoutInflater.from(getContext()).inflate(resourceId, null);
viewHolder =new NewsViewHolder();
viewHolder.title=(TextView) view.findViewById(R.id.news_title);
view.setTag(viewHolder);
}else{
view = convertView;
viewHolder =(NewsViewHolder) view.getTag();
}
News news =getItem(position);
viewHolder.title.setText(news.getTitle());
return view;
}
}
news_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="?android:attr/listPreferredItemHeight">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/news_title"/>
</LinearLayout>
<!-- android:minHeight="?android:attr/listPreferredItemHeight" 用于撑开item的高度 -->
RightFragment.java
package com.example.fragment;
import com.example.testsometing.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RightFragment extends Fragment{
TextView newstTitle;
TextView newsContent;
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, container,false);
newstTitle = (TextView) view.findViewById(R.id.newsTitle);
newsContent=(TextView) view.findViewById(R.id.newsContent);
return view;
}
public void refresh(String title,String content){
newstTitle.setText(title);
newsContent.setText(content);
}
}
right_fragment.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:id="@+id/newsTitle"
android:gravity="center"
android:text="title"
style="?android:attr/listSeparatorTextViewStyle"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:id="@+id/newsContent"/>
</LinearLayout>
<!--为TextView加下划线
style="?android:attr/listSeparatorTextViewStyle" -->
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Second_activity.java
package com.example.testsometing;
import com.example.fragment.RightFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class Second_activity extends FragmentActivity{
public static void onActionStart(Context context,String title,String content){
Intent intent =new Intent(context,Second_activity.class);
intent.putExtra("title", title);
intent.putExtra("content", content);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actcivity_second);
String title =getIntent().getStringExtra("title");
String content =getIntent().getStringExtra("content");
RightFragment rightFragment = (RightFragment) getSupportFragmentManager().findFragmentById(R.id.rightfragment);
rightFragment.refresh(title,content);
};
}
小屏幕效果:
大屏幕效果