OnLongClick stopping OnClick

北慕城南 提交于 2019-12-22 18:26:24

问题


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;
}
}

回答1:


I think you shouldn't do rowView.setOnLongClickListener.

Try something likes this:

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;
    }
});

I took the code from how to capture long press event for Listeview item of a ListActivity?

Hope this helps.



来源:https://stackoverflow.com/questions/15774408/onlongclick-stopping-onclick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!