【问题标题】:Android list view setOnItemClickListener not workingAndroid列表视图setOnItemClickListener不起作用
【发布时间】:2013-10-09 21:51:04
【问题描述】:

我想最初在列表视图中隐藏编辑文本和按钮字段,并在单击该原始文件时在列表视图中显示特定原始文件的编辑文本和按钮。所以我尝试在布局 xml 中将高度设置为 0 和然后当用户点击原始数据时将其设置为其他值,但它不起作用我认为我的列表视图点击事件不起作用。 在我有列表视图的 Android 布局中,还有图像视图作为按钮、编辑文本字段和列表视图。如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainPortal" >

<ListView
    android:id="@+id/employeeListView"
    android:layout_width="650dp"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="25dp"
    android:clickable="true">
</ListView>

<EditText
    android:id="@+id/empPin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/iv_start_journey_btn"
    android:layout_marginBottom="56dp"
    android:layout_marginRight="17dp"
    android:layout_toLeftOf="@+id/employeeListView"
    android:ems="10"
    android:inputType="number"
    android:hint="@string/add_emp_pin_hint" >

    <requestFocus />
</EditText>

<ImageView
    android:id="@+id/iv_start_journey_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/employeeListView"
    android:layout_alignRight="@+id/empPin"
    android:layout_marginBottom="84dp"
    android:onClick="startJourney"
    android:src="@drawable/start" />

</RelativeLayout>

我为列表视图使用了以下自定义布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/emp_avatar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="18dp"
    android:adjustViewBounds="true"
    android:maxHeight="80dp"
    android:maxWidth="80dp"
    android:src="@drawable/person" />

<TextView
    android:id="@+id/emp_number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/emp_avatar"
    android:layout_toRightOf="@+id/emp_avatar"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/empPin"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_alignBottom="@+id/emp_avatar"
    android:layout_alignRight="@+id/emp_number"
    android:layout_toRightOf="@+id/emp_avatar"
    android:ems="10"
    android:inputType="number" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/empAdd"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_alignBaseline="@+id/empPin"
    android:layout_alignBottom="@+id/empPin"
    android:layout_marginLeft="20dp"
    android:layout_toRightOf="@+id/empPin"
    android:text="@string/emp_add_btn" />

这里贴出Activity.java的代码

public class MainPortal extends Activity {
private List<Employee> employees = new ArrayList<Employee>();
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_portal);
    populateEmployeeList();
    //populsteListView();

    ListView list = (ListView) findViewById(R.id.employeeListView);
    ArrayAdapter<Employee> adapter = new MylistAdapter();
    list = (ListView) findViewById(R.id.employeeListView);
    list.setAdapter(adapter);

     list.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {
//I ADDED ON CLICK IMPLEMENTATION HERE, BUT THIS IS NOT WORKING
              Toast.makeText(getApplicationContext(), "CLICKED", Toast.LENGTH_SHORT).show();   
              }
            });


private void populateEmployeeList() {
      ...
}



private class MylistAdapter extends ArrayAdapter<Employee>{


    public MylistAdapter(){
        super(MainPortal.this,R.layout.item_view,employees);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemView = convertView;
        if(itemView==null){
            itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
        }
        Employee currentEmployee = employees.get(position);
        //Avatar
        ImageView imageView = (ImageView) itemView.findViewById(R.id.emp_avatar);
        imageView.setImageResource(currentEmployee.getAvatarId());
        //Employee number
        TextView emp_id = (TextView) itemView.findViewById(R.id.emp_number);
        emp_id.setText(currentEmployee.getId());

        et = (EditText) itemView.findViewById(R.id.empPin);
        return itemView;
    }
}
}

上面我发布了一些我认为重要的代码。 任何人都可以帮我完成这项任务。 提前致谢。

【问题讨论】:

    标签: android android-layout android-listview android-emulator


    【解决方案1】:

    将以下属性添加到您的按钮

      android:focusable="false"
      android:focusableInTouchMode="false"
    

    ListView setOnItemClickListener not working by adding button

    您的edittext 是否专注于点击列表项?如果是这样,也请删除对 edittext 的关注。

    【讨论】:

    • @Grant 当您单击列表项时按钮获得焦点。检查相同的edittext。如果是这样,请从按钮和编辑文本中删除焦点
    • @Grant 你可以将它添加到xml中的根元素android:descendantFocusability="blocksDescendants"
    • 我在点击监听器的列表视图中设置了 EditText.setHeight(SOME VALUE)。但它不工作。有什么问题
    • 用于刷新列表视图。假设您更改了填充列表视图的基础数据,您在适配器上调用 notifyDataSetChanged() 以刷新 lsitview。另外为什么要动态更改edittext的高度。只需将其设置为wrap_content
    • 他们为什么需要在这里设置高度。您可以根据需要将视图可见性设置为不可见或消失
    【解决方案2】:

    这里列表视图中的编辑文本具有所有焦点,因此此列表视图 onClick 不起作用。从editext框中删除焦点,它将开始工作

    【讨论】:

      【解决方案3】:
      // try this
      public class MainPortal extends Activity {
          private List<Employee> employees = new ArrayList<Employee>();
          EditText et;
          ListView list;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main_portal);
              populateEmployeeList();
              //populsteListView();
      
              list = (ListView) findViewById(R.id.employeeListView);
              ArrayAdapter<Employee> adapter = new MylistAdapter();
              list.setAdapter(adapter);
      
              list.setOnItemClickListener(new OnItemClickListener() {
                  public void onItemClick(AdapterView<?> parent, View view,
                                          int position, long id) {
      //I ADDED ON CLICK IMPLEMENTATION HERE, BUT THIS IS NOT WORKING
                      Toast.makeText(getApplicationContext(), "CLICKED", Toast.LENGTH_SHORT).show();
                  }
              });
          }
      }
      

      【讨论】:

        【解决方案4】:

        在我的情况下,setOnItemClickListener 在 Android 版本 5.1.1 之前可以正常工作 但是在 Android 6.0.1 上。最后我找到了解决方案 试试这个
        从您的 XML 布局中删除这些行

        android:orientation="vertical"
        tools:context=".ManageRequest"
        android:contextClickable="true"
        

        适用于 Android 版本 6.0.1 希望您的问题得到解决..!!!快乐编码:)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多