点击后如何更改视图?

时间:2015-03-24 09:56:16

标签: android android-view android-adapter

我做设计。而且我不知道如何。

screen

如果我登录该项目,则应在右侧显示红色视图。

我使用GridView和我的适配器public class BasketAdapter extends BaseAdapter

项目XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="142dp"
              android:layout_height="wrap_content"
              android:background="#eceff3"
              android:paddingBottom="1dp">

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="141.25dp"
        android:layout_height="138.75dp"
        android:background="#fff">

        <ImageView
            android:layout_width="65dp"
            android:layout_height="65dp"
            android:id="@+id/goodImage"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="27dp"
            android:layout_below="@+id/goodImage"
            android:layout_marginTop="11dp"
            android:id="@+id/linearLayout3"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginLeft="28.75dp"
            android:layout_marginRight="28.75dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:text="bascetName"
                android:id="@+id/bascetName"
                android:textSize="12.50sp"
                android:textStyle="bold"
                android:textColor="#333333"
                android:ellipsize="end"
                android:singleLine="false"
                android:gravity="center_horizontal" />
        </LinearLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="$1.23"
            android:id="@+id/bascetPrice"
            android:textSize="13.75sp"
            android:layout_gravity="center_horizontal"
            android:textColor="#ffc600"
            android:layout_below="@+id/linearLayout3"
            android:layout_centerHorizontal="true"
            android:layout_marginTop=" 8.75dp" />

        <ImageView
            android:layout_width="32dp"
            android:layout_height="38.50dp"
            android:id="@+id/countImage"
            android:src="@drawable/count"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="false" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"
            android:id="@+id/basketQuantity"
            android:textSize="12.50sp"
            android:textStyle="bold"
            android:layout_gravity="right"
            android:autoText="false"
            android:textColor="#ffffff"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="5dp" />

    </RelativeLayout>

</LinearLayout>

适配器

public class BasketAdapter extends BaseAdapter implements View.OnClickListener {
    private List<ShoppingCart> items;
    private AnimateFirstDisplayListener animateFirstDisplayListener = new AnimateFirstDisplayListener();
    private DisplayImageOptions options = ILOptions.getOption();
    private Typeface font;

    public BasketAdapter(List<ShoppingCart> items) {
        this.items = items;
    }

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

    @Override
    public ShoppingCart getItem(int position) {
        return items.get(position);
    }

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

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

        if (null == convertView) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            font = Fonts.getBlockBertholdRegular(parent.getContext());
            convertView = inflater.inflate(R.layout.basket_item, parent, false);
            holder = new ViewHolder();
            holder.name = (TextView) convertView.findViewById(R.id.bascetName);
            holder.price = (TextView) convertView.findViewById(R.id.bascetPrice);
            holder.imageView = (ImageView) convertView.findViewById(R.id.goodImage);
            holder.imageCount = (ImageView) convertView.findViewById(R.id.countImage);
            holder.quantity = (TextView) convertView.findViewById(R.id.basketQuantity);

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

        holder.name.setText(items.get(position).getItem().getName().toUpperCase());
        holder.name.setTypeface(font, Typeface.BOLD);
        holder.price.setText("$" + items.get(position).getTotal());
        holder.price.setTypeface(font, Typeface.NORMAL);
        ImageLoader.getInstance().displayImage(items.get(position).getItem().getImage(), holder.imageView, options, animateFirstDisplayListener);
        if (items.get(position).getItemCount() == 1) {
            holder.imageCount.setVisibility(View.GONE);
            holder.quantity.setVisibility(View.GONE);
        } else {
            holder.quantity.setText(String.valueOf(items.get(position).getItemCount()));
            holder.quantity.setTypeface(font, Typeface.BOLD);
        }
        convertView.setOnClickListener(this);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        Toast.makeText(v.getContext(),"cvcxvfdg",Toast.LENGTH_LONG).show();
    }

    private static class ViewHolder {
        TextView name;
        TextView price;
        ImageView imageView;
        ImageView imageCount;
        TextView quantity;
    }
}

如有必要,我会添加其他内容。可以准备一个图书馆吗?

1 个答案:

答案 0 :(得分:0)

点击gridview项目更改ImageView

gridView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
        ViewHolder holder = (ViewHolder)view.getTag();

        //set your new image here 
        holder.imageview.setImageResource(R.id.yourImage);
        ... ...

        //maybe also update items content in 'position' if need
    }
});

希望这有帮助!