ListView在长按时设置可见的两个按钮

时间:2016-05-23 19:56:35

标签: android listview

我有ListView和ArrayAdapter的问题。我希望长按ListView项目时显示删除按钮。问题是我长按一次按钮会显示出来(取决于我在显示器上看到的项目数量。如果我处于纵向模式,我会看到,例如15中的前10个,当我长按项目时位置3,我将看到两个按钮,位置3和位置13.如果我处于横向模式,我会看到15个项目中的前4个,当我长按2时,我会看到位置2的按钮,6 ,10,14。)。但是,当我按下其他按钮时,我仍然会删除原始项目(长按项目)。这是我的代码示例。

TicketListAdapter

BorderStyle

list_ticket_view.xml

Width

ListFragment

public class TicketListAdapter extends ArrayAdapter<Ticket> {
Context context;
int layoutResourceId;
TabFragmentAdapter tabMenager;
List<Ticket> data = null;

public TicketListAdapter(Context context, int layoutResourceId, List<Ticket> objects, TabFragmentAdapter tabMenager) {
    super(context, layoutResourceId, objects);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = objects;
    this.tabMenager = tabMenager;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    TicketHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new TicketHolder();
        holder.imgIcon = (ImageView) row.findViewById(R.id.item_icon);
        holder.txtTitle = (TextView) row.findViewById(R.id.name);
        holder.txtGain = (TextView) row.findViewById(R.id.description);
        holder.btnDelete = (Button) row.findViewById(R.id.deleteTicketButton);
        holder.btnDelete.setOnClickListener(new DeleteTicketAction((Activity) context, getItemId(position), tabMenager, null, null));
        row.setTag(holder);
    } else {
        holder = (TicketHolder) row.getTag();
    }

    Ticket ticket = data.get(position);
    holder.txtTitle.setText(ticket.ticketName);
    holder.txtGain.setText("Possible gain: " + ticket.possibleGain);
    holder.imgIcon.setImageResource(StatusHelper.getStatusIconType(ticket.status.status, context));

    return row;
}

static class TicketHolder {
    ImageView imgIcon;
    TextView txtTitle;
    TextView txtGain;
    Button btnDelete;
}

@Override
public long getItemId(int position) {
    return data.get(position).getId();
}

0 个答案:

没有答案