2016-04-04 66 views
-1

我有多個項目的列表視圖。每行包含圖像和文本的曲折形式我檢查位置如果((位置%2)== 0)然後我將圖像附加到左側和文本到右側,並在其他我設置圖像右側和文本左側。它工作正常,但當我向上滾動或向下滾動,然後我得到序列的問題。我需要的ListView這樣enter image description hereAndroid的列表視圖序列錯誤

但是當我滾動到列表視圖然後我得到這樣

enter image description here

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

你的代碼是非常搞砸解決這個問題。發佈的問題與行視圖的可用性有關。我認爲你應該嘗試使用回收視圖。順便說一句,爲什麼你動態設置邊距?您可以在.xml中創建左右佈局,並從維度中設置邊距 –

回答

0

我加入這一行

view = null; 

下面Viewholder聲明

0

您好你的圖像序列是這樣的0 3 4 7 8 11 12 15 16 18 19 21因此使用

如果((位置%2)== 0)

不會被足夠嘗試以下

如果((位置%3)== 0 || (位置%3)== 1)

這將產生你正在尋找所以當MOD的結果爲0或1的圖像必須被放置以Z字形方式,,

希望這有助於

+0

這個答案不適用於我 –