Recycler视图未正确放置视图

时间:2015-12-10 05:57:02

标签: android android-recyclerview

我在Horizontal Recycler View内使用Vertical Recycler View。代码正在运行。问题是Row Item的{​​{1}}是正确的。它不是将视图放在正确的位置

从Android Studio预览中查看 enter image description here

移动设备的实际视图 enter image description here

XML

Horizontal Recycler View

垂直适配器

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:background="#000">

    <ImageView
        android:id="@+id/imageView12"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:scaleType="fitXY"
        android:src="@drawable/im_bg" />


    <TextView

        android:id="@+id/tv_featured"
        style="@style/text_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:background="@drawable/gradient_button"
        android:paddingBottom="10dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingTop="10dp"
        android:text="Featured" />

    <ImageView
        android:id="@+id/iv_share"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tv_featured"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:padding="10dp"
        android:src="@drawable/ic_explore_share" />

    <ImageView
        android:id="@+id/iv_bookmark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:padding="10dp"
        android:src="@drawable/ic_explore_bookmark" />

    <TextView
        android:id="@+id/tv_rating"
        style="@style/text_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_venue_name"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="40dp"
        android:background="@drawable/gradient_button"
        android:padding="05dp"
        android:text="3.5"
        android:textSize="@dimen/title" />

    <TextView
        android:id="@+id/tv_venue"
        style="@style/text_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_venue_name"
        android:layout_alignLeft="@+id/tv_venue_name"
        android:layout_alignStart="@+id/tv_venue_name"
        android:layout_marginBottom="05dp"
        android:text="Venue" />

    <TextView
        android:id="@+id/tv_venue_name"
        style="@style/text_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tv_location"
        android:layout_alignLeft="@+id/tv_location"
        android:layout_alignStart="@+id/tv_location"
        android:layout_marginBottom="05dp"
        android:text="JW MARRIOT"
        android:textSize="@dimen/title" />

    <TextView
        android:id="@+id/tv_location"
        style="@style/text_style"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView12"
        android:layout_alignLeft="@+id/tv_featured"
        android:layout_alignStart="@+id/tv_featured"
        android:layout_marginBottom="20dp"
        android:text="Mumbai, Maharashtra" />

    <TextView
        android:id="@+id/tv_rate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tv_location"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="20dp"
        android:background="@drawable/explore_rounded_amount"
        android:paddingBottom="05dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="05dp"
        android:text="50000"
        android:textSize="@dimen/small" />
</RelativeLayout>

水平适配器

   private Context context;

    private static final int HORIZONTAL_LIST = 0;
    private static final int HEADER = 1;
    private static final int CATEGORIES = 2;

    public ExploreVerticalAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {

            return HORIZONTAL_LIST;

        } else if (position == 1) {

            return HEADER;

        } else {

            return CATEGORIES;
        }


    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == HORIZONTAL_LIST) {

            View view = LayoutInflater.from(context).inflate(R.layout.custom_explore_vertical_item, parent, false);
            HorizontalHolder viewHolder = new HorizontalHolder(view);
            return viewHolder;

        } else if (viewType == HEADER) {

            View view = LayoutInflater.from(context).inflate(R.layout.custom_explore_header, parent, false);
            HeaderHolder viewHolder = new HeaderHolder(view);
            return viewHolder;
        } else {


            View view = LayoutInflater.from(context).inflate(R.layout.custom_vertical_categories, parent, false);
            CategoriesHolder viewHolder = new CategoriesHolder(view);
            return viewHolder;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


        switch (holder.getItemViewType()) {

            case HORIZONTAL_LIST:

                HorizontalHolder horizontalHolder = (HorizontalHolder) holder;
                ExploreHorizontalAdapter adapter = new ExploreHorizontalAdapter(context);

                horizontalHolder.rvHorizontalList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, true));
                horizontalHolder.rvHorizontalList.setNestedScrollingEnabled(false);
                horizontalHolder.rvHorizontalList.setAdapter(adapter);
                adapter.notifyDataSetChanged();

                break;

            case HEADER:

                break;

            case CATEGORIES:

                break;
        }

    }

    @Override
    public int getItemCount() {
        return 9;
    }

    class HorizontalHolder extends RecyclerView.ViewHolder {

        private RecyclerView rvHorizontalList;

        public HorizontalHolder(View itemView) {
            super(itemView);

            rvHorizontalList = (RecyclerView) itemView.findViewById(R.id.rv_explore_hor_list);
            rvHorizontalList.setNestedScrollingEnabled(false);
            rvHorizontalList.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
            rvHorizontalList.setAdapter(null);
        }
    }

    class HeaderHolder extends RecyclerView.ViewHolder {

        public HeaderHolder(View itemView) {
            super(itemView);
        }
    }

    class CategoriesHolder extends RecyclerView.ViewHolder {

        private TextView tvTitle;
        private ImageView ivImage;

        public CategoriesHolder(View itemView) {
            super(itemView);

            tvTitle = (TextView) itemView.findViewById(R.id.tv_cat_tittle);
            ivImage = (ImageView) itemView.findViewById(R.id.iv_cat_image);
        }
    }

1 个答案:

答案 0 :(得分:0)

您可能会考虑使用StaggeredLayoutManager代替您的水平RecyclerView基础上的一些测试,然后再使Horizontal LinearLayoutManager填充父宽度有点单调乏味。在适配器中使用StaggeredLayoutManager后,请执行以下操作:

import static android.support.v7.widget.StaggeredGridLayoutManager.LayoutParams;

LayoutParams params = (LayoutParams) holder.itemView.getLayoutParams();
     if (params == null) {
           params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
     }
params.setFullSpan(true);
holder.itemView.setLayoutParams(params);

试一试,如果你需要更多助手,请回复我