如何使RecyclerView宽度=" wrap_content"

时间:2015-11-26 09:38:37

标签: android center android-recyclerview

我想像这样制作RecyclerView:

enter image description here

但在我的情况下,子视图不会设置为width="wrap_content",或者RecyclerView没有设置为width="wrap_content""center"

enter image description here

以下是activity_layout.xml中的RecyclerView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
</RelativeLayout>

这里设置Adapter onCreate:

    public void initRecyclerView(List<Invitation> invitationList) {
        recyclerView.setHasFixedSize(true);
        GridLayoutManager manager = new GridLayoutManager(this, 4);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(new ItemGridAdapter(getApplicationContext(), invitationList));
    }

这是我的适配器:

public class ItemGridAdapter extends RecyclerView.Adapter<ItemGridAdapter.ViewHolderItem> {
    private final Context context;
    private final List<Invitation> list;
    private final DrawableHelper drawableHelper;

    public ItemGridAdapter(Context context, List<Invitation> list) {
        this.context=context;
        this.list=list;
        this.drawableHelper = new DrawableHelper();
    }

    @Override
    public ItemGridAdapter.ViewHolderItem onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid, parent, false);
        return new ItemGridAdapter.ViewHolderItem(context,view);
    }

    @Override
    public void onBindViewHolder(final ItemGridAdapter.ViewHolderItem viewHolder, int position) {
        Invitation invitation = list.get(position);
        viewHolder.position=position;
        Picasso.with(context)
               .load(invitation.getCustomUser().getAvatar())
               .transform(new CircleTransformation())
               .placeholder(drawableHelper.getDrawableForName(invitation.getCustomUser().getFullName()))
               .into(viewHolder.userIcon);
        if (invitation.getYelpID()!=null&&invitation.getYelpID().length()>0){
            viewHolder.votedIcon.setVisibility(View.VISIBLE);
        }else{
            viewHolder.votedIcon.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return position;
    }


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

    public static class ViewHolderItem extends RecyclerView.ViewHolder {
        public ImageView votedIcon;
        public ImageView userIcon;
        Context mContext;
        int position;
        public ViewHolderItem(Context mContext,View itemView) {
            super(itemView);
            this.mContext = mContext;
            userIcon=(ImageView)itemView.findViewById(R.id.userIcon); 
            votedIcon = (ImageView)itemView.findViewById(R.id.votedIcon);
        }
    }

}

这里是适配器项目R.layout.item_grid的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center">


        <ImageView
            android:background="@drawable/white_circle"
            android:padding="3dp"
            android:id="@+id/userIcon"
            android:layout_width="50dp"
            android:layout_height="50dp"/>

        <ImageView
            android:id="@+id/votedIcon"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/icon_voted"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

默认情况下,

Android Support Library as of 23.2在RecyclerView中支持此WRAP_CONTENT。