2011-02-18 133 views
1

適配器和充氣器在android中有什麼用?適配器和充氣器

+0

我會建議閱讀文檔離子適配器和充氣機。 http://developer.android.com/reference/java/util/zip/Inflater.html,http://developer.android.com/reference/android/widget/Adapter.html。 – EboMike 2011-02-18 05:26:21

回答

1

充氣機用於加載佈局資源。例如:

private class SMSAdapter extends CursorAdapter { 

    public SMSAdapter(Context context, Cursor c) { 
     super(context, c); 
    } 

    public SMSAdapter(Context context, Cursor c, boolean autoRequery) { 
     super(context, c, autoRequery); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 

     Log.i(TAG, "adapter -- new view"); 

     View itemView = LayoutInflater.from(context).inflate(R.layout.sms_list_item, parent,false); 

     ViewHolder holder = new ViewHolder(); 
     holder.who_tv = (TextView) itemView.findViewById(R.id.sms_who_tv); 
     holder.content_tv = (TextView) itemView.findViewById(R.id.sms_content_itv); 
     holder.time_tv = (TextView) itemView.findViewById(R.id.sms_time_tv); 
     itemView.setTag(holder); 

     return itemView; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     Log.i(TAG, "adapter -- bind view"); 
     final ViewHolder holder = (ViewHolder) view.getTag(); 
     holder.who_tv.setText("來自:"); 
     holder.who_tv.append(cursor.getString(cursor.getColumnIndexOrThrow("address"))); 
     holder.content_tv.setText(cursor.getString(cursor.getColumnIndexOrThrow("body"))); 
     holder.time_tv.setText(Tools.date2str(new Date(cursor.getLong(cursor.getColumnIndexOrThrow("date"))))); 
     //根據類型,設置背景 
     int type = cursor.getInt(cursor.getColumnIndexOrThrow("type")); 
     final LinearLayout.LayoutParams params = (LayoutParams) holder.content_tv.getLayoutParams(); 
     if(2 == type) { 
      holder.content_tv.setBackgroundResource(R.drawable.chatfrom_bg); 
      params.gravity = Gravity.LEFT; 
     } else { 
      holder.content_tv.setBackgroundResource(R.drawable.chatto_bg); 
      params.gravity = Gravity.RIGHT; 
     } 
    } 

    class ViewHolder { 
     TextView who_tv; 
     TextView time_tv; 
     TextView content_tv; 
    } 
} 
0

以我的經驗吹氣用於啓動佈局:e.g:

public class ProfileFragment extends Fragment{ 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
    View rootView = inflater.inflate(R.layout.profile,container, false); 
    return rootView; 
}} 

適配器:適配器基本數據連接到一個視圖。比方說,如果你有一個列表,並且你想用項目填充它,那麼適配器負責填充。例如:http://www.ezzylearning.com/tutorial.aspx?tid=1763429

1
適配器&氣筒之間

簡單的區別

ADAPTER它充當刪節數據和圖,之間其還負責填充視圖

INFLATOR它有助於在定製佈局/視圖