Android listview如何在listview中的特定位置显示imageview(列表项)?

时间:2016-10-09 16:12:43

标签: android listview imageview

我遇到一个问题,我有一个listview包含text和imageview,我想让imageview只显示在偶数位置,如何制作呢?

That's what i mean

public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView==null){
        LayoutInflater inflater=activity.getLayoutInflater();
        convertView=inflater.inflate(id,null);

    }
    sora item=items.get(position);
    TextView tv_id= (TextView) convertView.findViewById(R.id.sora_id);

    //TextView tv_title= (TextView) convertView.findViewById(R.id.topic);
    int colorPos = position % colors.length;
    final int colorPos2 = position % colors.length;
    convertView.setBackgroundColor(colors[colorPos]);
    tv_id.setTextColor(colors2[colorPos2]);
    tv_id.setTypeface(null, Typeface.BOLD);
    tv_id.setText(item.getSora().toString());

    final ImageView b= (ImageView)  convertView.findViewById(R.id.list_item_btn);

    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            if (position ==2)
            {
                b.setBackgroundColor(0xAA000000);

            }
             Toast.makeText(getContext(),"Clicked on: "+position,Toast.LENGTH_LONG).show();                 
        }
    });


    return convertView;
}

2 个答案:

答案 0 :(得分:1)

getView()

中添加此内容
final ImageView b= (ImageView)  convertView.findViewById(R.id.list_item_btn);
if (position%2!=0){
    b.setVisibility(View.GONE);
}

答案 1 :(得分:0)

感谢Asmaa你的答案很好地解决了我的问题,但我尝试了另一种解决方案,并且使用可绘制图像阵列对我来说很好用

private int[] p = {R.drawable.im1,R.drawable.im2};

并使用setBackgroundResource作为指定按钮,如下所示

int imagepos = position % p.length;

    b.setBackgroundResource(p[imagepos]);
    b.getLayoutParams().height = 65;
    b.getLayoutParams().width = 65;