使RecyclerView拥有2种不同的布局

时间:2017-11-07 13:50:04

标签: android android-layout android-recyclerview android-relativelayout

我正在尝试让RecyclerView保留两个不同的布局,例如卡片和TextView。我想要我的`TextView'位于' RecyclerView'的位置0其余的位置通常会绑定到卡片上。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/linearLayout">

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

</RelativeLayout>

接下来,我展示了一些我的适配器方法: 我的onCreateViewHolder

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.d(TAG,"viewType: " + viewType);
    View view = inflater.inflate(R.layout.card_item, parent, false);
    return new ViewHolder(view);
}

接下来是onBindViewHolder

@Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //position 0 tem o ViewHolder tem o Layout da  TextView
        if(position==0){
            View view = inflater.inflate(R.layout.above_card_view, holder.getParent(), false);
//holder??
        }else{
            Pick data = picks.get(position);
            Log.d(TAG, data.toString());
            String aux = data.getCountry() + " - " + data.getLeague();
            holder.getFlag().setImageResource(data.getFlag_src());
            holder.getLeague().setText(aux);
        }
    }

知道我该怎么办?谢谢!

1 个答案:

答案 0 :(得分:0)

试试这个我的朋友

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


    <RelativeLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="@string/app_name"
            android:textAppearance="@style/TextAppearance.AppCompat.Tooltip" />
    </RelativeLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp">
    </android.support.v7.widget.RecyclerView>


</LinearLayout>