OnItemClickListener不起作用,但OnLongItemClickListener在自定义列表视图中工作

时间:2015-05-12 11:34:43

标签: java android xml listview android-listview

我有一个带自定义适配器的自定义listview。我想点击listview的项目并执行某些操作。 OnItemClickListener不起作用。但是我实施了OnLongItemClickListener,它运作得很好。

MainActivity

public class MainActivity extends Activity {
ArrayList<Product> products = new ArrayList<Product>();
Adapter listviewAdapter;  //custom adapter object
ListView listview; 
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listview = (ListView) findViewById(R.id.lvMain);
    listview.setLongClickable(true);
    listviewAdapter = new Adapter(this, products);      
    listview.setAdapter(listviewAdapter);    
   listview.setOnItemLongClickListener(new OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {  //this works
        Toast.makeText(getApplicationContext(), "Long pressed", Toast.LENGTH_SHORT).show();
        return false;
    }
});      
   listview.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {    //does not work
        Toast.makeText(getApplicationContext(), " pressed", Toast.LENGTH_SHORT).show();
    }
});     
}

更新自定义适配器适配器

public class Adapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
TextView itemname,itemprice;
Adapter(Context context, ArrayList<Product> products) {
    ctx = context;
    objects = products;
    lInflater = (LayoutInflater) ctx
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    return objects.size();
}
@Override
public Object getItem(int position) {
    return objects.get(position);
}
@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.item, parent, false);
    }
    Product p = getProduct(position);
     itemname= ((TextView) view.findViewById(R.id.tvDescr));
     itemname.setText(p.name);
     itemprice=((TextView) view.findViewById(R.id.tvPrice));
     itemprice.setText(p.price + "");
    CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
    cbBuy.setOnCheckedChangeListener(myCheckChangList);
    cbBuy.setTag(position);
    cbBuy.setChecked(p.selected);       
    view.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

        }
    });       
    view.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            return false;
        }
    });     
    return view;
}
Product getProduct(int position) {
    return ((Product) getItem(position));
}
ArrayList<Product> getBox() {
    ArrayList<Product> selected = new ArrayList<Product>();
    for (Product p : objects) {
        if (p.selected)
            selected.add(p);
    }
    return selected;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
        getProduct((Integer) buttonView.getTag()).selected = isChecked;
    }
};

}

自定义列表视图 item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" 
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="@+id/cbBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" >
</CheckBox>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_height="wrap_content"
 android:orientation="vertical" >
<TextView
android:id="@+id/tvDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""  >
    </TextView> 
</LinearLayout>

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"  >
<ListView
android:id="@+id/lvMain"
android:layout_width="match_parent"
android:layout_height="0dp"
android:longClickable="true">
</ListView>

4 个答案:

答案 0 :(得分:1)

这是因为你的CheckBox。你可以这样解决:

为您的Linearlayout添加ID。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" 
android:descendantFocusability="blocksDescendants"
android:id="@+id/main_layout">

在你的getView

LinearLayout main_layout = (LinearLayout)view.findViewById(R.id.main_layout));

main_layout.setOnClickListener(new OnClickListener() {

                            public void onClick(View v) {
                                listview .performItemClick(view,
                                        listview.getPositionForView(view),
                                        listview.getPositionForView(view));

                            }
                        });

编辑: 使用以下代码进行长按

main_layout.setOnLongClickListener(new OnLongClickListener() {

                            @Override
                            public boolean onLongClick(View arg0) {
                                listview.performLongClick();
                                return true;
                            }
                        });

答案 1 :(得分:1)

到您的文字视图和复选框

android:focusable="false"

答案 2 :(得分:1)

您可以在xml中提供android:onClick属性:

<CheckBox
android:id="@+id/cbBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:onClick="onCheckboxClicked" />

当您选中或取消选中复选框时,此功能将起作用。

原因为什么onClick不起作用,但onLongClick有效:

您已在列表视图行上应用onListItemClickListener,因此当您点击复选框时,此侦听器会使用该事件

listview.setOnItemClickListener(new OnItemClickListener() {

更新:

getView()中的

    // for list row
 view.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View arg0) {
            Log.d("onClick","row click");

        }
});   

    // for list row check box
 view.cbBuy.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View arg0) {
              Log.d("onClick","checkbox click");

        }
}); 

在MainActivity中

删除此部分:

/*   listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {    //does not work
            Toast.makeText(getApplicationContext(), " pressed", Toast.LENGTH_SHORT).show();
        }
    }); */

复选框是否具有属性android:focusable="true"false此代码将运行 这是编译和运行代码 谢谢

答案 3 :(得分:0)

@Amsheer您的代码有效。但为此,您必须更改自定义适配器并在其中编写onClick个事件的操作。我想在ManiActivity中编写onClick个事件。我找到了一个解决方法。我从自定义适配器

中删除了它
 view.setOnLongClickListener(new OnLongClickListener() {        
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            return false;
        }
    });
view.setOnClickListener(new OnClickListener() { 
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub  
    }
});