本文介绍了OnLongClick停止OnClick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Listview.在实施OnLongClick之前,我的onListItemClick运转良好,但是现在,在实施OnLongClick之后,长按和常规列表单击都无济于事.似乎隐藏了您已经可以使用的onListItemClick()函数的隐患

I am using a Listview. before implementing OnLongClick, my onListItemClick was working perfectly, however now, after implementing OnLongClick the long clicks work and normal list clicks don't do anything. It seems to hide exposure to the onListItemClick() function you already have working

谁能看到为什么/建议解决方案?

can anyone see why/ suggest a solution?

public class CombChange extends ListActivity {
    @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

setListAdapter(new ListEdit(this, symbols));

@Override
 protected void onListItemClick(ListView l, View v, int position, long id) {

  String selectedValue = (String) getListAdapter().getItem(position);
  if (lastPressed.equals(selectedValue) ){
   count++;}
}

public class ListEdit extends ArrayAdapter<String> implements OnLongClickListener{
 private final Context context;
 private final String[] values;

 public ListEdit(Context context, String[] values) {
  super(context, R.layout.activity_comb_change, values);
  this.context = context;
  this.values = values;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  LayoutInflater inflater = (LayoutInflater) context
   .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  View rowView = inflater.inflate(R.layout.activity_comb_change, parent, false);
  TextView textView = (TextView) rowView.findViewById(R.id.label);
  ImageView imageView = (ImageView) rowView.findViewById(R.id.logo);
  textView.setText(values[position]);
  rowView.setOnLongClickListener(new OnLongClickListener(){

            public boolean onLongClick(View arg0) {
             context.startActivity(new Intent(context,RestoreOriginal.class));
                return false;
            }
        });
 // Change icon based on name
  String s = values[position];

  if (s.equals("a")) {
   imageView.setImageResource(R.drawable.a);

return rowView;
}
}

推荐答案

我认为您不应该 rowView.setOnLongClickListener .

I think you shouldn't do rowView.setOnLongClickListener.

尝试这样的事情:

this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
// whatever you wanna do
        return true;
    }
});

我从中获取了代码如何捕获ListActivity的Listeview项目的长按事件?

希望这会有所帮助.

这篇关于OnLongClick停止OnClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 22:07