etOnItemClickListener不工作在自定义列表视图

etOnItemClickListener不工作在自定义列表视图

本文介绍了setOnItemClickListener不工作在自定义列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了自定义的ListView 延长相对布局的每一行。

I have Implemented a custom ListView by extending Relative Layout for every row.

每一行都有复选框,TextView的, EDITTEXT 。一切工作正常。它维护它的状态。列表视图 OnItemClickListener 工作到具有行复选框,TextView中。但它停止工作,因为我行加入EDITTEXT。结果
任何帮助将鸭preciable。

Every row has Checkbox,Textview,Edittext. Everything working fine. It maintains it states. Listview OnItemClickListener working till row having Checkbox and Textview. But it stopped working as I added Edittext in row.
Any Help Will Be Appreciable.

这是我的code

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (convertView == null) {

        LayoutInflater inflator = context.getLayoutInflater();
        convertView = inflator.inflate(R.layout.takeso_row, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) convertView.findViewById(R.id.takesolabel);
        viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.takesocheck);
        viewHolder.edittext = (EditText) convertView.findViewById(R.id.takesoquantity);

        viewHolder.edittext.setClickable(true);
        viewHolder.edittext.setFocusable(true);

        viewHolder.edittext.setOnFocusChangeListener(new OnFocusChangeListener() {

            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                int getPosition = (Integer) v.getTag();

                if(!hasFocus){
                    //final int pos = position;
                    final EditText quantity = (EditText) v;
                    //Product prod = (Product)v.getTag();
                    if(quantity.getText().toString().length() == 0){
                        list.get(getPosition).setProdQuantity(0);
                    }else{
                        list.get(getPosition).setProdQuantity(Integer.parseInt(quantity.getText().toString()));
                    }
                }
            }
        });

        viewHolder.checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                int getPosition = (Integer) buttonView.getTag();  // Here we get the position that we have set for the checkbox using setTag.
                 list.get(getPosition).setIsChecked(buttonView.isChecked()); // Set the value of checkbox to maintain its state.

                 for(int i=0; i < passlist.size(); i++)
                 {
                     if(list.get(getPosition).getProductId().compareTo(passlist.get(i).getProductId()) == 0){
                         passlist.get(i).setIsChecked(isChecked);
                         //Log.d(passlist.get(i).getProductName(), passlist.get(i).getIsChecked().toString());
                     }
                 }
            }
        });

        convertView.setTag(viewHolder);
        convertView.setTag(R.id.label, viewHolder.text);
        convertView.setTag(R.id.check, viewHolder.checkbox);
        convertView.setTag(R.id.check, viewHolder.edittext);
    }else{
        viewHolder = (ViewHolder) convertView.getTag();
    }

    viewHolder.checkbox.setTag(position); // This line is important.
    viewHolder.edittext.setTag(position);
    viewHolder.text.setText(list.get(position).getProductName());
    viewHolder.checkbox.setChecked(list.get(position).getIsChecked());
    viewHolder.edittext.setText(list.get(position).getProdQuantity().toString());

    return convertView;
}`

XML code -

XML Code-

`

<CheckBox
android:id="@+id/takesocheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:focusable="false"
android:focusableInTouchMode="false">
</CheckBox>

<TextView
android:id="@+id/takesolabel"
android:layout_width="wrap_content"
android:layout_height="40sp"
android:textSize="25sp"
android:layout_toRightOf="@id/takesocheck">
</TextView>

<EditText android:id="@+id/takesoquantity"
    android:layout_alignParentRight="true"
    android:layout_width="120dip"
    android:layout_height="wrap_content"
    style="@style/EditText"
    android:inputType="number"
    android:layout_gravity="right"
    android:selectAllOnFocus="true"/>

<View android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@drawable/blue"
        android:layout_below="@id/takesocheck"/>

`

推荐答案

添加

android:focusable="false"

属性的复选框。这将窃取列表项点击停止复选框。

attribute to the CheckBox. This will stop the checkbox from stealing the List item clicks.

添加的android:descendantFocusability =beforeDescendants您的ListView这使得之前的ViewGroup任一后代获得焦点

Add android:descendantFocusability="beforeDescendants" to your ListView which makes the ViewGroup to get focus before any of its descendants.

这篇关于setOnItemClickListener不工作在自定义列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 09:49