RecyclerView查看实施

时间:2017-01-01 07:02:43

标签: android listview android-recyclerview android-linearlayout

我正在尝试开发一个具有卡片视觉的应用程序。我试图模仿与LinkedIn相同的布局:

请注意每个视图的边缘,您可以看到下一张卡片或上一张卡片。

enter image description here enter image description here

我尝试使用RecyclerView

中的LinearLayoutManagerHorizontal Mode.来实现相同的功能

这是它的结果:

I can only see one card at a time

这是我的代码:

profile_card_view.xml(这是RecyclerView中每张卡的布局文件)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:background="#ededed"
        android:padding="10dp"
        android:layout_margin="10dp"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:elevation="11dp">
    <TextView
        android:text="Tanishq Sharma"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/profile_name"
        android:padding="20dp" />

    <TextView
        android:text="I am an entreprenuer, developer."
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/profile_about"
        android:padding="20dp" />

    <TextView
        android:text="9819075165"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/profile_mobile"
        android:padding="20dp" />

    <TextView
        android:text="Mumbai"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/profile_location"
        android:padding="20dp" />

        </LinearLayout>

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

Profile_Cards_Adapter.java - recyclerView适配器

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by tanishqsharma on 31/12/16.
 */

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

    List<Profile_Card> items;

    public Profile_Cards_Adapter(List<Profile_Card> items)
    {
        this.items = items;
    }

    @Override
    public Profile_Cards_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.profile_card_view, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

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

        Profile_Card card = items.get(position);
        holder.user_name.setText(card.getName());
        holder.user_about.setText(card.getAbout());
        holder.user_contact.setText(card.getContact());
        holder.user_location.setText(card.getLocation());
    }


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

    class ViewHolder extends RecyclerView.ViewHolder{

        public TextView user_name;
        public TextView user_location;
        public TextView user_about;
        public TextView user_contact;
        public ViewHolder(View itemView) {

            super(itemView);

            user_name = (TextView) itemView.findViewById(R.id.profile_name);
            user_about = (TextView) itemView.findViewById(R.id.profile_about);
            user_contact = (TextView) itemView.findViewById(R.id.profile_mobile);
            user_location = (TextView) itemView.findViewById(R.id.profile_location);
        }
    }
}

无法获得像LinkedIn这样的布局,我可以看到下一张和上一张卡的边缘。

1 个答案:

答案 0 :(得分:0)

您必须使用我在评论中提到的ViewPager。阅读更多相关信息here

提示:您可以使用library来了解它。