Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击事件不适用于android中的linearlayout_Android_Android Layout_Listview - Fatal编程技术网

单击事件不适用于android中的linearlayout

单击事件不适用于android中的linearlayout,android,android-layout,listview,Android,Android Layout,Listview,我面临的问题是,我正在使用listview扩展Baseadapter,在这个问题中,我需要单击Linearlayout,但没有得到它的clickevent。这是我的布局和适配器类 <LinearLayout android:id="@+id/zerokg" android:layout_width="wrap_content" android:layout_height=

我面临的问题是,我正在使用listview扩展Baseadapter,在这个问题中,我需要单击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);
        }

}

为什么要使用multiple Holder???@AmarbirSingh,因为它是从必须声明为final的内部类访问的。因此使用了多个holder。我会尝试在ViewHolder类中而不是在getView方法中设置侦听器。@Carloshernandez-你能告诉我如何设置吗?不确定android:clickable=“true”在线性布局中如果你有疑问,下面是一个视频,让你了解如何实现ViewHolder: