(libgdx学习)AndroidApplicationConfiguration和 Gdx.input.isPeripheralAvailable(Peripheral.)

自定义一个listView ,然后再new 出来的BaseAdapter的继承类中实现监听和获取position。然后自己封装了一个Adapter。其中借鉴了网上别人的代码,然后作了部分修改,做个mark。

借鉴地址:http://blog.csdn.net/xinqiqi123/article/details/6800437   谢谢这位





public class AddrItemInfoAdapter extends BaseAdapter
{
private InfoActivity infoActivity;
private AddrInfos addrInfos;
private LayoutInflater listContainer;
private Button curDel_btn;
private ImageView curEdit_Img;
private float x,ux;
private AddrInfo addrInfo;

private AddrListView addrListView;
private ListView listView;


final static class AddrListView
{
public TextView name;
public TextView tel;
public TextView addr;
public ImageView editIView;//自定义控件
public Button delBtn;//自定义控件
public int position;
}

public AddrItemInfoAdapter(InfoActivity infoActivity, AddrInfos addrInfos,ListView listView)
{
this.infoActivity = infoActivity;
this.addrInfos = addrInfos;

this.listContainer = LayoutInflater.from(infoActivity);
this.listView = listView ;
listView.setAdapter(this);
}

@Override
public int getCount()
{
return addrInfos.addrInfoList.size();
}

@Override
public Object getItem(int position)
{
return addrInfos.addrInfoList.get(position);
}

@Override
public long getItemId(int position)
{
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// AddrListView addrListView = null;

if (convertView == null)
{
addrListView = new AddrListView();
// 获取list_item布局文件的视图
convertView = listContainer.inflate(R.layout.info_addr_item_layout,
null);
// 获取控件对象
addrListView.name = (TextView) convertView
.findViewById(R.id.show_name);
addrListView.tel = (TextView) convertView
.findViewById(R.id.show_tel);
addrListView.addr = (TextView) convertView
.findViewById(R.id.show_addr);
addrListView.editIView = (ImageView) convertView
.findViewById(R.id.edit_imageview);
addrListView.delBtn = (Button) convertView
.findViewById(R.id.addr_del_bt);
// 设置控件集到convertView
convertView.setTag(addrListView);
convertView.setActivated(false);

}
else
{
addrListView = (AddrListView) convertView.getTag();
// 获取控件对象
addrListView.name = (TextView) convertView
.findViewById(R.id.show_name);
addrListView.tel = (TextView) convertView
.findViewById(R.id.show_tel);
addrListView.addr = (TextView) convertView
.findViewById(R.id.show_addr);
addrListView.editIView = (ImageView) convertView
.findViewById(R.id.edit_imageview);
addrListView.delBtn = (Button) convertView
.findViewById(R.id.addr_del_bt);

}
addrListView.position = position;  //获取每个对象中的positon
addrListView.name.setText((CharSequence) addrInfos.addrInfoList
.get(position).name);
addrListView.tel.setText((CharSequence) addrInfos.addrInfoList
.get(position).tel);
addrListView.addr
.setText((CharSequence) (addrInfos.addrInfoList.get(position).city + addrInfos.addrInfoList
.get(position).addr));

convertView.setOnTouchListener(new OnTouchListener() {

/*
* 实现滑动效果 滑动显示控件然后操作
*/
@Override
public boolean onTouch(View v, MotionEvent event)
{



final AddrListView addrListView = (AddrListView) v.getTag();
// 当按下时处理
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
// 设置背景为选中状态
// v.setBackgroundResource(R.drawable.mm_listitem_pressed);

x = event.getX();
// 判断之前是否出现了删除按钮如果存在就隐藏
if (curDel_btn != null||curEdit_Img != null)
{
curDel_btn.setVisibility(View.GONE);
curEdit_Img.setVisibility(View.GONE);
}
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{// 松开处理
// 获取松开时的x坐标
ux = event.getX();
// 判断当前项中按钮控件不为空时
if (addrListView.delBtn != null || 
addrListView.editIView != null)
{
// 按下和松开绝对值差当大于20时显示删除按钮,否则不显示
if (Math.abs(x - ux) > 20)
{
addrListView.delBtn.setVisibility(View.VISIBLE);
addrListView.editIView.setVisibility(View.VISIBLE);
curDel_btn = addrListView.delBtn;
curEdit_Img = addrListView.editIView;
}
}
}
return true;
}
});

addrListView.delBtn.setOnClickListener(new OnClickListener() {
private int position;

@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
int vid = v.getId();
AddrListView holder = getViewHolder(v);  
this.position = holder.position;
infoActivity.myAddr.delAddr(addrInfos.addrInfoList.get(position).id);
Toast.makeText(infoActivity, "id:"+addrInfos.addrInfoList.get(position).id, Toast.LENGTH_SHORT).show();
}
});
addrListView.editIView.setOnClickListener(new OnClickListener() {

private int position;
@Override
public void onClick(View v)
{
int vid = v.getId();
AddrListView holder = getViewHolder(v);  
this.position = holder.position;
infoActivity.showEditAddr(addrInfos.addrInfoList.get(position));
Toast.makeText(infoActivity, "position:"+position, Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
/*
* 获取view返回对应自定listview 然后进行比较
*/
public AddrListView getViewHolder(View v){  
 if (v.getTag() == null){  
   return getViewHolder((View) v.getParent());  
 }  
 return (AddrListView) v.getTag();  
}

}

     整个功能下来,原理就是,adapter在获取要显示的view的时候利用自定义的view中一个变量暂存position,然后再提取view的时候根据view的获取对应的自定义listview,最后获取自定义listview中的position。

(libgdx学习)AndroidApplicationConfiguration和 Gdx.input.isPeripheralAvailable(Peripheral.),布布扣,bubuko.com

(libgdx学习)AndroidApplicationConfiguration和 Gdx.input.isPeripheralAvailable(Peripheral.)

上一篇:Android - ToDoList(fragment) 详解


下一篇:【招聘】阿里巴巴-OS事业群-OS手机事业部-系统服务部门招聘Java开发工程师,有意者请进来