2015-04-04 77 views
0

我有一個ListView,自定義項目。Android Listview混合了項目

該項目的佈局:

我有以下的類用於存儲信息:

public class MainItem{ 

private int DrawableId; 
private String title; 

public MainItem(String title, int drawable) 
{ 
    this.title = title; 
    this.DrawableId = drawable; 
} 

public String getTitle() { 
    return title; 
} 

public int getDrawableId() { 
    return DrawableId; 
}} 

public class MainRow{ 

public MainItem getMainItem1() { 
    return mainItem1; 
} 

public MainItem getMainItem2() { 
    return mainItem2; 
} 

public MainItem getMainItem3() { 
    return mainItem3; 
} 

私人MainItem mainItem1,mainItem2,mainItem3;

public int getNumberOfItems() { 
    return numberOfItems; 
} 

private int numberOfItems = 0;

public MainRow(MainItem mainItem) 
{ 
    this(1,mainItem,null,null); 
} 

public MainRow(MainItem mainItem, MainItem mitem2) 
{ 
    this(2,mainItem,mitem2,null); 
} 
public MainRow(int nb, MainItem mainItem, MainItem mainItem2, MainItem mainItem3) 
{ 
    this.mainItem1 = mainItem; 
    this.mainItem2 = mainItem2; 
    this.mainItem3 = mainItem3; 
    this.numberOfItems = nb; 
}} 

和finnaly我BaseAdapter是:

public class MainItemAdapter extends BaseAdapter { 

List<MainRow> mitems; 

MainScreenItemLisener mainScreenItemLisener;

public MainItemAdapter(MainScreenItemLisener mainScreenItemLisener, ArrayList<MainRow> list) 
    { 
     mitems = list; 
     this.mainScreenItemLisener = mainScreenItemLisener; 
    } 

@Override 
public int getCount() { 
    return mitems.size(); 
} 

@Override 
public Object getItem(int position) { 
    return mitems.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return position; 
} 

static class ViewHolder { 
    ImageView imgLeft, imgRight, imgMiddle; 
    TextView tvLeft, tvRight, tvMiddle; 
    LinearLayout mLeft, mRight, mMiddle; 
} 

@Override 
public boolean isEnabled(int position) { 
    return false; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ViewHolder holder = null; 


    if (convertView == null) 
    { 
     holder = new ViewHolder(); 

     LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(
       Context.LAYOUT_INFLATER_SERVICE); 

      convertView = inflater.inflate(R.layout.main_screen_two, null); 
      holder.imgLeft = (ImageView) convertView.findViewById(R.id.image_left); 
     holder.imgMiddle = (ImageView) convertView.findViewById(R.id.image_middle); 
      holder.imgRight = (ImageView) convertView.findViewById(R.id.image_right); 
      holder.tvLeft = (TextView) convertView.findViewById(R.id.tvLeft); 
      holder.tvRight = (TextView) convertView.findViewById(R.id.tvRight); 
      holder.tvMiddle = (TextView) convertView.findViewById(R.id.tvMiddle); 
      holder.mLeft = (LinearLayout) convertView.findViewById(R.id.mLinearLauoutleft); 
      holder.mRight = (LinearLayout) convertView.findViewById(R.id.mLinearLauoutRight); 
     holder.mMiddle = (LinearLayout) convertView.findViewById(R.id.mLinearLauoutMiddle); 
     convertView.setTag(holder); 

    } 
    else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    final MainRow mItem = mitems.get(position); 

    if(mItem.getNumberOfItems() >=1) 
    if(mItem.getMainItem1() != null) 
    { 

     Picasso.with(parent.getContext()) 
        .load(mItem.getMainItem1().getDrawableId()) 
        .into(holder.imgLeft); 
      holder.tvLeft.setText(mItem.getMainItem1().getTitle()); 

      holder.mLeft.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      mainScreenItemLisener.ItemClicked(mItem.getMainItem1());  } 
      }); 

    } 

    if(mItem.getNumberOfItems() ==1) return convertView; 

    if(mItem.getNumberOfItems() >=2) 
    if(mItem.getMainItem2() != null) 
    { 

     Picasso.with(parent.getContext()) 
       .load(mItem.getMainItem2().getDrawableId()) 
       .into(holder.imgMiddle); 
     holder.tvMiddle.setText(mItem.getMainItem2().getTitle()); 

     holder.mMiddle.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mainScreenItemLisener.ItemClicked(mItem.getMainItem2());} 
     }); 
    } 

    if(mItem.getNumberOfItems() >=3) 
    if(mItem.getMainItem3() != null) 
    { 

     Picasso.with(parent.getContext()) 
       .load(mItem.getMainItem3().getDrawableId()) 
       .into(holder.imgRight); 
     holder.tvRight.setText(mItem.getMainItem3().getTitle()); 

     holder.mRight.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mainScreenItemLisener.ItemClicked(mItem.getMainItem3());} 
     }); 
    } 
    return convertView; 
}} 

我的問題是這樣的:

當首先創建的列表視圖中顯示如下圖所示:

的設置:

items.add(new MainRow(3, new MainItem(getString(R.string.evenimente),R.drawable.evenimente), 
          new MainItem(getString(R.string.atracti_turistice),R.drawable.atracti_turistice), 
          new MainItem(getString(R.string.ture_turistice), R.drawable.ghid))); 

    items.add(new MainRow(3, new MainItem(getString(R.string.restaurante),R.drawable.restaurant), 
           new MainItem(getString(R.string.hoteluri),R.drawable.hotel), 
           new MainItem(getString(R.string.spa), R.drawable.spa_wellnes))); 

    items.add(new MainRow(3, new MainItem(getString(R.string.nightlife),R.drawable.clubs), 
           new MainItem(getString(R.string.shopping),R.drawable.shop), 
           new MainItem(getString(R.string.transport_local), R.drawable.transport))); 


    items.add(new MainRow(3, new MainItem(getString(R.string.parcari),R.drawable.parcari), 
           new MainItem(getString(R.string.servicii_medicale),R.drawable.service_medicale), 
          new MainItem(getString(R.string.service_auto), R.drawable.service_auto))); 


    items.add(new MainRow(new MainItem(getString(R.string.map),R.drawable.harta))); 

    mainItemAdapter = new MainItemAdapter(this,items); 

    mListView.setAdapter(mainItemAdapter); 

在最後一行僅「Altele」的東西是可以調用的,但是當第一次顯示列表視圖時,就像你看到的是另一個項目中的東西在加上。該怎麼辦 ?

This is how it looks like!

+0

是否有任何理由,你爲什麼不直接使用GridView控件來實現這一目標? – 2015-04-04 11:14:25

回答

0

你缺少你如果邏輯的「其他」的情況。您必須明確設置每一行,因爲這些行正在被重用。另外,如果沒有花括號的邏輯是不好的mojo並且讓你的getView()難以遵循。

例如,如果您的最後一行沒有3項,該怎麼辦?以下2個插槽將被重新使用並顯示之前在這些行中的圖像。這條線if(mItem.getNumberOfItems() ==1) return convertView; 是你的罪魁禍首。現在

...你的代碼(更新):

if(mItem.getNumberOfItems() >=2) { 
    if(mItem.getMainItem2() != null) { 
     // do something 
    } 
} 

// OR 

if(mItem.getNumberOfItems() >=2 && mItem.getMainItem2() != null) { 
     // do something 
}