如何根据位置隐藏Listview中的视图

时间:2016-11-08 11:02:46

标签: android listview baseadapter

我有一个相对布局,我想在按钮点击时隐藏和显示(对于每个列表视图项目)。

Adapter.java

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

    inflate list_items.xml

    set data


on button click
            if view is hidden 
                view.setVisibility(View.VISIBLE);
            } else {
                view.setVisibility(View.GONE);
            }
        }
    });
    return cView;
}

2 个答案:

答案 0 :(得分:0)

我修改了一些代码。试试这个,可能适合你。

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

inflate list_items.xml

set data

//Layout which i want to hide and show
        barRL = (RelativeLayout) convertView.findViewById(R.id.layout20);
        button= (Button) convertView.findViewById(R.id.button); // to click button 

        if (button is not visible) {
                barRL.setVisibility(View.VISIBLE);
        } else {
                barRL.setVisibility(View.GONE);
        }

        //Button to hide and show layout
        ImageView arrowUpDown =           (ImageView)convertView.findViewById(R.id.arrowupdown);

        arrowUpDown.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                if (arrowUpDownis not visible) {
                    arrowUpDownis.setVisibility(View.VISIBLE);
                 } else {
                    arrowUpDownis.setVisibility(View.GONE);
                 }
    notifyDataSetChanged();

                }
            });
            return convertView;
        }

并且不要在xml中设置按钮的可见性。

答案 1 :(得分:0)

只需在代码中添加notifydatasetchanged()即可。

 ArrayList <Boolean> statusList;//initialise it in array as true for all the elements

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

inflate list_items.xml

set data

//Layout which i want to hide and show
barRL = (RelativeLayout) convertView.findViewById(R.id.layout20);

if (statusList.get(position)) {
            barRL.setVisibility(View.VISIBLE);
        } else {
            barRL.setVisibility(View.GONE);
        }
//Button to hide and show layout
ImageView arrowUpDown = (ImageView) convertView.findViewById(R.id.arrowupdown);

arrowUpDown.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
if (view is not visible) {
           statusList.set(position,true);
        } else {
             statusList.set(position,false);
        }
        notifyDataSetChanged();
    }
});
return convertView;
}