Moudiz
Moudiz

Reputation: 7377

orginase the layout views in recyclerviews

I have 2 views in a recyclerview

  @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;
        }
    }

my problem is withe layout design , I want the rowlayout to be above childlayout how to do that ?

this is main activity

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);

this is the UPDATED layouts

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" />

With the recent layout I have the below wrong results

enter image description here

I want to have such result , please notice I am trying to add a title 'today' above the view enter image description here

notice that there are scroller view horizontal

Upvotes: 0

Views: 78

Answers (2)

andrea.petreri
andrea.petreri

Reputation: 4147

Hope this could help:

  • 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>
    

Upvotes: 1

karthik prasad
karthik prasad

Reputation: 728

In your recyclerview adapter you can override a method called getItemViewType(). Notice that one off the parameters for onCreateViewHolder() is "int viewType". This viewType is got by the getItemViewType() method. Here you can write your logic to return a certain number for the type of view you want.

Eg: - @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 ;
}`

I wasn't sure what you meant by - "I want the rowlayout to be above childlayout". You can maybe return 0 for all views of type RowLayout and 1 for ChildLayout.

Upvotes: 0

Related Questions