2011-02-18 60 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它有助于在定制布局/视图