java - ListItem 不响应 Android ArrayAdapter 中的 clickEvents?

标签 java android listview android-button custom-arrayadapter

“当用户单击描述 TextView 和 Like ImageButton 时,我尝试创建 Toast 消息。但 list_item 未响应触摸事件”

“我询问了很多其他人关于改变焦点的问题,但他们都没有效果”

        EventsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final EventsObject mEventsObject = mEventsAdapter.getItem(position);
                final String mEventUrl = mEventsObject.geteLink();
                Log.e(TAG, "Inside ListVIew");
                final boolean status = mEventsObject.hasLiked();
                //LikeButton likeButton = view.findViewById(R.id.heart_button);
                TextView description = view.findViewById(R.id.eventDesc);
                //final TextView likesCountTextView = view.findViewById(R.id.likesCount);
                Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
                description.setFocusable(false);
                description.setFocusableInTouchMode(false);
                description.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!TextUtils.isEmpty(mEventUrl)) {
                            Intent openLinkInBrowser = new Intent(Intent.ACTION_VIEW);
                            openLinkInBrowser.setData(Uri.parse(mEventUrl));
                            startActivity(openLinkInBrowser);
                        } else {
                            Toast.makeText(MainActivity.this, "Links are not provided", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                Button loveBtn = view.findViewById(R.id.loveButton);
                loveBtn.setFocusable(false);
                loveBtn.setFocusableInTouchMode(false);
                loveBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (status) {
                            Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
                            mEventsObject.setHeartLiked(true);
                        } else {
                            Toast.makeText(MainActivity.this, "Disliked", Toast.LENGTH_SHORT).show();
                            mEventsObject.setHeartLiked(false);
                        }
                    }
                });

            }

list_item 的 XML 为

?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="8dp"
    android:orientation="vertical"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/organiser"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:paddingLeft="16dp"
            android:textAllCaps="true"
            android:textColor="#ffffff"
            android:textSize="16sp"
            android:textStyle="bold"
            tools:text="Organiser" />

        <TextView
            android:id="@+id/dateOfEvent"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:textColor="#ffffff"
            android:textStyle="bold"
            tools:text="12/03/20" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/organiserImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/events_circle"
            android:padding="16dp"
            android:src="@mipmap/ic_launcher"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/eventDesc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="4dp"
            android:gravity="fill"
            android:textColor="#ffffff"
            android:textSize="16sp"
            tools:text="@string/test_event_desc" />
    </LinearLayout>

    <ImageButton
        android:id="@+id/loveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="8dp"
        android:background="@drawable/events_love"

        android:scaleType="center"
        android:src="@drawable/love" />
    <!--
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_marginTop="0dp">

            <com.like.LikeButton
                app:icon_type="heart"
                app:icon_size="18dp"
                android:id="@+id/heart_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                app:circle_start_color="#ff2134"
                app:circle_end_color="#000000"

                />
    -->
    <!--<TextView
        android:id="@+id/likesCount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        tools:text="10"
        android:textColor="#ffffff"/>
</LinearLayout>-->

</LinearLayout>

“我想让 list_item 响应点击事件并显示 toast 消息.. 请帮忙.. 提前致谢!”

最佳答案

这可能是因为您试图在 ListView 的项目单击监听器内执行单击。

您可以通过为 ListView 创建自定义适配器来修复它。 Customer ListView Adapter

创建此自定义适配器后,您可以获取descriptionloveBtn的引用,并对其进行点击操作。

你的适配器的 getView() 代码将是这样的 -

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      if(convertView==null) {
         LayoutInflater layoutInflater = LayoutInflater.from(context);
         convertView=layoutInflater.inflate(R.layout.list_row, null);

         TextView description=convertView.findViewById(R.id.eventDesc);
         Button loveBtn=convertView.findViewById(R.id.loveButton);

      }

         description.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!TextUtils.isEmpty(mEventUrl)) {
                        Intent openLinkInBrowser = new Intent(Intent.ACTION_VIEW);
                        openLinkInBrowser.setData(Uri.parse(mEventUrl));
                        startActivity(openLinkInBrowser);
                    } else {
                        Toast.makeText(MainActivity.this, "Links are not provided", Toast.LENGTH_SHORT).show();
                    }
                }
            });

           loveBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (status) {
                        Toast.makeText(MainActivity.this, "Liked", Toast.LENGTH_SHORT).show();
                        mEventsObject.setHeartLiked(true);
                    } else {
                        Toast.makeText(MainActivity.this, "Disliked", Toast.LENGTH_SHORT).show();
                        mEventsObject.setHeartLiked(false);
                    }
                }
            });

      return convertView;
   }

关于java - ListItem 不响应 Android ArrayAdapter 中的 clickEvents?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56755845/

相关文章:

java - com.fasterxml.jackson.databind.JsonMappingException : Can not construct instance of : poja class no suitable constructor found

java - ChildFragmentManager java.lang.IllegalStateException : Activity has been destroyed

java - 我可以在 iBatis 上使用 subMap value = "X"和 subMap value = "!X"的 Discriminator 吗?

android - 如何解决显示垂直视口(viewport)的 flutter 抽屉被赋予无限高度

java - 调用 fragment Activity 错误无法实例化 Activity 。无法转换为 android.app.Activity

android - 在 android 中使用 viewpager 的具有多个 fragment 的 SearchView

java - Firebase 读取数据时出现空对象引用的 onDataChange 方法错误

android - Parse.com 相当于 SQL 连接

Android smoothScrollToPosition 位于顶部

android - Gesturelistener onFling 行为不一致