2016-01-21 60 views
1

我有含有customView(圓形)一viewHolder一個customAdapter 我需要找回它包含在視圖持有人,而不是目標項目列表視圖 - 對項目的訪問viewHolder元素點擊

視圖的顏色

我該怎麼做? 非常感謝您

我的適配器:

public class WelcomeAdapter extends BaseAdapter { 

// contex 
private Context context; 

// Liste a affichée 
private RealmList<Measure> welcomeList; 

public RealmList<WelcomeList> getWelcomeList() { 
    return welcomeList; 
} 

private LayoutInflater inflater = null; 

private int color1; 
private int color2; 
private int color3; 



public WelcomeAdapter (Context context, RealmList<Welcome> welcomeList) { 
    this.context = context; 
    this.welcomeList= welcomeList; 

    color1= ContextCompat.getColor(context, R.color.green); 
    color2= ContextCompat.getColor(context, R.color.yellow); 
    color3= ContextCompat.getColor(context, R.color.blue); 


    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getCount() { 
    return welcomeList== null ? 0 : welcomeList.size(); 
} 

@Override 
public Object getItem(int position) { 
    return welcomeList== null ? null : welcomeList.get(position); 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ViewHolder viewHolder; 
    if (convertView == null) { 
     convertView = inflater.inflate(R.layout.row_welcome, parent, false); 
     viewHolder = new ViewHolder(); 
     viewHolder.cs = (CircleShape) convertView.findViewById(R.id.row_welcome_cv); 

     convertView.setTag(viewHolder); 
    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 

    Welcome welcome= WelcomeList.get(position); 



    if (welcome.getWelcomePeople() != null) { 
     for (People p : welcome.getWelcomePeople()) { 
      if (p.getNumber() > 5) { 
       viewHolder.cs.setColor(color1); 
      } else if (p.getNumber() > 15) { 
       viewHolder.cs.setColor(color2); 
      } else if (p.getNumber() > 25) { 
       viewHolder.cs.setColor(color3); 
      } 
     } 
    } 

    return convertView; 
} 

class ViewHolder { 
    CircleShape cs; 

} 

活動

protected AdapterView.OnItemClickListener welcomeClickListener = new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      // need to retrieve the current color of the CircleShape of the view holder associated with the item clicked there 

     } 
    }; 

非常感謝您

回答

2

如果我沒有記錯的話,你應該能夠檢索通過致電getTag()查看返回給您的視圖持有人,因爲您正在將標記設置爲視圖持有人,使用convertView.setTag(viewHolder)

因此,您onItemClick方法獲取CircleShape應如下所示:

ViewHolder vh = (ViewHolder) view.getTag(); 
CircleShape cs = vh.cs; 

讓我知道你的作品,我和適配器有點生疏。

+0

正確答案。你還需要確保'Activity'和'Adapter'在同一個包中(因爲OP的當前'ViewHolder'具有包保護的可見性),或者使'ViewHolder'成爲公共類。 –

+0

@Jospeh Roque。謝謝你,完美的作品:) – ulquiorra