如何在单击项目时更改列表视图中的行视图

时间:2016-03-16 18:31:40

标签: android listview

点击它时是否可以更改行视图?例如,我有listView,其中行显示了一些简短信息,并且在点击行详细视图后将显示而不是旧行。 我尝试在listView中使用 addView ,但在AdapterView"中不支持&list; listview addView(View,int)。错误出现了。

1 个答案:

答案 0 :(得分:0)

尝试类似的事情:

public class MyAdapter extends BaseAdapter {

    private Context mContext;
    private List<UserPojo> mList;

    public MyAdapter(Context context, List<UserPojo> users) {
        this.mContext = context;
        this.mList = users;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public UserPojo getItem(int position) {
        return mList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final UserPojo user = mList.get(position);
        final ViewHolder holder;

        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_ad, null);

            holder = new ViewHolder();

            holder.pic = (CircleImageView) convertView.findViewById(R.id.profile_imageView);
            holder.name = (TextView) convertView.findViewById(R.id.name_textView);

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

        if (user.isClicked) {
            holder.pic.setVisibility(View.VISIBLE);
            holder.name.setVisibility(View.GONE);
        } else {
            holder.pic.setVisibility(View.GONE);
            holder.name.setVisibility(View.VISIBLE);
        }

        holder.name.setText("text");
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user.setClicked(true);
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    private static class ViewHolder {
        CircleImageView pic;
        TextView name;
    }

}