【问题标题】:RecyclerView View ImplementationRecyclerView视图实现
【发布时间】:2017-01-01 07:02:43
【问题描述】:

我正在尝试开发一个具有卡片愿景的应用程序。我正在尝试模拟与 LinkedIn 相同的布局:

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

我尝试在Horizontal Mode. 中使用RecyclerViewLinearLayoutManager 来实现相同的功能

这是它的结果:

这是我的代码:

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 这样的布局,我可以看到下一张和上一张卡片的边缘。

【问题讨论】:

  • 改用ViewPager
  • 哦,任何阐述都会很好。
  • 要求解释否决票。

标签: android listview android-recyclerview android-linearlayout


【解决方案1】:

您必须像我在 cmets 中提到的那样使用ViewPager。阅读更多关于它的信息here

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    相关资源
    最近更新 更多