在recyclerview中重叠的图像

时间:2018-04-23 12:33:11

标签: java android xml

我正在创建一个应用程序,我使用recyclerview制作个人资料卡但是我的问题是如果你看到这个我的图像相互重叠

图像前两张卡片的形状是正确的,但是当我添加其他图像时,它不仅会重叠第一张图像,而且会丢失其形状,如图所示

现在我做了一些研究并找到了这个,我的问题与这个question不重复。

但我不认为这是一个很好的方法,因为这个人与Recyclerview完全相反 所以,如果有人可以在这里指导我 我的mainfragment代码

public class FeedFragment extends Fragment {
    RecyclerView feed_recycler_view;
    RecyclerView.LayoutManager feed_layout_manager;
    RecyclerView.Adapter feed_adapter;
    int[] images = {R.drawable.gradient2,R.drawable.gradient2,R.drawable.club1};

    public FeedFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_feed, container, false);
        feed_recycler_view = (RecyclerView)view.findViewById(R.id.feed_recycler_view);
        feed_layout_manager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
        feed_recycler_view.setLayoutManager(feed_layout_manager);
        feed_recycler_view.setNestedScrollingEnabled(false);
        feed_recycler_view.setHasFixedSize(true);
        feed_adapter = new FeedAdapter(images);
        feed_recycler_view.setAdapter(feed_adapter);
        return view;
    }

}

我的适配器代码

public class FeedAdapter extends RecyclerView.Adapter<FeedAdapter.FeedViewHolder>{

    int[] images;

    public FeedAdapter(int[] images) {
        this.images = images;
    }

    @Override
    public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.feed_data,parent,false);
        FeedViewHolder feedViewHolder = new FeedViewHolder(view);
        return feedViewHolder;
    }

    @Override
    public void onBindViewHolder(FeedViewHolder holder, int position) {
    int image_id = images[position];
    //holder.background_image_layout.setImageDrawable(null);
    holder.background_image_layout.setImageResource(image_id);

    }

    @Override
    public int getItemCount() {
        return images.length;
    }
    public static class FeedViewHolder extends RecyclerView.ViewHolder {
        CustomTextViewMedium first_text,second_text,third_text,fourth_text,fifth_text,sixth_text,
                seventh_text;
        ImageView favourite_image;
        ImageView background_image_layout;
        CircleImageView profile_image;
        public FeedViewHolder(View itemView) {
            super(itemView);
            first_text = (CustomTextViewMedium)itemView.findViewById(R.id.first_text);
            second_text = (CustomTextViewMedium)itemView.findViewById(R.id.second_text);
            third_text = (CustomTextViewMedium)itemView.findViewById(R.id.third_text);
            fourth_text = (CustomTextViewMedium)itemView.findViewById(R.id.fourth_text);
            fifth_text = (CustomTextViewMedium)itemView.findViewById(R.id.fifth_text);
            sixth_text = (CustomTextViewMedium)itemView.findViewById(R.id.sixth_text);
            seventh_text = (CustomTextViewMedium)itemView.findViewById(R.id.seventh_text);
            favourite_image = (ImageView)itemView.findViewById(R.id.favourite_image);
            background_image_layout = (ImageView) itemView.findViewById(R.id.background_image_layout);
            profile_image = (CircleImageView)itemView.findViewById(R.id.profile_image);

        }
    }
}

和我的适配器

的xml代码
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    app:cardCornerRadius="8dp">
    <ImageView
        android:id="@+id/background_image_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/gradient2"/>
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profile_image"
                android:layout_width="96dp"
                android:layout_height="96dp"
                android:layout_marginTop="20dp"
                android:layout_marginLeft="10dp"
                android:src="@drawable/ellipse" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="5dp">

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/first_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:text="John Doe"
                        android:layout_marginTop="35dp"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/second_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:text="checked in to"
                        android:layout_marginTop="35dp"
                        android:textColor="@color/White"
                        android:textSize="10sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/third_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:padding="5dp"
                        android:text="W south"
                        android:layout_marginTop="35dp"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:orientation="horizontal">

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/fourth_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="beach mumbai"
                        android:textColor="@color/White"
                        android:layout_marginRight="9dp"
                        android:textSize="15sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/fifth_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/second_text"
                        android:layout_toRightOf="@+id/fourth_text"
                        android:text="30 mins ago."
                        android:textColor="@color/White"
                        android:textSize="10sp" />
                </LinearLayout>
            </LinearLayout>


        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp">

            <com.ct.listrtrial.Custom.CustomTextViewMedium
                android:id="@+id/sixth_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerInParent="true"
                android:padding="10dp"
                android:textColor="@color/White"
                android:text="reply to abc............" />


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true">

                <ImageView
                    android:id="@+id/favourite_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:src="@drawable/ic_favorite_border_black_24dp" />

                <com.ct.listrtrial.Custom.CustomTextViewMedium
                    android:id="@+id/seventh_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="40 likes"
                    android:textColor="@color/White"
                    android:layout_marginRight="15dp"/>
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>
</android.support.v7.widget.CardView>

2 个答案:

答案 0 :(得分:0)

试试这个

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/primary_card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="15dp"
    app:cardCornerRadius="13dp">

    <ImageView
        android:id="@+id/background_image_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:visibility="gone" />

    <LinearLayout
        android:id="@+id/background_image"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:background="@drawable/gradient2"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profile_image"
                android:layout_width="56dp"
                android:layout_height="56dp"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:src="@drawable/ellipse" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="5dp">

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/first_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="John Doe"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/second_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="checked in to"
                        android:textColor="@color/White"
                        android:textSize="10sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/third_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="18dp"
                        android:padding="5dp"
                        android:text="W south"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:orientation="horizontal">

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/fourth_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="9dp"
                        android:text="beach mumbai"
                        android:textColor="@color/White"
                        android:textSize="15sp" />

                    <com.ct.listrtrial.Custom.CustomTextViewMedium
                        android:id="@+id/fifth_text"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/second_text"
                        android:layout_toRightOf="@+id/fourth_text"
                        android:text="30 mins ago."
                        android:textColor="@color/White"
                        android:textSize="10sp" />
                </LinearLayout>
            </LinearLayout>


        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp">

            <com.ct.listrtrial.Custom.CustomTextViewMedium
                android:id="@+id/sixth_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerInParent="true"
                android:padding="10dp"
                android:text="reply to abc............"
                android:textColor="@color/White" />


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true">

                <ImageView
                    android:id="@+id/favourite_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="5dp"
                    android:src="@drawable/ic_favorite_border_black_24dp" />

                <com.ct.listrtrial.Custom.CustomTextViewMedium
                    android:id="@+id/seventh_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="15dp"
                    android:text="40 likes"
                    android:textColor="@color/White" />
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>
</android.support.v7.widget.CardView>

答案 1 :(得分:0)

我怀疑你的图像太大而没有占据全宽尺寸。

<ImageView
    android:id="@+id/background_image_layout"
    android:layout_width="match_parent" // change to wrap content
    android:layout_height="match_parent" // change to wrap content
   // use scale type and adjust view bounds
               android:scaleType="fitXY"
                android:adjustViewBounds="true"
    android:background="@drawable/gradient2"/>