开发者

Adding buttons at run time to each row in a list view

开发者 https://www.devze.com 2023-01-07 08:03 出处:网络
I am working in an android application that uses a list view. I currently have a XML for the row layout with only one text view. Based on certain conditions, some rows will have one additional button

I am working in an android application that uses a list view. I currently have a XML for the row layout with only one text view. Based on certain conditions, some rows will have one additional button and some other rows may have 2 additional buttons. Can I override the getView method of the a开发者_JS百科dapter class to do this logic? is there any performance issue?


Have a row layout with 2 buttons, then:

public View getView(int position, View convertView, ViewGroup parent){
    // the usual convertView stuff
    if(convertView == null){
        convertView = layoutInflater.inflate(//TODO);
        Tag tag = new Tag();
        convertView.setTag(tag);
        tag.button1 = (Button)convertView.findViewById(R.id.btn1);
        tag.button2 = (Button)convertView.findViewById(R.id.btn2);
    }
    Tag tag = (Tag)convertView.getTag();
    boolean buttonOneShown = //TODO;
    boolean buttonTwoShown = //TODO;
    tag.button1.setVisibility(buttonOneShown ? VISIBLE : GONE);
    tag.button1.setVisibility(buttonTwoShown ? VISIBLE : GONE);
}

Performance would be just fine.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号