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

时间:2016-03-18 11:16:04

标签: android android-layout listview

我面临的问题是我使用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;
     }

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

这是您必须在适配器中实现的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);
        }

}