标签:
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; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" tools:context="com.example.fragmentbasepractice.MainActivity" > <fragment android:id= "@+id/news_title_fragment" android:name= "com.example.fragmentbasepractice.NewsTitleFragment" android:layout_width= "match_parent" android:layout_height= "match_parent" /> </LinearLayout>再看大的
<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.fragmentbasepractice.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.fragmentbasepractice.NewsContentFragment" android:layout_width= "match_parent" android:layout_height= "match_parent" /> </FrameLayout > </LinearLayout>4,关于左边标题列表news_title_frag.xml,同时也是一个碎片类具体小布局
<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/news_title_list_view" android:layout_width= "match_parent" android:layout_height= "match_parent" ></ListView > </LinearLayout>5,列表中的小子项news_item.xml
<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:paddingRight= "10dp" android:paddingTop= "15dp" android:paddingBottom= "15dp" /> </LinearLayout>6,这些小子项的适配器NewsAdapter.java
public class NewsAdapter extends ArrayAdapter<News> { private int resourecId; public NewsAdapter(Context context, int resource, List<News> objects) { super( context, resource, objects); // TODO Auto-generated constructor stub resourecId = resource; } @Override public View getView( int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub /* return super.getView(position, convertView, parent); */ News news = getItem( position); View view; if ( convertView == null) { view = LayoutInflater.from(getContext()).inflate( resourecId, null); } else { view = convertView; } TextView newTitleText = (TextView) view.findViewById(R.id. news_title); newTitleText.setText( news.getTitle()); return view; }
public class NewsTitleFragment extends Fragment implements OnItemClickListener { private ListView newsTitleListView; private List<News> newsList; private NewsAdapter adapter; private boolean isTwoPane; @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach( activity); // 初始化数据 newsList = getNews(); adapter=new NewsAdapter( activity, R.layout. news_item, newsList); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view= inflater.inflate(R.layout. news_title_frag, container, false); newsTitleListView=(ListView) view.findViewById(R.id. news_title_list_view); newsTitleListView.setAdapter( adapter); newsTitleListView.setOnItemClickListener( this); return view; } @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated( savedInstanceState); if (getActivity().findViewById(R.id.news_content_layout )!=null) { //可以找到news_content-fragment布局的时候,为双页布局 isTwoPane= true; } else { isTwoPane= false; } } /* * 初始化数据 */ private List<News> getNews() { // TODO Auto-generated method stub List<News> newsList1 = new ArrayList<News>(); News new1 = new News(); News new2 = new News(); new1.setTitle( "这是标题1"); new1.setContent( "xxxx是的,这就是标题1的内容xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ); new2.setTitle( "这是标题2"); new2.setContent( "yyyy是的,这就是标题2的内容yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy" ); newsList1.add( new1); newsList1.add( new2); return newsList1; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub News news= newsList.get( position); if( isTwoPane){ //双页模式,刷新NewsContentFragment中的内容 NewsContentFragment newsContentFragment=(NewsContentFragment) getFragmentManager().findFragmentById(R.id.news_content_fragment ); newsContentFragment.refresh( news.getTitle(), news.getContent()); } else{ NewsContentActivity. actionStart(getActivity(), news.getTitle(), news.getContent()); } } }
<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.fragmentbasepractice.NewsContentFragment" android:layout_width= "match_parent" android:layout_height= "match_parent" /> </LinearLayout>
<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" /> <ImageView android:layout_width= "match_parent" android:layout_height= "1dp" android:scaleType= "fitXY" android:src= "@drawable/spilt_line" /> <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 > <ImageView android:layout_width= "1dp" android:layout_height= "match_parent" android:layout_alignParentLeft="true" android:scaleType= "fitXY" android:src= "@drawable/spilt_line_vertical" /> </RelativeLayout>
public class NewsContentFragment extends Fragment { private View view; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub view = inflater.inflate(R.layout. news_content_frag, container, false); return view; } 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); } }
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) { // TODO Auto-generated method stub 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界面 newsContentFragment.refresh( newsTitle, newsContent); }效果如下:
标签:
原文地址:http://blog.csdn.net/hll174/article/details/45676381