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/

相关文章:

android - 测量要在 Canvas 上绘制的文本高度(Android)

安卓 : How to write text to specific area in an image

android - 如何设置位图大小以在 SurfaceView 上正确显示

java - Android:将ListView数据存储在Hashmap<String, String>的ArrayList中

c - 如何检测鼠标是否位于 ListView 控件中的项目/子项目上?

android - 从大型 XML 填充 ListView

android - AndEngine错误使用定时器

android - 适用于手机和平板电脑

android - 以灰色显示的菜单项图标颜色未显示其原始颜色

django - annotate() - 对另一列进行过滤的列的 Sum()