Android - 关于Adapter的使用
2018-01-03 本文已影响85人
Allens_Jiang
老婆保佑,代码无BUG
前言
首先说一下,为什么我想写这个文章,实际项目中,一般我都会使用自己封装的BaseAdater,或者鸿洋大神的万能适配器,这就导致了,很多基础的用法,知道是那么回事,但是每次写,总感觉很手生,所以,这里坐下记录,也方便小伙伴们查阅
目录
- 一:ArrayAdapter
- 二: SimpleAdapter
- 三:BaseAdapter
- 四:RecyclerAdapter
- 五: 张鸿洋 万能适配器
一:ArrayAdapter
public class MainActivity extends Activity {
private ListView lv;//控件引用声明
private List<String> list;//数据源集合引用声明
private ArrayAdapter<String> adapter;//适配器声明
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);//找出ListView对象
list = new ArrayList<String>();//数据源集合创建
for(int i=1;i<=50;i++){
list.add(Integer.toString(i));//往数据源集合中添加50个字符串
}
//创建适配器,第一个参数上下文,第二个是单项的布局,第三个是数据源,这里已经将数据源整合到适配器
adapter = new ArrayAdapter<String>(this, R.layout.my_item, list);//用自己定义的单项布局
// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);//再将适配器组装到ListView控件上
}
}
二: SimpleAdapter
//创建简单适配器对象,第一个参数上下文,第二个数据源,第三个单项布局
//第四个表示要展现在ListView单项视图中的数据的键,是一个字符串数组
//第五个表示展示上述数据的控件的id,是一个int数组
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.item,
new String[]{"name","age"}, new int[]{R.id.textView1,R.id.textView2});
//位于第一个的name的值放到同样第一个的textView1中,位于第二个的age的值放到同样第二个的textView2中
三:BaseAdapter
public class MyAdapter extends BaseAdapter
{
private LayoutInflater mInflater;
private Context mContext;
private List<String> mDatas;
public MyAdapter(Context context, List<String> mDatas)
{
mInflater = LayoutInflater.from(context);
this.mContext = context;
this.mDatas = mDatas;
}
@Override
public int getCount()
{
return mDatas.size();
}
@Override
public Object getItem(int position)
{
return mDatas.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder = null;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.item_single_str, parent,
false);
viewHolder = new ViewHolder();
viewHolder.mTextView = (TextView) convertView
.findViewById(R.id.id_tv_title);
convertView.setTag(viewHolder);
} else
{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mTextView.setText(mDatas.get(position));
return convertView;
}
private final class ViewHolder
{
TextView mTextView;
}
}
四:RecyclerAdapter
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
/**
* 展示数据
*/
private ArrayList<String> mData;
/**
* 事件回调监听
*/
private MyAdapter.OnItemClickListener onItemClickListener;
public MyAdapter(ArrayList<String> data) {
this.mData = data;
}
public void updateData(ArrayList<String> data) {
this.mData = data;
notifyDataSetChanged();
}
/**
* 添加新的Item
*/
public void addNewItem() {
if(mData == null) {
mData = new ArrayList<>();
}
mData.add(0, "new Item");
notifyItemInserted(0);
}
/**
* 删除Item
*/
public void deleteItem() {
if(mData == null || mData.isEmpty()) {
return;
}
mData.remove(0);
notifyItemRemoved(0);
}
/**
* 设置回调监听
*
* @param listener
*/
public void setOnItemClickListener(MyAdapter.OnItemClickListener listener) {
this.onItemClickListener = listener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// 实例化展示的view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_rv_item, parent, false);
// 实例化viewholder
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
// 绑定数据
holder.mTv.setText(mData.get(position));
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if(onItemClickListener != null) {
int pos = holder.getLayoutPosition();
onItemClickListener.onItemClick(holder.itemView, pos);
}
}
});
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if(onItemClickListener != null) {
int pos = holder.getLayoutPosition();
onItemClickListener.onItemLongClick(holder.itemView, pos);
}
//表示此事件已经消费,不会触发单击事件
return true;
}
});
}
@Override
public int getItemCount() {
return mData == null ? 0 : mData.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView mTv;
public ViewHolder(View itemView) {
super(itemView);
mTv = (TextView) itemView.findViewById(R.id.item_tv);
}
}
public interface OnItemClickListener {
void onItemClick(View view, int position);
void onItemLongClick(View view, int position);
}
}