ListView setOnItemClickListener无法通过添加按钮工作

Kis*_*ore 87 android listview

我有一个列表视图,每行有文本和按钮,列表视图setOnItemClickListener()不起作用.是否可以以不同方式处理项目点击和按钮点击事件(项目点击应调用ActivityA,按钮点击应调用ActivityB).有没有人有办法解决吗

    private ArrayList<String> userIDArr = null;
    private ArrayList<String> userNameArr = null;
    private DatabaseHelper dbHelper = null;
    private ListView userListView=null; 


    public void onCreate(Bundle savedInstanceState) 
        {
          super.onCreate(savedInstanceState);         
          setContentView(R.layout.list_view);         
          dbHelper = new DatabaseHelper(this.getApplicationContext());        
          Map<String,ArrayList<String>> displayMap = dbHelper.getUserListToDisplay();
          userIDArr = displayMap.get("UserID");
          userNameArr = displayMap.get("FirstName1");           


          userListView = (ListView) findViewById(R.id.listView2);
          userListView.setAdapter(new UserListAdapter(this,userIDArr));


          userListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                  Toast.makeText(usersListActivity.this,
                            "Item in position " + position + " clicked", Toast.LENGTH_LONG).show();
              }
            });
     }


    public class UserListAdapter extends ArrayAdapter<String>
    {
        Activity context;
        public UserListAdapter(Activity context, ArrayList<String> names) {
            super(context, R.layout.list_item, names);
            this.context = context;
        }
        private class ViewHolder {
            public TextView UserNameAndID;
            public TextView Description;
            public Button  UploadBtn;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            View rowView = convertView;
            if (rowView == null) {
                LayoutInflater inflater = context.getLayoutInflater();
                rowView = inflater.inflate(R.layout.list_item, null, true);
                holder = new ViewHolder();
                holder.UserNameAndID = (TextView) rowView.findViewById(R.id.User_detailsTxt);
                holder.Description = (TextView) rowView.findViewById(R.id.User_status);
                holder.UploadBtn = (Button) rowView.findViewById(R.id.uploadbutton);
                holder.UploadBtn.setOnClickListener(new View.OnClickListener() {  

                        public void onClick(View v) {  
                        Toast.makeText(usersListActivity.this," Button clicked",Toast.LENGTH_SHORT).show();
                        }   
                    }); 
                    rowView.setTag(holder);
            } else {
                holder = (ViewHolder) rowView.getTag();
            }
            String s = userNameArr.get(position)+","+userIDArr.get(position);
            holder.UserNameAndID.setText(s);
            holder.Description.setText("U r in middle");
            return rowView;
        }
    }
}`
Run Code Online (Sandbox Code Playgroud)

Ben*_*Lee 258

尝试设置按钮(或您要处理的任何其他视图在列表项内单击),如下所示:

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

  • ImageButton不起作用.在列表项视图中包含ImageButton,设置android:descendantFocusability ="blocksDescendants". (12认同)
  • 警告:如果您使用的是ImageButton,则需要使用setFocusable(false),因为ImageButton的构造函数会在从xml文件中膨胀后启用此属性.但是好的答案 (11认同)
  • 添加android:clickable ="false"也可能是必需的(在布局设计器中将其打开和关闭以将其显式添加到XML中) (8认同)

And*_*lva 116

有时列表仍然无法使Click Listener通过.在这种情况下,您可能需要再添加一个属性.

android:descendantFocusability="blocksDescendants" 
Run Code Online (Sandbox Code Playgroud)

此属性必须添加到您提供ListView元素的XML的最顶层布局中.


小智 14

如果您在列表视图中有一个活动的视图/可聚焦视图,那么它将禁用onItemClickListener...您可以通过向android:focusable="false"任何通常可聚焦的视图添加:来尝试使其无法 聚焦.


Adi*_*mro 8

击球手的事情是将两者都添加Listener到整个内部rowViewButton内部Adapter.像这样的东西.

public class MyAdapter extends BaseAdapter implements View.OnClickListener{

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
            View rowView = convertView;
            if(rowView == null)
            {
                    //intialize rowView, and set onclick listener only once.
                    rowView = someIntilizationMehhodOrInflatorMethod();
                    //add listener to the button also and also to the row view
                    rowView.setOnClickListener(this);
            }
            //all your inflation and setting values goes here and at the end,
            //set position as tag to get the correct position, rather buggy one.
            rowView.setTag(String.valueOf(position));


            return rowView;
    }
    public void onClick(View v)
    {
            //now get the tag of View v and convert it to integer.
            int pos = Integer.parseInt(v.getTag().toString());
            Toast.makeText(context,"Item in position " + pos + " clicked",Toast.LENGTH_LONG).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这甚至更好吗?您正在为每个数据元素创建一个onClickListener,而不是只有一个侦听器.你的例子甚至没有回收哪些观点. (3认同)

Vla*_*lad 5

尝试为每个Button,ImageButton等设置setClickable(false)并按如下方式查看:

    view.setClickable(false);
    button.setClickable(false);
    imagebutton.setClickable(false);
Run Code Online (Sandbox Code Playgroud)

另外你还必须添加

android:descendantFocusability="blocksDescendants"
Run Code Online (Sandbox Code Playgroud)

到主(第一层)布局

  • 我发现 set blocksDescendants 就足够了,不需要取消 focusable 等 Android 4.1 (3认同)