Android抽屉 - 更改图标onClick

时间:2015-05-18 11:48:57

标签: android listview onclick icons navigation-drawer

我有一个带标题的简单导航抽屉(见图)。

enter image description here

我现在要做的是每当标题(listview-item with position 0)被点击时,将向下箭头图标(标题的右下角)更改为向上箭头图标或旋转它所以箭头指向上方。如何使用我的适配器实现这一目标?

我的列表 - 适配器:

public class MyAdapter extends BaseAdapter {

private String[] _navTitles;
private int[] _navIcons;

private String _nameHeader;
private int _profileHeader;
private String _emailHeader;
public int _arrowHeader;


public MyAdapter(String[] navTitles, int[] navIcons, String nameHeader, String emailHeader, int profileHeader, int arrowHeader) {
    _navTitles = navTitles;
    _navIcons = navIcons;
    _nameHeader = nameHeader;
    _emailHeader = emailHeader;
    _profileHeader = profileHeader;
    _arrowHeader = arrowHeader;
}

@Override
public int getCount() {
    return _navTitles.length;
}

@Override
public Object getItem(int position) {
    return _navTitles[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder viewHolder = null;

    if (view == null) {
        if (position == 0) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.profileView = (CircleImageView) view.findViewById(R.id.circleView);
            viewHolder.nameView = (TextView) view.findViewById(R.id.name);
            viewHolder.emailView = (TextView) view.findViewById(R.id.email);
            viewHolder.arrowView = (ImageView) view.findViewById(R.id.arrow);
            viewHolder.holderID = 0;

            view.setTag(viewHolder);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.textView = (TextView) view.findViewById(R.id.rowText);
            viewHolder.imageView = (ImageView) view.findViewById(R.id.rowIcon);
            viewHolder.holderID = 1;

            view.setTag(viewHolder);
        }

    } else {
        viewHolder = (ViewHolder) view.getTag();
    }

    if (_navTitles.length > position && position != 0) {
        viewHolder.textView.setText(_navTitles[position]);
        viewHolder.textView.setTag(_navTitles[position]);
        viewHolder.imageView.setImageResource(_navIcons[position]);
        viewHolder.imageView.setTag(_navIcons[position]);
    }
    if (position == 0) {
        viewHolder.profileView.setImageResource(_profileHeader);
        viewHolder.nameView.setText(_nameHeader);
        viewHolder.emailView.setText(_emailHeader);
        viewHolder.arrowView.setImageResource(_arrowHeader);
    }



    return view;
}

public static class ViewHolder {

    int holderID;
    TextView textView;
    ImageView imageView;
    CircleImageView profileView;
    TextView nameView;
    TextView emailView;
    ImageView arrowView;

}
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我自己解决了。如果有人需要它:

final ViewHolder finalViewHolder = viewHolder;
        viewHolder.arrowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (finalViewHolder.arrowView.getTag().equals("down")) {
                    finalViewHolder.arrowView.setImageResource(R.drawable.ic_arrow_drop_up_white_48dp);
                    finalViewHolder.arrowView.setTag("up");
                } else if (finalViewHolder.arrowView.getTag().equals("up")) {
                    finalViewHolder.arrowView.setImageResource(R.drawable.ic_arrow_drop_down_white_48dp);
                    finalViewHolder.arrowView.setTag("down");
                }
            }
        });