ListView setOnItemClickListener不起作用

时间:2017-02-18 11:01:43

标签: android listview editview

我没有足够的知识来解决我的问题。 在我问之前,我一直在寻找答案并尝试了各种选择。

我有ListView和setOnItemClickListener:

新版本,只是ListItem和setOnItemClickListener 的 activity_main

<?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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin">


    <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
    android:id="@+id/listView">



    </ListView>
</RelativeLayout>

项目

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

    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name"
            android:ems="10"
            android:id="@+id/editText"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:focusable="false"/>
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Name"
            android:ems="10"
            android:id="@+id/editText1"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:focusable="false"/>
    <TextView
            android:text="TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            android:focusableInTouchMode="false"
            android:clickable="false"
            android:focusable="false"/>


</LinearLayout>

mainActivity,NoteItems,NoteItemsAdapter

    public class MainActivity extends Activity {


        ListView listView;
        List<NotesItems> list;
        private NoteItemsAdapter adapter;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            list= new ArrayList<NotesItems>();
            list.add(new NotesItems(0, "",",",",", ""));
            listView= (ListView) findViewById(R.id.listView);
            adapter = new NoteItemsAdapter(this, list);
            listView.setAdapter(adapter);


            listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                @Override
                public void onItemClick(AdapterView<?> adapter, View itemClicked, int position, long id) {
                    Toast.makeText(itemClicked.getContext(), "Work", Toast.LENGTH_LONG).show();
                }

            });

        }
    }

public class NotesItems {

    private long id;
    private String title;
    private String description;
    private String fileName;
    private String packageName;




    public NotesItems(int id, String title, String description, String fileName, String packageName){
        this.description = description;
        this.title = title;
        this.id = id;
        this.fileName=fileName;
        this.packageName = packageName;

    }



    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getPackageName(){return  packageName;}

    public String getNoteTitle() {
        return title;
    }

    public String getFileName() {return fileName;}





    public void setFileName(String fileName) {this.fileName = fileName;}

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

public class NoteItemsAdapter  extends BaseAdapter {

    Context context;
    private LayoutInflater layoutInflater;
    private List<NotesItems> items;

    public NoteItemsAdapter(Context view, List<NotesItems> list){
        this.context = view;
        this.items = list;
        this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
            return items.size();
    }

    @Override
    public Object getItem(int i) {
        return items.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {

        View view = convertView;
        if(view == null){
            view = layoutInflater.inflate(R.layout.item, viewGroup, false);
        }

        final NotesItems notesItems = getNoteItem(i);
        EditText editText1= (EditText)view.findViewById(R.id.editText);

        editText1.setText(notesItems.getDescription());


        EditText editText2= (EditText)view.findViewById(R.id.editText1);

        editText2.setText(notesItems.getNoteTitle());
        return view;
    }

    private  NotesItems getNoteItem(int position){
        return (NotesItems) getItem(position);
    }
}

我添加了TextView进行测试: 如果点击EditText - 没有任何反应, 如果单击TextView - toast出现。

我需要让EditText看起来像TextView - 当你点击listItem toast上的任何地方时必须出现。

3 个答案:

答案 0 :(得分:0)

尝试将此添加到您的Textviews和所有其他可点击元素,让您的适配器挂钩整个项目的所有点击。

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

答案 1 :(得分:0)

您正在执行onClick editText视图,该视图是listview中的子项。当你对列表视图的子项使用onClick时,onItemClick不会工作,因为项目的焦点完全集中在你的case中的editText的onClick项目上。

请参阅以下链接:

Focusable EditText in the ListView and onItemClick

答案 2 :(得分:0)

在我看来,这是因为您明确地将onClick侦听器设置为子视图。 将onClick侦听器用于List视图项内的子视图是什么意思? 它创造了一个模糊的状态。