在RecycleView中使用矢量图像

时间:2017-07-08 10:46:08

标签: android vector android-recyclerview android-image recycler-adapter

RecyclerView适配器类。

public class TravelListAdapter extends RecyclerView.Adapter<TravelListAdapter.ViewHolder> {

    Context mContext;
    OnItemClickListener mItemClickListener;
    String []names = {"Hotels", "Travel", "Medicine", "Education", "Travel", "Hotels"};
    private int[] advertImageList = {R.drawable.hotel, R.drawable.travel, R.drawable.medical, R.drawable.education, R.drawable.travel, R.drawable.hotel};


    // 2

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }

    public TravelListAdapter(Context context) {
        this.mContext = context;
    }

    // 3
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        public LinearLayout placeHolder;
        public LinearLayout placeNameHolder;
        public TextView placeName;
        public ImageView placeImage;

        public ViewHolder(View itemView) {
            super(itemView);
            placeHolder = itemView.findViewById(R.id.mainHolder);
            placeName =  itemView.findViewById(R.id.placeName);
            placeNameHolder =  itemView.findViewById(R.id.placeNameHolder);
            placeImage = itemView.findViewById(R.id.placeImage);

            placeHolder.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mItemClickListener != null) {
                mItemClickListener.onItemClick(itemView, getPosition());
            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }




    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_row_item, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
//        final Place place = new PlaceData().placeList().get(position);

        holder.placeName.setText(names[position]);
        holder.placeName.setTextColor(R.color.black);
        Picasso.with(mContext).load(advertImageList[position]).into(holder.placeImage);
        Bitmap photo = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image2);

        Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
            public void onGenerated(Palette palette) {
                //int bgColor = palette.getMutedColor(mContext.getResources().getColor(android.R.color.transparent));
                holder.placeNameHolder.setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));
            }
        });

    }

    @Override
    public int getItemCount() {
        return names.length;
    }


}`

我的Xml文件

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/placeCard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:cardBackgroundColor="@android:color/transparent">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <android.support.v7.widget.AppCompatImageView
            android:id="@+id/placeImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="center"
            android:transitionName="tImage"
            android:layout_centerInParent="true"
            android:tint="@android:color/white"
            android:padding="10dp"/>

        <!-- Used for the ripple effect on touch -->
        <LinearLayout
            android:id="@+id/mainHolder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="?android:selectableItemBackground"
            android:orientation="horizontal" />

        <LinearLayout
            android:id="@+id/placeNameHolder"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:orientation="horizontal"
            android:transitionName="tNameHolder"
            android:padding="5dp"
            android:layout_below="@+id/placeImage">

            <TextView
                android:id="@+id/placeName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:gravity="center_horizontal"
                android:textAppearance="?android:attr/textAppearanceListItem"
                android:textColor="@android:color/white"/>

        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

我正在使用从Android Studio生成的矢量图像,但这些图像不会显示在recyclerView中。

我该怎么办?让我提一下,如果我在简单的图像视图中使用它们,我在recyclerView中使用的矢量图像会正确显示。

0 个答案:

没有答案