ListView setOnItemClickListener无法在自定义列表视图中工作

Kul*_*eep 5 android android-layout android-listview

我有一个列表视图,每行有两个文本视图和一个编辑文本,列表视图setOnItemClickListener()不起作用.

这里是我的Java代码.

public class CreateChallan extends Activity {


ListView lstCreate;

String[] strmainItemCode;
String[] strItem;
String[] strQuantity;
Context context=this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.createchallan);
    lstCreate= (ListView) findViewById(R.id.createlist);
    lstCreate.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);

    strmainItemCode= new String[]{"555551","255555","355555","455555","555555"};

    strItem =new String[]{"A","B","C","D","F"};

    strQuantity =new String[]{"100","200","30","400","500"};

    CreateAdapter adapter= new CreateAdapter(this, strmainItemCode, strItem, s trQuantity);

    lstCreate.setAdapter(adapter);

    lstCreate.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position1, long id) {
            // TODO Auto-generated method stub

            Toast.makeText(context, "Position",   Toast.LENGTH_LONG).show();

        }
    });
}







// Create List Adapter

class CreateAdapter extends ArrayAdapter<String>
 {
    TextView txtItecode, txtItem;
    EditText editQuantity;
    String[] strItecode;
    String[] strItem;
    String[] strQuantity;
    Context context;

    CreateAdapter(Context context, String[] strItemcode, String[] strItem,  String[] strQauntity)
    {
           super(context,R.layout.create_list_item,R.id.txtItemcode,strItemcode);
        this.context= context;
        this.strItecode= strItemcode;
        this.strItem= strItem;
        this.strQuantity= strQauntity;
    }
     public View getView(int position, View convertView, ViewGroup parent) {
         LayoutInflater mInflater = (LayoutInflater)  context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
         View row;
         row=mInflater.inflate(R.layout.create_list_item, parent,false);

         txtItecode= (TextView) row.findViewById(R.id.txtItemcode);
         txtItem =(TextView) row.findViewById(R.id.txtItem);
         editQuantity = (EditText)  row.findViewById(R.id.editcreateQuantity);

         txtItecode.setText(strItecode[position]);
         txtItem.setText(strItem[position]);
        editQuantity.setText(strQuantity[position]);

         txtItecode.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "click", Toast.LENGTH_LONG).show();
            }
        });

         return row;


     }
 }


}
Run Code Online (Sandbox Code Playgroud)

这里是我的列表xml代码

 <ListView
    android:id="@+id/createlist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   android:clickable="true"
    android:cacheColorHint="#00000000"
    android:divider="#adb8c2"
    android:dividerHeight="1dp"
    android:scrollingCache="false"
    android:smoothScrollbar="true" 
    android:focusable="false"
android:focusableInTouchMode="false"

   >

</ListView>
Run Code Online (Sandbox Code Playgroud)

请建议我如何解决这个问题.

提前致谢

Piy*_*ush 11

设置这些属性

 android:focusable="false"
 android:focusableInTouchMode="false"
Run Code Online (Sandbox Code Playgroud)

对于create_list_item xml文件中的所有UI元素.

还从ListView中删除该属性.

所以你的ListView将是

 <ListView
   android:id="@+id/createlist"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:clickable="true"
   android:cacheColorHint="#00000000"
   android:divider="#adb8c2"
   android:dividerHeight="1dp"
   android:scrollingCache="false"
   android:smoothScrollbar="true">
 </ListView>
Run Code Online (Sandbox Code Playgroud)

  • 这是可行的,但对于相关的“视图”来说,属性“android:clickable”必须为 false。注意亲们的“View”不要使用“android:clickable”。这将阻止您的列表或衍生产品的模式选择器。您可以使用属性`android:foreground`将模式选择器与自定义选择器匹配,如下所示:`&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:fitsSystemWindows=" true"&gt; &lt;FrameLayout "android:foreground="CUSTOM SELECTOR""&gt; &lt;RelativeLayout&gt;&lt;RelativeLayout/&gt; &lt;.../&gt; &lt;.../&gt;` (2认同)

Lun*_*ong 6

我认为,因为自定义列表项的布局中有Button或ImageButton。

因此,有两种解决方案,

  1. 将按钮替换为其他元素,然后在适配器代码中删除onClick侦听器。这是解决此问题的简便方法。

  2. 像这样禁用按钮焦点,

android:focusable="false"
android:focusableInTouchMode="false"
Run Code Online (Sandbox Code Playgroud)

通过添加按钮来引用此ListView setOnItemClickListener无效

玩得开心。@。@