我有多个项目的listview。每行包含zig zag方式的图像和文本我检查位置if((位置%2)== 0)然后我将图像附加到左边,文本附加到右边,否则我将图像设置为右边,文本设置为左边。它工作正常但当我向上滚动或向下滚动然后我得到序列的问题。我需要这样的列表视图
但是当我滚动到listview然后我变得像这样
public View getView(final int position, View view, ViewGroup parent) {
final ViewHolder holder;
if (view == null || view.getTag() == null) {
holder = new ViewHolder();
view = inflater.inflate(R.layout.adapter_hotel_list_left_layout, null);
// get total display height and width
WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
// divide total width by 2 and store it into newWidth variable
int newWidth = width / 2;
Log.d("Width-NewWidth", "" + width + "-" + newWidth);
int marginLeftSide = 0, marginRightTopBottomSide = 0;
// if Utility.deviceTypeFlag == "Tab" then user device is tab then set margins according to tab
if (Utility.deviceTypeFlag.equalsIgnoreCase("Tab")) {
float density = mContext.getResources().getDisplayMetrics().density;
int marginLeft = 15;
marginLeftSide = (int) (marginLeft * density);
int marginRightTopBottom = 8;
marginRightTopBottomSide = (int) (marginRightTopBottom * density);
newWidth = newWidth - (marginRightTopBottomSide * 3);
} else {
// set margins accroding to smartphone.
float density = mContext.getResources().getDisplayMetrics().density;
int marginLeft = 10;
marginLeftSide = (int) (marginLeft * density);
int marginRightTopBottom = 5;
marginRightTopBottomSide = (int) (marginRightTopBottom * density);
newWidth = newWidth - (marginRightTopBottomSide * 3);
}
// if Utility.deviceTypeFlag == "Tab" then user device is tab then set margins according to tab
if (Utility.deviceTypeFlag.equalsIgnoreCase("Tab")) {
int direction = Integer.parseInt(arrayListHotelDetails.get(position).get("directionFlag"));
if ((position % 2) == 0) {
Log.d("position", String.valueOf(position));
// check position if it is even then set hotel image to left and hotel name to right.
holder.relativeLayout_Image = (RelativeLayout) view.findViewById(R.id.relativelayout_hll);
holder.imageviewHotel = (ImageView) view.findViewById(R.id.imageview_Hotel_Name_ahl);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
layoutParams.setMargins(marginLeftSide, marginLeftSide, marginRightTopBottomSide, 0);
holder.relativeLayout_Image.setLayoutParams(layoutParams);
holder.relativeLayout_Text = (RelativeLayout) view.findViewById(R.id.relativelayout2_hll);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
layoutParams1.setMargins(marginRightTopBottomSide, marginLeftSide, marginLeftSide, 0);
layoutParams1.addRule(RelativeLayout.RIGHT_OF, R.id.relativelayout_hll);
holder.relativeLayout_Text.setLayoutParams(layoutParams1);
holder.textViewHotelName = (TextView) view.findViewById(R.id.textview_Hotel_Name);
} else {
// else set hotel image to right and hotel name textview to left.
holder.relativeLayout_Image = (RelativeLayout) view.findViewById(R.id.relativelayout_hll);
holder.imageviewHotel = (ImageView) view.findViewById(R.id.imageview_Hotel_Name_ahl);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
layoutParams.setMargins(marginRightTopBottomSide, marginLeftSide, marginLeftSide, 0);
layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.relativelayout2_hll);
holder.relativeLayout_Image.setLayoutParams(layoutParams);
holder.relativeLayout_Text = (RelativeLayout) view.findViewById(R.id.relativelayout2_hll);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
layoutParams1.setMargins(marginLeftSide, marginLeftSide, marginRightTopBottomSide, 0);
holder.relativeLayout_Text.setLayoutParams(layoutParams1);
holder.textViewHotelName = (TextView) view.findViewById(R.id.textview_Hotel_Name);
holder.textViewHotelName.setGravity(Gravity.RIGHT);
}
} else {
int direction = Integer.parseInt(arrayListHotelDetails.get(position).get("directionFlag"));
// set margins according to smartphone
if ((position % 2) == 0) {
Log.d("position", String.valueOf(position));
// check position if it is even then set hotel image to left and hotel name to right.
holder.relativeLayout_Image = (RelativeLayout) view.findViewById(R.id.relativelayout_hll);
holder.imageviewHotel = (ImageView) view.findViewById(R.id.imageview_Hotel_Name_ahl);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
layoutParams.setMargins(marginLeftSide, marginRightTopBottomSide, marginRightTopBottomSide, marginRightTopBottomSide);
holder.relativeLayout_Image.setLayoutParams(layoutParams);
holder.relativeLayout_Text = (RelativeLayout) view.findViewById(R.id.relativelayout2_hll);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
layoutParams1.setMargins(marginRightTopBottomSide, marginRightTopBottomSide, marginLeftSide, marginRightTopBottomSide);
layoutParams1.addRule(RelativeLayout.RIGHT_OF, R.id.relativelayout_hll);
holder.relativeLayout_Text.setLayoutParams(layoutParams1);
holder.textViewHotelName = (TextView) view.findViewById(R.id.textview_Hotel_Name);
} else {
// else set hotel image to right and hotel name textview to left.
holder.relativeLayout_Image = (RelativeLayout) view.findViewById(R.id.relativelayout_hll);
holder.imageviewHotel = (ImageView) view.findViewById(R.id.imageview_Hotel_Name_ahl);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
layoutParams.setMargins(marginRightTopBottomSide, marginRightTopBottomSide, marginLeftSide, marginRightTopBottomSide);
layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.relativelayout2_hll);
holder.relativeLayout_Image.setLayoutParams(layoutParams);
holder.relativeLayout_Text = (RelativeLayout) view.findViewById(R.id.relativelayout2_hll);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(newWidth, Utility.relativelayout_Height);
layoutParams1.setMargins(marginLeftSide, marginRightTopBottomSide, marginRightTopBottomSide, marginRightTopBottomSide);
holder.relativeLayout_Text.setLayoutParams(layoutParams1);
holder.textViewHotelName = (TextView) view.findViewById(R.id.textview_Hotel_Name);
holder.textViewHotelName.setGravity(Gravity.RIGHT);
}
}
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
// get hotel name from arraylist set it to textview.
holder.textViewHotelName.setText(arrayListHotelDetails.get(position).get("hotelName"));
Log.d("textViewHotelName adapter", arrayListHotelDetails.get(position).get("hotelName"));
String hotelId = "";
hotelId = arrayListHotelDetails.get(position).get("hotelId");
Log.d("hotelId", hotelId);
// set image using picasso to image view.
Picasso.with(mContext).load("https://s3-us-west-2.amazonaws.com/foodappdev/hotel/" + hotelId + ".jpg").placeholder(R.drawable.default_hotel).error(R.drawable.default_hotel).into(holder.imageviewHotel);
return view;
}
答案 0 :(得分:0)
你的图像序列是这样的0 3 4 7 8 11 12 15 16 18 19 21所以使用
if((位置%2)== 0)
不能尝试以下
if((位置%3)== 0 ||(位置%3)== 1)
这将产生你正在寻找的锯齿形方式,所以当mod的结果为0或1时,必须放置图像,
希望这会有所帮助