错误的循环视图/卡片视图宽度与水平滚动

时间:2015-05-20 09:26:45

标签: android horizontal-scrolling

使用水平滚动设置CardViewRecycleView的正确宽度尺寸时出现问题。

这是卡片xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">

<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_gravity="center"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    card_view:cardCornerRadius="4dp">

<TextView
    android:id="@+id/info_text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</android.support.v7.widget.CardView>

这是RecycleView

<android.support.v7.widget.RecyclerView
            android:id="@+id/mrecycleview"
            android:scrollbars="horizontal"
            android:layout_width="match_parent"
            android:layout_height="150dp" />

我的活动中的LinearLayoutManager:

mLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recycleView.setLayoutManager(mLayoutManager);

最后,这是我的适配器:

...
public static class ViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView nameTV;

    ViewHolder(View itemView) {
        super(itemView);
        cv = (CardView)itemView.findViewById(R.id.card_view);
        nameTV = (TextView)itemView.findViewById(R.id.info_text);
    }
}


public MyCardAdapter(List<Dog> myDataset) {
    mDataset = myDataset;
}


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

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_card, parent, false);
    ViewHolder vh = new ViewHolder(v);

    return vh;
}

这就是结果: enter image description here

但我希望循环视图占用所有水平空间;像这样的东西:

enter image description here

1 个答案:

答案 0 :(得分:0)

只需将onCreateViewHolder更改为:

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

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_card, parent, false);
v.setMinimumWidth(parent.getMeasuredWidth());
ViewHolder vh = new ViewHolder(v);

return vh;
}