在列表视图按钮上单击,日志显示:“ViewPostImeInputStage ACTION_DOWN”

时间:2017-08-10 18:05:57

标签: android listview android-studio button logging

我已经看到很多人遇到过这个问题,但提供给他们的解决方案(比如将布局放在另一个布局中)不起作用。因此,我决定上传自己的代码,希望有人知道它有什么问题。我有一个listview,每行有2个按钮(subtract_button& add_button),使用以下适配器类显示:

public class AdapterSIUsed2 extends CursorAdapter implements View.OnClickListener {

private RowViewHolder rowView = new RowViewHolder();

public static class RowViewHolder {
    public TextView name,used,category,amount;
    public Button subtract,add;
    public int position;
}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case (rowView.add.getId()):
            // Do something
            Log.d("Button clicked","add");
            return;
        case (rowView.subtract.getId()):
            Log.d("Button clicked","subtract");
            return;
    }
}

public AdapterSIUsed2(Context context, Cursor cursor) {
    super(context, cursor, 0);
}

@Override
public View newView(Context context, Cursor csr, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View listView = inflater.inflate(R.layout.listlayout_shopping_items_used, null);
    rowView.name = (TextView) listView.findViewById(R.id.shopping_items_name);
    rowView.used = (TextView) listView.findViewById(R.id.shopping_items_used);
    rowView.category = (TextView) listView.findViewById(R.id.shopping_items_category);
    rowView.amount = (TextView) listView.findViewById(R.id.shopping_items_amount);
    rowView.subtract = (Button) listView.findViewById(R.id.subtract_button);
    rowView.add = (Button) listView.findViewById(R.id.shopping_items_add_button);
    rowView.position = csr.getPosition();
    rowView.subtract.setOnClickListener(this);
    rowView.add.setOnClickListener(this);
    listView.setTag(rowView);
    rowView.name.setTag(rowView);
    rowView.used.setTag(rowView);
    rowView.category.setTag(rowView);
    rowView.amount.setTag(rowView);
    return listView;
}

@Override
public void bindView(View view, Context context, Cursor csr) {
    TextView name = (TextView) view.findViewById(R.id.shopping_items_name);
    TextView used = (TextView) view.findViewById(R.id.shopping_items_used);
    TextView category = (TextView) view.findViewById(R.id.shopping_items_category);
    TextView amount = (TextView) view.findViewById(R.id.shopping_items_amount);

    name.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_1)));
    used.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_2)));
    category.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_3)));
    amount.setText(csr.getString(csr.getColumnIndex(Constants.SHOPPING_ITEMS_COL_4)));

    Button add = (Button) view.findViewById(R.id.shopping_items_add_button);
    add.setOnClickListener(this);
    Button subtract = (Button) view.findViewById(R.id.subtract_button);
    subtract.setOnClickListener(this);

    int pos = csr.getPosition();
    add.setTag(pos);
    subtract.setTag(pos);
}

奇怪的是,当我单击添加或减去按钮时,在这两种情况下,我的日志显示以下两行: D / ViewRootImpl:ViewPostImeInputStage ACTION_DOWN 我/项目点击::( 当我单击列表项本身时,我的日志只显示以下行: D / ViewRootImpl:ViewPostImeInputStage ACTION_DOWN

有谁知道这里发生了什么以及如何解决?提前谢谢!

1 个答案:

答案 0 :(得分:0)

ACTION_DOWN只是意味着某些东西被压了。它与ACTION_UP有关,这意味着某些东西不再受到压迫。

显然,您按下的按钮是您在bindView中设置的按钮。当您单击列表项本身时,您不清楚为什么会出现这种行为;您似乎没有在单元格本身或任何类型的父onClickListeners上设置任何ViewGroup。您可能需要仔细检查并确保您的布局ID是它们应该是的。任何使用它作为适配器的东西都可能搞乱了。

您似乎从接口实现中覆盖了onClick方法,从而获得了正确的行为。即使它为您提供了额外的信息,它也会记录您要求的内容("项目点击::(")。我建议只使用它,并将日志更改为使用Log.d代替(Log.d专门用于调试目的。)你也可以这样做:

    @Override
    public void onClick(View v) {
        if (v.getId() == rowView.add.getId()) {
            //do something;
            return;
        if (v.getId() == rowView.subtract.getId()) {
            //do something;
            return;
        }
    }
});

然后你就做了

rowView.add.setOnClickListener(this);
rowView.subtract.setOnClickListener(this);