请教一个关于Android开发ListView的问题
我的LV每条item都有一个button,我该怎样通过点击item上的button获得当前item的position呢?不是直接点击item,如果直接点击item可以通过...
我的LV每条item都有一个button,我该怎样通过点击item上的button获得当前item的position呢?不是直接点击item,如果直接点击item可以通过setOnItemClickListener去拿.
有懂的麻烦指导一下,谢谢! 展开
有懂的麻烦指导一下,谢谢! 展开
1个回答
2017-05-03
展开全部
在ListView的Item中有一个Switch控件,当Item过多屏幕装不下时,如果上下滑动使未显示的Item显示,这时开始和结尾的几个Item中的Switch控件会自动改变状态,并响应状态改变的事件。如果Item越多,会自动改变的Switch控件就越多。求解决方法,谢谢!
private class MAdapter extends CursorAdapter
{
private Context context;
private LayoutInflater mInflater;
public MAdapter(Context context, Cursor c)
{
super(context, c, true);
this.context = context;
mInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
View v = mInflater.inflate(R.layout.main_task_list_item, null);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
final MZTask task = new MZTask(cursor);
// ic
ImageView ic = (ImageView) view
.findViewById(R.id.main_list_action_ic);
ic.setImageResource(task.getIcId());
// action name
TextView actionName = (TextView) view
.findViewById(R.id.main_list_action_name);
actionName.setText(task.getActionName(context));
// time
TextView time = (TextView) view
.findViewById(R.id.main_list_task_time);
time.setText(task.getTimeString());
// repeat
TextView repeat = (TextView) view
.findViewById(R.id.main_list_task_repeat);
repeat.setText(task.getDayStringOfWeek(context));
// switch view
final Switch stch = (Switch) view
.findViewById(R.id.main_list_task_switch);
stch.setOnCheckedChangeListener(getCheckedChangeListener(task));
stch.setChecked(task.state());
// list more
RelativeLayout icLayout = (RelativeLayout) view
.findViewById(R.id.main_list_more);
icLayout.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
popUpMenu(task);
}
});
// ..
RelativeLayout itemLayout = (RelativeLayout) view
.findViewById(R.id.main_list_layout_switch);
itemLayout.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
stch.setChecked(!stch.isChecked());
}
});
}
public OnCheckedChangeListener getCheckedChangeListener(
final MZTask task)
{
return new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton button,
boolean change)
{
if (change)
task.startTask(context, dbAdapter);
else
task.cancelTask(context, dbAdapter);
}
};
}
}
private class MAdapter extends CursorAdapter
{
private Context context;
private LayoutInflater mInflater;
public MAdapter(Context context, Cursor c)
{
super(context, c, true);
this.context = context;
mInflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
View v = mInflater.inflate(R.layout.main_task_list_item, null);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor)
{
final MZTask task = new MZTask(cursor);
// ic
ImageView ic = (ImageView) view
.findViewById(R.id.main_list_action_ic);
ic.setImageResource(task.getIcId());
// action name
TextView actionName = (TextView) view
.findViewById(R.id.main_list_action_name);
actionName.setText(task.getActionName(context));
// time
TextView time = (TextView) view
.findViewById(R.id.main_list_task_time);
time.setText(task.getTimeString());
// repeat
TextView repeat = (TextView) view
.findViewById(R.id.main_list_task_repeat);
repeat.setText(task.getDayStringOfWeek(context));
// switch view
final Switch stch = (Switch) view
.findViewById(R.id.main_list_task_switch);
stch.setOnCheckedChangeListener(getCheckedChangeListener(task));
stch.setChecked(task.state());
// list more
RelativeLayout icLayout = (RelativeLayout) view
.findViewById(R.id.main_list_more);
icLayout.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
popUpMenu(task);
}
});
// ..
RelativeLayout itemLayout = (RelativeLayout) view
.findViewById(R.id.main_list_layout_switch);
itemLayout.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
stch.setChecked(!stch.isChecked());
}
});
}
public OnCheckedChangeListener getCheckedChangeListener(
final MZTask task)
{
return new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton button,
boolean change)
{
if (change)
task.startTask(context, dbAdapter);
else
task.cancelTask(context, dbAdapter);
}
};
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询