使用带有标题和详细信息的recycleview的Android Cardview

时间:2017-02-23 00:19:43

标签: android android-layout android-recyclerview android-cardview

我想使用回收视图创建卡片列表,但我无法理解如何创建CustomAdapter和Layout来执行卡片详细信息。请参阅homeData列表以获取我提供的数据。我能够创建卡的地址标题,但不知道如何处理细节。标题应该被视为类别,因为我每张卡只使用一个地址,但需要每个地址列出每个月。

此外,细节中可能有多行的月份。

任何帮助将不胜感激!

enter image description here

List<HomeAvailability> homeData = new ArrayList<>(); 
    homeData.add(new HomeAvailability("1111 Main St", "Jun"));
    homeData.add(new HomeAvailability("1111 Main St", "Jul"));
    homeData.add(new HomeAvailability("1111 Main St", "Aug"));
    homeData.add(new HomeAvailability("1111 Park St", "Jul"));
    homeData.add(new HomeAvailability("2222 Park St", "Aug"));
    homeData.add(new HomeAvailability("2222 Oregon St", "Oct"));
    homeData.add(new HomeAvailability("1111 Oregon St", "Nov"));
    homeData.add(new HomeAvailability("1111 Oregon St", "Dec"));
    homeData.add(new HomeAvailability("1111 Oregon St", "Jan"));

MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);

    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

     MainAdapter mAdapter = new MainAdapter(homeData);
     mRecyclerView.setAdapter(mAdapter);
}

CustomAdapter.java

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        TextView address;
        TextView month; 

        public ViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.card_view);
            address = (TextView)itemView.findViewById(R.id.address);
            //month = (TextView)itemView.findViewById(R.id.month);
        }
    } 

    List<HouseAvailability> mDataset;

    public CustomAdapter(List<HouseAvailability> myDataset) {
        this.mDataset = myDataset;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,
                                               int viewType) { 
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, viewGroup, false);  
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int i) { 
       holder.address.setText(mDataset.get(i).address); 
    }

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

card_layout.xml

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  
    card_view:cardCornerRadius="6dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TextView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="3"
            android:padding="12dp"
            android:textColor="#222"
            android:textStyle="bold"
            android:textSize="18dp" />

        <View
            android:id="@+id/divider"
            android:layout_below="@id/address"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_margin="4dp"
            android:background="@android:color/black" />

         //Months here?

        </RelativeLayout>


    </RelativeLayout>

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

1 个答案:

答案 0 :(得分:2)

您可以使用当前数据集实现目标,但我建议您制作另一个数据集,每个项目都有一个唯一的地址数据。

无论如何,如果你想用当前的套装做,你必须修改getItemCount()getItemType()onCreateViewHolder()onBindViewHolder()

代码示例

(我还没有对这段代码进行过测试,这是手写的。)

假设您的数据类已经像这样修改过:

class HouseAvailability
{
    public String address;
    public ArrayList<String> monthList;
}

CustomAdapter.java

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

    public static class ViewHolder extends RecyclerView.ViewHolder {

        CardView cv;
        TextView address;
        TextView month; 

        public ViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.card_view);
            address = (TextView)itemView.findViewById(R.id.address);
            month = (TextView)itemView.findViewById(R.id.month);
        }

        public void bindData(HouseAvailability data) {
            this.address.setText(data.address);
            String monthStr = "";
            for(int i=0; i<data.monthList.size(); i++) {
                monthStr += data.monthList.get(i);
                if(i<data.monthList.size()-1) {
                    if(i%4 == 3)
                        monthStr += "\n"
                    else
                        monthStr += "  "
                }
            }
            this.month.setText(monthStr);
        }
    } 

    List<HouseAvailability> mDataset;

    public CustomAdapter(List<HouseAvailability> myDataset) {
        this.mDataset = myDataset;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup,
                                               int viewType) { 
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, viewGroup, false);  
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int i) { 
       holder.bindData(mDataset.get(i)); 
    }

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

和XML部分:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  
    card_view:cardCornerRadius="6dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <TextView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="3"
            android:padding="12dp"
            android:textColor="#222"
            android:textStyle="bold"
            android:textSize="18dp" />

        <View
            android:id="@+id/divider"
            android:layout_below="@id/address"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_margin="4dp"
            android:background="@android:color/black" />

        <TextView
            android:id="@+id/month"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="3"
            android:padding="12dp"
            android:textColor="#222"
            android:textStyle="bold"
            android:textSize="18dp" />

        </RelativeLayout>


    </RelativeLayout>

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

您可以根据需要修改详细信息。