在itemclick上的自定义列表视图中显示隐藏文本视图

时间:2014-11-13 04:55:15

标签: android listview

我在自定义列表视图中有两个文本视图。当我选择一个列表项时,我希望holder.text2对于所选行是可见的,当我选择另一行时,我希望先前的holder.text2是不可见的并且是新的选中的位置行holder.text2变为可见。我怎么能这样做?。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    ViewHolder holder;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();

        row = inflater.inflate(layoutResID, parent, false);
        holder = new ViewHolder();


        holder.text1 = (TextView) row.findViewById(R.id.text1);
        holder.text2 = (TextView) row.findViewById(R.id.text2);


        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();

    }

    Item item = items.get(position);

    holder.text1.setText(item.gettext());
    holder.text2.setText(item.getText2());

    return row;
}
public static class ViewHolder {

    TextView text1, text2;

}

项目点击时:

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        ViewHolder holder= (ViewHolder) view.getTag();
        holder.text2.setVisibility(View.VISIBLE);

    }

5 个答案:

答案 0 :(得分:1)

我从未使用ViewHolder holder= (ViewHolder) view.getTag();,也不知道它是否有效。

这个应该有效:

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

    TextView text1 = (TextView)view.findViewById(R.id.text1);
    TextView text2 = (TextView)view.findViewById(R.id.text2);

    if (text1.getVisibility() == View.VISIBLE) 
       text1.setVisibility(View.GONE); 
    else 
       text2.setVisibility(View.VISIBLE);

    }

答案 1 :(得分:1)

在列表项目持有者中添加一个布尔值falg并尝试在项目点击中更改旗帜状态:

public class Item {

private String text1;
private String text2;
private boolean isSelected;

public String getText1() {
    return text1;
}

public void setText1(String text1) {
    this.text1 = text1;
}

public String getText2() {
    return text2;
}

public void setText2(String text2) {
    this.text2 = text2;
}

public boolean isSelected() {
    return isSelected;
}

public void setSelected(boolean isSelected) {
    this.isSelected = isSelected;
}

}

现在尝试在项目点击时更改标志状态:

ArrayList listItems;

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
    for (Item item :listItems){
        item.setSelected(false);
    }
    listItems.get(position).setSelected(true);
    adapter.notifyDataSetChanged();
}

显示/隐藏text2基本项目选择标志值:

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

        convertView = LayoutInflater.from(context).inflate(layoutResID, parent, false);
        holder.text1 = (TextView) convertView.findViewById(R.id.text1);
        holder.text2 = (TextView) convertView.findViewById(R.id.text2);

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

    holder.text1.setText(items.get(position).getText1());
    holder.text2.setText(ititems.get(position)getText2());
    if(items.get(position).isSelected()){
       holder.text2.setVisibility(View.VISIBLE)
    }else{
       holder.text2.setVisibility(View.GONE)
    }

    return convertView;
}

答案 2 :(得分:0)

试试这段代码......

if (holder.text2.getVisibility() == View.VISIBLE) {
    holder.text2.setVisibility(View.GONE); 
} else {
    holder.text2.setVisibility(View.VISIBLE);
}

答案 3 :(得分:0)

尝试这种方式。

row.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                SetVisible(position)
            }
        });

private void SetVisible(int pos){
        for(int i = 0; i < listview.getCount(); i++){
            View listitem = listview.getAdapter().getView(i, null, listview);
            Textview textview = (TextView) listitem.findViewById(R.id.text2);
            text2.setVisibility(View.GONE);
        }
        View view = listview.getAdapter().getView(pos, null, listview);
        Textview text2 = (TextView) view.findViewById(R.id.text2);

        text2.setVisibility(View.VISIBLE);
    }

答案 4 :(得分:0)

试试这个。

@Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        ViewHolder holder= (ViewHolder) view.getTag();
           if(position=firstrowposition){
            holder.text2.setVisibility(View.VISIBLE);
            holder.text1.setVisibility(View.INVISIBLE);
          }
       else if(position=anotherrowposition){
            holder.text1.setVisibility(View.VISIBLE);
            holder.text2.setVisibility(View.INVISIBLE);
       }
       else{
          //your desired states
       }


    }