0

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

我有 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 - 出现吐司。

我需要使 EditText 看起来像 TextView - 当您单击 listItem 上的任意位置时,必须出现吐司。

4

3 回答 3

0

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

android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
于 2017-02-20T11:31:04.833 回答
0

您正在对作为列表视图中的子项的 editText 视图执行 onClick。当您在列表视图的子项目上使用 onClick 时,onItemClick 将不起作用,因为该项目的焦点完全集中在您的情况下 editText 的 onClick 项目上。

请参考以下链接:

ListView 和 onItemClick 中的可聚焦 EditText

于 2017-02-20T11:54:46.867 回答
0

在我看来,这是因为您明确地将 onClick 侦听器设置为子视图。让 onClick 侦听器在列表视图项内进行子视图是什么意思?它创造了一个模棱两可的状态。

于 2017-02-20T12:00:24.327 回答