要求:单击ImageButton
中的ListView
将显示一个弹出窗口。根据弹出窗口的选择,将ImageButton
中的Listview
更改为其他ImageButton
。
问题是,当我单击另一个ImageButton
时,只有最后一个ImageButton
被更改。问题在于ImageButton
在ListView
中的位置没有被考虑。
如何通过获取选定的ImageButton
的位置来更改选定的ImageButton
?
代码如下:
public class FavouritesCellAdapter extends ArrayAdapter<String> {
private int layout;
private List<String> mObjects;
private RelativeLayout mRelativeLayout;
final ViewHolder viewHolder = new ViewHolder();
public FavouritesCellAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
mObjects = objects;
layout = resource;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder mainViewHolder = null;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(layout, parent, false);
mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.favorites_listview_small);
viewHolder.title = (TextView) convertView.findViewById(R.id.text1);
viewHolder.button = (ImageButton) convertView.findViewById(R.id.subscribe);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupDialog( viewHolder);
Log.v("Message", viewHolder.button + " was selected");
}
});
viewHolder.title.setText(getItem(position));
convertView.setTag(viewHolder);
}
else {
mainViewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
public class ViewHolder {
TextView title;
ImageButton button;
Button ok_button;
Button cancel_button;
CheckBox escalatedChkBx;
CheckBox priorityChkBx;
}
private void showPopupDialog(final ViewHolder viewHolder){
try{
LayoutInflater li = LayoutInflater.from(getContext());
final View prompt = li.inflate(R.layout.popup_dialog, null);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getContext());
viewHolder.ok_button = (Button)prompt.findViewById(R.id.ok_button);
viewHolder.cancel_button = (Button)prompt.findViewById(R.id.cancel_button);
viewHolder.escalatedChkBx = ( CheckBox ) prompt.findViewById( R.id.checkBox );
viewHolder.priorityChkBx = ( CheckBox ) prompt.findViewById( R.id.checkBox2 );
alertDialogBuilder.setView(prompt);
final AlertDialog show = alertDialogBuilder.show();
viewHolder.ok_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (viewHolder.escalatedChkBx.isChecked() && viewHolder.priorityChkBx.isChecked()) {
viewHolder.button.setImageResource(R.drawable.ic_email_inactive);
show.dismiss();
}
}
});
viewHolder.cancel_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show.dismiss();
}
});
alertDialogBuilder.show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果此条件成功,是否需要在此处传递ImageButton
的位置以设置其他ImageButton
?
if (viewHolder.escalatedChkBx.isChecked() && viewHolder.priorityChkBx.isChecked()) {
**viewHolder.button.setImageResource(R.drawable.ic_email_inactive);**
show.dismiss();
}
当我单击另一个时,只有最后一个ImageButton
发生了变化。
答案 0 :(得分:0)
您必须创建一个这样的界面:
public interface RecyclerViewOnClick {
void itemOnClick (View view, int position);
}
然后在适配器的视口中
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRecyclerViewOnClick.itemOnClick(v, position);
}
});
然后在您的活动中实施recyclerviewonclick
@Override
public void itemOnClick(View view, final int position) {
//you will got the item clicked position here.
//then you can validate to differentiate your image based on position right here
}
希望有帮助!
答案 1 :(得分:0)
将int位置属性添加到show popup方法中,并通过在getView中传递的位置获取视图持有者