android 入门进修条记 LayoutInflater的利用

 在实际开拓种LayoutInflater这个类还长短常有用的,它的浸染雷同于 findViewById(),

差异点是LayoutInflater是用来找layout下xml机关文件,而且实例化!而findViewById()是找详细xml下的详细 widget控件(如:Button,TextView等)。

 

用法有两种:

LayoutInflater inflater = LayoutInflater.from(this); View view=inflater.inflate(R.layout.ID, null); 可能爽性并成一句: View view=LayoutInflater.from(this).inflate(R.layout.ID, null);

另一种要领:

LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE); View view=inflater.inflate(R.layout.ID, null);

下面code为上例提到的MyAdapter 类的实现code

/* import相关class */ import java.io.File; import java.util.List; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; /* 自界说的Adapter,担任android.widget.BaseAdapter */ public class MyAdapter extends BaseAdapter { /* 变量声明 mIcon1:回到根目次的图文件 mIcon2:回到上一层的图档 mIcon3:文件夹的图文件 mIcon4:文件的图档 */ private LayoutInflater mInflater; private Bitmap mIcon1; private Bitmap mIcon2; private Bitmap mIcon3; private Bitmap mIcon4; private List<String> items; private List<String> paths; /* MyAdapter的结构器,传入三个参数 */ public MyAdapter(Context context,List<String> it,List<String> pa) { /* 参数初始化 */ mInflater = LayoutInflater.from(context); items = it; paths = pa; mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.back01); mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.back02); mIcon3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.folder); mIcon4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.doc); } /* 因担任BaseAdapter,需包围以下要领 */ @Override public int getCount() { return items.size(); } @Override public Object getItem(int position) { return items.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position,View convertView,ViewGroup parent) { ViewHolder holder; if(convertView == null) { /* 利用自界说的file_row作为Layout */ convertView = mInflater.inflate(R.layout.file_row, null); /* 初始化holder的text与icon */ holder = new ViewHolder(); holder.text = (TextView) convertView.findViewById(R.id.text); holder.icon = (ImageView) convertView.findViewById(R.id.icon); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } File f=new File(paths.get(position).toString()); /* 配置[回到根目次]的文字与icon */ if(items.get(position).toString().equals("b1")) { holder.text.setText("Back to /"); holder.icon.setImageBitmap(mIcon1); } /* 配置[回到上一层]的文字与icon */ else if(items.get(position).toString().equals("b2")) { holder.text.setText("Back to .."); holder.icon.setImageBitmap(mIcon2); } /* 配置[文件或文件夹]的文字与icon */ else { holder.text.setText(f.getName()); if(f.isDirectory()) { holder.icon.setImageBitmap(mIcon3); } else { holder.icon.setImageBitmap(mIcon4); } } return convertView; } /* class ViewHolder */ private class ViewHolder { TextView text; ImageView icon; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/8426.html