android - 单击事件不适用于 android 中的线性布局

标签 android android-layout listview

我面临的问题是我正在使用扩展 Baseadapter 的 ListView ,我需要在其中单击 Linearlayout,但我没有得到它的 clickevent。这是我的布局和我的适配器类。

<LinearLayout
                    android:id="@+id/zerokg"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@mipmap/bag_hover_bg"
                    android:orientation="vertical"
                    android:paddingTop="@dimen/margin_content_2"
                    android:gravity="center_horizontal|center_vertical"
                    android:layout_margin="5dp">
                    <com.nusatrip.view.TextViewPlus
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textColor="@color/white"
                        android:text="@string/zero_KG"
                        android:textSize="@dimen/content_textsize_1_"
                        app:customFont="@string/font_bold"
                        />
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">
                        <com.nusatrip.view.TextViewPlus
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="@string/curreny_name"
                            android:textColor="@color/white"
                            android:textSize="@dimen/small_textsixe_5"
                            app:customFont="@string/font_regular"/>
                        <com.nusatrip.view.TextViewPlus
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textColor="@color/white"
                            android:textSize="@dimen/content_textsize_3_"
                            android:text="@string/default_"
                            app:customFont="@string/font_semibold"/>
                    </LinearLayout>
                </LinearLayout>

我的适配器类。

        @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View vi = convertView;
                ViewHolder holder = null;
                LayoutInflater mInflater = (LayoutInflater)
                        mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.row_passenger_baggage_count, parent, false);
        holder.zerokg = (LinearLayout) convertView.findViewById(R.id.zerokg);
    holder.zerokg.setClickable(true);
         convertView.setTag(holder);
        }else {
                    holder = (ViewHolder) convertView.getTag();
                }
        final ViewHolder finalHolder = holder;
          holder.zerokg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finalHolder.zerokg.setBackgroundResource(R.mipmap.bag_hover_bg);
                        finalHolder.fifteenkg.setBackgroundResource(R.mipmap.bag_bg);
                        finalHolder.twentykg.setBackgroundResource(R.mipmap.bag_bg);
                        finalHolder.twentyfivekg.setBackgroundResource(R.mipmap.bag_bg);
                        finalHolder.thirtykg.setBackgroundResource(R.mipmap.bag_bg);
                        finalHolder.thirtyfive.setBackgroundResource(R.mipmap.bag_bg);
                        finalHolder.fourtykg.setBackgroundResource(R.mipmap.bag_bg);
                    }
                });
static class ViewHolder {
        LinearLayout  zerokg, fifteenkg, twentykg, twentyfivekg, thirtykg, thirtyfive, fourtykg;
     }

任何帮助将不胜感激

最佳答案

这是您必须在适配器中实现的 ViewHolder 类:

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public LinearLayout zerokg;
    public LinearLayout fifteenkg;
    public LinearLayout twentykg;
    public LinearLayout twentyfivekg;
    public LinearLayout thirtykg;
    public LinearLayout fourtykg;

        public ViewHolder(View itemView) {
            super(itemView);
            zerokg= (LinearLayout) itemView.findViewById(R.id.your_id);
            fifteenkg= (LinearLayout) itemView.findViewById(R.id.your_id);
            ...
           itemView.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            zerokg.setBackgroundResource(R.mipmap.bag_hover_bg);
                    fifteenkg.setBackgroundResource(R.mipmap.bag_bg);
                    twentykg.setBackgroundResource(R.mipmap.bag_bg);
                    twentyfivekg.setBackgroundResource(R.mipmap.bag_bg);
                    thirtykg.setBackgroundResource(R.mipmap.bag_bg);
                    thirtyfive.setBackgroundResource(R.mipmap.bag_bg);
                    fourtykg.setBackgroundResource(R.mipmap.bag_bg);
        }

}

关于android - 单击事件不适用于 android 中的线性布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36082980/

相关文章:

java - setAdapter和setDropDownViewResource是IDE用红色写的

Android Xml - 带有顶部、中心和底部 3 个 TextView 的 LinearLayout

android - 如何从 AsyncTask 向布局添加 View

c# - 检查现有项目不起作用

android - 如何使抽屉导航中的特定项目显示为选中状态?

c# - 使 ListView 在垂直方向上可滚动

android - Python 交叉编译问题

java - 动态创建 JSON 对象

java - ScrollView 上的 GridView(应用程序在其主线程上做了太多工作)

android - padding和android :gravity的区别