Android编程权威指南 - 第9章 使用ListFragme

2016-12-02  本文已影响104人  JMasche

程序目标

程序最后会生成如下的界面

最终界面

程序整体结构图

程序结构图

模型层

模型层需要:一个单条信息定义的类;由于最终显示的是一个列表,所以需要一个ArrayList对象;另外需要一个生成固定列表数据的类。

Crime对象

根据最终显示的内容,Crime需要一个title数据、一个列表每个条目的data数据、一个表示复选框是否选中的标志数据。再加上一个后台可能唯一确定id数据。具体代码如下:

public class Crime {   
      private UUID mId;    
      private String mTitle;    
      private Date mDate;    
      private boolean mSolved;    
      
      public Date getDate() {        
            return mDate;    
      }    

      public void setDate(Date date) {        
            mDate = date;    
      }    

      public boolean isSolved() {        
            return mSolved;    
      }    

      public void setSolved(boolean solved) {        
            mSolved = solved;    
      }    

      public Crime() {        
            mId = UUID.randomUUID();        
            mDate = new Date();    
      }    

      public UUID getId() {        
            return mId;    
      }    

      public String getTitle() {        
            return mTitle;    
      }    

      public void setTitle(String title) {        
            mTitle = title;    
      }    

      @Override    
      public String toString() {        
            return mTitle;    
      }
}

生成Crime列表数据的CrimeLab类

public class CrimeLab {    
      private ArrayList<Crime> mCrimes;    
      private static CrimeLab sCrimeLab;    
      private Context mAppContext;    

      public CrimeLab(Context appContext) {        
            mAppContext = appContext;        
            mCrimes = new ArrayList<Crime>();        
            
            for (int i = 0; i < 100; i++) {            
                  Crime c = new Crime();            
                  c.setTitle("Crime #" + i);            
                  c.setSolved(i % 2 == 0);            
                  mCrimes.add(c);        
            }    
      }    

      public static CrimeLab get(Context c) {        
            if (sCrimeLab == null) {            
                  sCrimeLab = new CrimeLab(c.getApplicationContext());        
            }        
            return sCrimeLab;    
      }    

      public ArrayList<Crime> getCrimes() {        
            return mCrimes;    
      }    

      public Crime getCrime(UUID id) {        
            for (Crime c : mCrimes) {            
                  if (c.getId().equals(id)) {                
                        return c;            
                  }        
            }        
            return null;    
      }
}

视图层+控制层

概述

抽象的Activity基类

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"             
      android:id="@+id/fragmentContainer"             
      android:layout_width="match_parent"             
      android:layout_height="match_parent" />
FragmentManager fm = getFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
fragment = createFragment();
fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
protected abstract Fragment createFragment();

SingleFragmentActivity类的全部代码如下:

public abstract class SingleFragmentActivity extends Activity {    
      protected abstract Fragment createFragment();    

      @Override    
      public void onCreate(Bundle savedInstanceState) {        
            super.onCreate(savedInstanceState);              
            setContentView(R.layout.activity_fragment);        
            
            FragmentManager fm = getFragmentManager();        
            Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);        

            if (fragment == null) {            
                  fragment = createFragment();            
                  fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();        
            }    
      }
}

CrimeList显示界面的实现

public class CrimeListActivity extends SingleFragmentActivity {    
      protected Fragment createFragment() {        
            return new CrimeListFragment();    
      }
}
<?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="wrap_content">        

      <CheckBox        
            android:id="@+id/crime_list_item_solvedCheckBox"        
            android:layout_width="wrap_content"        
            android:layout_height="wrap_content"        
            android:gravity="center"        
            android:layout_alignParentRight="true"        
            android:enabled="false"        
            android:focusable="false"        
            android:padding="4dp"/>    

      <TextView        
            android:id="@+id/crime_list_item_titleTextView"        
            android:layout_width="match_parent"        
            android:layout_height="wrap_content"        
            android:layout_toLeftOf="@id/crime_list_item_solvedCheckBox"        
            android:textStyle="bold"        
            android:paddingLeft="4dp"        
            android:paddingRight="4dp"        
            android:text="Crime title"/>    

      <TextView        
            android:id="@+id/crime_list_item_dateTextView"        
            android:layout_width="match_parent"        
            android:layout_height="wrap_content"        
            android:layout_below="@id/crime_list_item_titleTextView"        
            android:layout_toLeftOf="@id/crime_list_item_solvedCheckBox"        
            android:paddingLeft="4dp"        
            android:paddingRight="4dp"        
            android:paddingTop="4dp"        
            android:text="Crime date"/>
</RelativeLayout>
getActivity().setTitle(R.string.crimes_title);
mCrimes = CrimeLab.get(getActivity()).getCrimes();
public class CrimeAdapter extends ArrayAdapter<Crime> {    
      public CrimeAdapter(ArrayList<Crime> crimes) {        
            super(getActivity(), 0, crimes);    
      }
}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {    
      // If we weren't given a view, inflate one    
      if (convertView == null) {        
            convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null);    
      }    

      // configure the view of this Crime    
      Crime c = getItem(position);    
      
      TextView titleTextView = (TextView)convertView.findViewById(R.id.crime_list_item_titleTextView);    
      titleTextView.setText(c.getTitle());    
      
      TextView dateTextView = (TextView)convertView.findViewById(R.id.crime_list_item_dateTextView);    
      dateTextView.setText(c.getDate().toString());    

      CheckBox solvedCheckBox = (CheckBox) convertView.findViewById(R.id.crime_list_item_solvedCheckBox);    
      solvedCheckBox.setChecked(c.isSolved());    

      return convertView;
}
CrimeAdapter adapter = new CrimeAdapter(mCrimes);
setListAdapter(adapter);
public class CrimeListFragment extends ListFragment {    
      private ArrayList<Crime> mCrimes;    
      private static final String TAG = "CrimeListFragment";    

      @Override    
      public void onCreate(Bundle savedInstanceState) {        
            super.onCreate(savedInstanceState);        
            getActivity().setTitle(R.string.crimes_title);        
            mCrimes = CrimeLab.get(getActivity()).getCrimes();        

            CrimeAdapter adapter = new CrimeAdapter(mCrimes);        
            setListAdapter(adapter);    
      }    

      @Override    
      public void onListItemClick(ListView a, View v, int position, long id) {        
            Crime c = ((CrimeAdapter)getListAdapter()).getItem(position);        
            Log.d(TAG, c.getTitle() + " was clicked");    
      }    

      public class CrimeAdapter extends ArrayAdapter<Crime> {        
            public CrimeAdapter(ArrayList<Crime> crimes) {            
                  super(getActivity(), 0, crimes);       
            }        

            @Override        
            public View getView(int position, View convertView, ViewGroup parent) {            
                  // If we weren't given a view, inflate one            
                  if (convertView == null) {                
                        convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime, null);           
                  }            

                  // configure the view of this Crime            
                  Crime c = getItem(position);            

                  TextView titleTextView = (TextView)convertView.findViewById(R.id.crime_list_item_titleTextView);            
                  titleTextView.setText(c.getTitle());            

                  TextView dateTextView = (TextView)convertView.findViewById(R.id.crime_list_item_dateTextView);            
                  dateTextView.setText(c.getDate().toString());            

                  CheckBox solvedCheckBox = (CheckBox) convertView.findViewById(R.id.crime_list_item_solvedCheckBox);            
                  solvedCheckBox.setChecked(c.isSolved());            
 
                  return convertView;        
            }    
      }
}

启动代码就可以看到文章开头时显示的内容了

上一篇 下一篇

猜你喜欢

热点阅读