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的元素永远也显示不出来。坑死我了,我开始没有意识到这个简单的问题。