orginase在recyclerviews中的布局视图

时间:2016-01-30 23:14:22

标签: android android-recyclerview

我在recyclerview

中有2个观看次数
  @Override
    public RecyclerView.ViewHolder  onCreateViewHolder(ViewGroup parent,
                                                       int viewType) {
        if (viewType == TYPE_IMAGE) {
            // create a new view
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
            // set the view's size, margins, paddings and layout parameters
            ImageViewHolder vh = new ImageViewHolder(v);
            return vh;
        } else {
            // create a new view
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.childlayout, parent, false);
            // set the view's size, margins, paddings and layout parameters
            TextViewHolder vh = new TextViewHolder(v);
            return vh;
        }
    }

我的问题是布局设计,我希望rowlayout高于childlayout怎么做?

这是主要活动

setContentView(R.layout.activity_recycler_view);

    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    mRecyclerView.setHasFixedSize(true);
   // LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
    mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
    mRecyclerView.setLayoutManager(mLayoutManager);
    mAdapter = new MyAdapter(getDataSet());
    mRecyclerView.setAdapter(mAdapter);
    RecyclerView.ItemDecoration itemDecoration =
            new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
    mRecyclerView.addItemDecoration(itemDecoration);

这是UPDATED布局

的RowLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="6dp">

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="TODO"
        android:src="@drawable/error" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/firstLine"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Example application"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/secondLine"
            android:layout_width="fill_parent"
            android:layout_height="26dip"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:text="Description"
            android:textSize="12sp" />

    </LinearLayout>

</LinearLayout>

childlayout

<ImageView
    android:id="@+id/header_photo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/success" />

<TextView
    android:id="@+id/textc"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Android Gig"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textStyle="bold" />

根据最近的布局,我有以下错误的结果

enter image description here

我希望得到这样的结果,请注意我正在尝试添加标题&#39;今天&#39;在视图上方 enter image description here

注意滚动条视图水平

2 个答案:

答案 0 :(得分:1)

希望这可以提供帮助:

  • <强> rowlayouts.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="6dp">
    
        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="6dp"
            android:contentDescription="TODO"
            android:src="@drawable/logo" />
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:orientation="vertical">
    
            <TextView
                android:id="@+id/firstLine"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:text="Example application"
                android:textSize="16sp" />
    
            <TextView
                android:id="@+id/secondLine"
                android:layout_width="fill_parent"
                android:layout_height="26dip"
                android:ellipsize="marquee"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:text="Description"
                android:textSize="12sp" />
    
        </LinearLayout>
    
    </LinearLayout>
    
  • <强> childlayout.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="10dp">
    
        <ImageView
            android:id="@+id/header_photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/logo" />
    
        <TextView
            android:id="@+id/textc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"
            android:gravity="center_horizontal"
            android:text="Android Gig"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />
    
    </LinearLayout>
    

答案 1 :(得分:0)

在recyclerview适配器中,您可以覆盖名为getItemViewType()的方法。 请注意,onCreateViewHolder()的参数之一是&#34; int viewType&#34;。此viewType由getItemViewType()方法获取。在这里,您可以编写逻辑以返回所需视图类型的特定数字。

例如: -     @override

public int getItemViewType(int position) {

    // you can return whatever integers you want
    //your logic goes here


    // Just as an example, return 0 or 1 depending on position.
    return position % 2 ;
}`

我不确定你的意思 - &#34;我希望rowlayout高于childlayout&#34;。您可以为RowLayout类型的所有视图返回0,为ChildLayout返回1。