• ListView Viewholder的坑 线性布局的坑


    1。ListView Viewholder的坑

        /**
         * 默认带图片的menu adapter
         */
        public static class MenuImageAdapter extends BaseAdapter {
            private List<Aliquot> mDataSet;
            private Context mContext;
    
            public MenuImageAdapter(List<Aliquot> dataSet, Context context) {
                this.mDataSet = dataSet;
                this.mContext = context;
            }
    
            @Override
            public int getCount() {
                return mDataSet == null ? 0 : mDataSet.size();
            }
    
            @Override
            public Object getItem(int position) {
                return mDataSet.get(position);
            }
    
            @Override
            public long getItemId(int position) {
                return position;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                MenuImageHolder holder = null;
                Aliquot aliquot = mDataSet.get(position);
                if (convertView == null) {
                    holder = new MenuImageHolder();
                    convertView = LayoutInflater.from(mContext).inflate(DEF_TEXT_IMAGE_ITEM_RES, null);
                    holder.mImageViewLeft = (ImageView) convertView.findViewById(R.id.img_menu_dialog_left);
                    holder.mImageViewRight = (ImageView) convertView.findViewById(R.id.img_menu_dialog_right);
                    holder.mParentRl = (RelativeLayout) convertView.findViewById(R.id.dialog_menu_parent_rl);
                    holder.mTextView = (TextView) convertView.findViewById(R.id.tv_menu_dialog);
                    holder.mVline = (View) convertView.findViewById(R.id.shelf_dilog_item_v_line);
                    convertView.setTag(holder);
                } else {
                    holder = (MenuImageHolder) convertView.getTag();
                }
                if (aliquot.mSrcLeftDrawableId != 0) {
                    holder.mImageViewLeft.setImageResource(aliquot.mSrcLeftDrawableId);
                    holder.mImageViewLeft.setVisibility(View.VISIBLE);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.mParentRl.getLayoutParams();
                    params.setMargins(15,0,0,0);
                }else{
                    holder.mImageViewLeft.setVisibility(View.GONE);
                }
    
                if (aliquot.mSrcRightDrawableId != 0) {
                    holder.mImageViewRight.setImageResource(aliquot.mSrcRightDrawableId);
                    holder.mImageViewRight.setVisibility(View.VISIBLE);
                }else{
                    holder.mImageViewRight.setVisibility(View.GONE);
                }
    
    //            if(position == mDataSet.size()-1){
    //                holder.mVline.setVisibility(View.INVISIBLE);
    //            }else{
    //                holder.mVline.setVisibility(View.VISIBLE);
    //            }
                holder.mTextView.setText(aliquot.mContent);
                return convertView;
            }
        }
    
        public static class MenuImageHolder {
            private TextView mTextView;//文字
            private ImageView mImageViewLeft;//图片在左侧
            private ImageView mImageViewRight;//图片在右侧
            private RelativeLayout mParentRl;
            private View mVline;//分割线
        }

    其中:

    if (aliquot.mSrcLeftDrawableId != 0) {
                    holder.mImageViewLeft.setImageResource(aliquot.mSrcLeftDrawableId);
                    holder.mImageViewLeft.setVisibility(View.VISIBLE);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) holder.mParentRl.getLayoutParams();
                    params.setMargins(15,0,0,0);
                }else{
                    holder.mImageViewLeft.setVisibility(View.GONE);
                }

    如果这里没有else的话,就会导致你的holder.mImageViewLeft 显示出来。因为你holder重用了嘛,用了holder之后,记得改状态。

    2。线性布局的坑

    水平线性布局如果被其中一个元素布满了(match_paraent),那么你写的后面的元素都不会显示出来了。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dialog_menutype_item_height"
        android:gravity="center_vertical"
        android:paddingLeft="@dimen/dialog_padding_left"
        android:orientation="horizontal">
    
        <ImageView
            android:id="@+id/img_menu_dialog_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />
    
        <RelativeLayout
            android:id="@+id/dialog_menu_parent_rl"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dialog_menutype_item_height"
            android:layout_marginLeft="15dp"
            >
    
            <TextView
                android:id="@+id/tv_menu_dialog"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:textSize="15sp"
                android:textColor="@color/primary_color_1"
                 />
    
            <View
                android:id="@+id/shelf_dilog_item_v_line"
                android:layout_width="match_parent"
                android:layout_height="2px"
                android:visibility="gone"
                android:layout_alignParentBottom="true"
                android:background="@color/zz_black_10_percent_transparent"
                ></View>
    
        </RelativeLayout>
    
        <ImageView
            android:id="@+id/img_menu_dialog_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
    
    
    </LinearLayout>

    这个布局中id 为img_menu_dialog_right的元素永远也显示不出来。坑死我了,我开始没有意识到这个简单的问题。

  • 相关阅读:
    分分钟提升命令行模式下密码输入逼格
    MySQL server has gone away 的两个最常见的可能性
    第一次遇到刷新缓冲区延时
    Mac上安装mysqlclient的报错
    python3 --- locale命名空间让程序更加安全了
    doctest --- 一个改善python代码质量的工具
    MySQL优化器 --- index_merge
    机智的MySQL优化器 --- is null
    Centos-7.x 下子网掩码的配置
    JS组件系列——BootstrapTable+KnockoutJS实现增删改查解决方案(三):两个Viewmodel搞定增删改查
  • 原文地址:https://www.cnblogs.com/caoxinyu/p/6647717.html
Copyright © 2020-2023  润新知