package com.example.snofragment; import java.util.LinkedList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.app.ListFragment; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn1=(Button)findViewById(R.id.button1); btn1.setOnClickListener(new OnClickListener() { public void onClick(View v){ Log.i("snodebug","snodddddd"); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); snoFragment1 fragment1 = new snoFragment1(); fragmentTransaction.replace(R.id.layout1, fragment1); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }); Button btn2=(Button)findViewById(R.id.button2); btn2.setOnClickListener(new OnClickListener(){ public void onClick(View v){ FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); snoFragment3 fragment1 = new snoFragment3(); fragmentTransaction.replace(R.id.layout2, fragment1); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } }); } public static class snoFragment1 extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.snofragment1, container, false); } @Override public void onSaveInstanceState(Bundle outState) { // TODO Auto-generated method stub super.onSaveInstanceState(outState); System.out.println("Fragment1-->onSaveInstanceState"); ///outState.putString("curChoice", mCurCheckPosition);//保存当前的下标 } } public static class snoFragment2 extends ListFragment{ public static String[] array = { "text1,", "text2", "text3", "text4", "text5,", "text6", "text7", "text8" }; @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); System.out.println("Fragment-->onActivityCreted"); setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array));///<---这里使用系统的列表资源. } @Override public void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); Log.i("snoDebug","----position is :"+position); } } ///------------------list中放置的是按钮和各种其他的图标. 可以这样做. public static class snoFragment3 extends ListFragment{ ///<---在其中放置图片和文字. String a = "A", b = "B", c = "C", d = "D", e = "E"; private snoArrayAdapter mVideoAdapter; @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); System.out.println("Fragment-->onActivityCreted"); List<String> list = new LinkedList<String>(); list.add(a); list.add(e); list.add(d); mVideoAdapter = new snoArrayAdapter(getActivity(), android.R.layout.simple_list_item_1,list); setListAdapter(mVideoAdapter);///<---这里使用系统的列表资源. } @Override public void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); Log.i("snoDebug","----snoFragment3 position is :"+position); } } /// public static class snoArrayAdapter extends ArrayAdapter<String>{ public snoArrayAdapter(Context context, int textViewResourceId, List<String> objects) { super(context, textViewResourceId, objects); // TODO Auto-generated constructor stub } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; View v = convertView; LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = inflater.inflate(R.layout.video_grid_item, parent, false); holder = new ViewHolder(); holder.layout = v.findViewById(R.id.layout_item); holder.thumbnail = (ImageView) v.findViewById(R.id.ml_item_thumbnail); holder.title = (TextView) v.findViewById(R.id.ml_item_title); holder.subtitle = (TextView) v.findViewById(R.id.ml_item_subtitle); holder.progress = (ProgressBar) v.findViewById(R.id.ml_item_progress); v.setTag(holder); return v; } } static class ViewHolder { boolean listmode; View layout; ImageView thumbnail; TextView title; TextView subtitle; ImageView more; ProgressBar progress; } }