1.android中,adapter怎么写/** * 适配器示例 */public class SpeedWholeAdapter extends BaseAdapter { private HashMap positionMap = new HashMap(); private WeakReference weak; private List mDataSource; private LayoutInflater mInflater; public SpeedWholeAdapter(Context context, List infos) { weak = new WeakReference((SpeedWholeActivity)context); mInflater = LayoutInflater.from(context); if (infos == null) { mDataSource = new ArrayList(); } else { mDataSource = infos; } } @Override public int getCount() { if (mDataSource == null) { return 0; } return mDataSource.size(); } @Override public Object getItem(int position) { if(position > mDataSource.size()-1){ return null; } return mDataSource.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ApkItem info = mDataSource.get(position); if(info == null) return convertView; ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(R.layout.commom_gridview_item, null); holder.icon = (ImageView)convertView.findViewById(R.id.iv_commom_gridview_icon); holder.name = (TextView)convertView.findViewById(R.id.tv_commom_gridview_name); holder.progress = (CoverView)convertView.findViewById(R.id.cv_progress); holder.progress.setDrawableId(R.drawable.progress_fg, R.drawable.progress_bg, 0, 0); convertView.setTag(holder); } else { holder = (ViewHolder)convertView.getTag(); } // 绑定数据 positionMap.put(info.getPackName(), position); BitmapUtil.loadImageBitmap(weak.get(), info.getIconDownloadPath(), holder.icon, R.drawable.ic_launcher); holder.name.setText(info.getApkName()); return convertView; } /** 根据包名获取下载信息 */ public ApkItem getInfo(String pkgName) { if(positionMap.containsKey(pkgName)) { int position = positionMap.get(pkgName); return (ApkItem) getItem(position); } else { return null; } } /** 添加数据 */ public void addData(List data) { if (data != null && data.size() > 0) { mDataSource.clear(); mDataSource.addAll(data); notifyDataSetChanged(); } }class ViewHolder { CoverView progress; ImageView icon; TextView name; }} 。
2.recyclerview复杂布局 的adapter怎么写异同比较: 相同点:都可以实现垂直方向的滚动列表效果;都需要使用适配器(Adapter)不同点:ListView只能实现垂直滚动列表 , 但RecyclerView还可以实现水平、多列、跨列等复杂的滚动列表;RecyclerView不但需要Adapter , 还必须有LayoutManager , 用法更复杂一些 。
总结:ListView能做到的 , RecyclerView都能做到 , 反之则不行 。RecyclerView用法比ListView复杂 。
一般用法:ListView用法ListView listView = (ListView) findViewById(R.id.list_view);listView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1,getData()));RecyclerView用法recyclerView = findView(R.id.id_recyclerview);//设置布局管理器recyclerView.setLayoutManager(layout);//设置adapterrecyclerView.setAdapter(adapter) ;//设置Item增加、移除动画recyclerView.setItemAnimator(new DefaultItemAnimator());//添加分割线recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL_LIST)); 。
3.android 里怎么用adapter写下拉菜单选择所使用的工具:电脑开发工具:eclipse for Android , Android Studio用adapter 写下拉菜单需要我们自己进行自定义 , 具体步骤如下:1、自定义类 , 类成员与需要显示的字段一致;public class Manufacture {private String name ;public Manufacture(String name) {super();this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}}2、自定义adapter并继承自BaseAdapter实现相应方法 , 下面是贴出的代码示例:public class MnufactureAdapter extends BaseAdapter{private List mList;private Context context ;//创建有参构造 , 参数需要传入两个1.自定义类类型的List , 上下文参数public MnufactureAdapter(List mList, Context context) {super();this.mList = mList;this.context = context;}//返回长度位所定义List的长度@Overridepublic int getCount() {return mList.size();}//返回位置为自定义list当前位置@Overridepublic Object getItem(int position) {return mList.get(position);}//返回条目的[email protected] long getItemId(int position) {return position;}//这里getView需方法需要重点说明和注意:@Overridepublic View getView(int position, View convertView, ViewGroup parent) {//创建一个LayoutInflater并传入上下文LayoutInflater linflater = LayoutInflater.from(context);//添加自定义item布局到convertView;(item内容和样式通过自定义方式实现)convertView = linflater.inflate(R.layout.spinner_item_layout, null);//初始化Item中控件TextView textView =(TextView) convertView.findViewById(R.id.manufacture_list);//设置控件显示内容textView.setText(mList.get(position).getName());return convertView;}}3、在activity中需要添加如下代码://初始化布局文件中的spinner Spinner mSpinner = (Spinner) findViewById(R.id.spinner1);//初始化自定义的List , 并往里面添加数据List manufacture = new ArrayList();接着就是添加数据 , 在项目中我是添加的从网络上下载的数据解析之后进行添加的 , 具体的添加方式根据具体需要进行添加 , 我把自己的贴在下面供大家参考:for(int i = 0;i 评论0 0 0 。