Thursday, July 9, 2015

Place RecyclerView in layout XML

Last example show RecyclerView by calling setContentView() using Java code. Here show how to place RecyclerView in layout XML.


Modify layout XML, layout/activity_main.xml, to place <android.support.v7.widget.RecyclerView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/myrecyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>



Modify com.example.androidrecyclerview.MainActivity.java to call setContentView(R.layout.activity_main) and load myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview).
package com.example.androidrecyclerview;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements RecyclerViewAdapter.OnItemClickListener{

    private RecyclerView myRecyclerView;
    private LinearLayoutManager linearLayoutManager;
    private RecyclerViewAdapter myRecyclerViewAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myRecyclerView = (RecyclerView)findViewById(R.id.myrecyclerview);

        linearLayoutManager =
                new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        /*
        linearLayoutManager =
                new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        */

        myRecyclerViewAdapter = new RecyclerViewAdapter(this);
        myRecyclerViewAdapter.setOnItemClickListener(this);
        myRecyclerView.setAdapter(myRecyclerViewAdapter);
        myRecyclerView.setLayoutManager(linearLayoutManager);

        //insert dummy items
        myRecyclerViewAdapter.add(0, "Eric");
        myRecyclerViewAdapter.add(1, "Android");
        myRecyclerViewAdapter.add(0, "Android-er");
        myRecyclerViewAdapter.add(2, "Google");
        myRecyclerViewAdapter.add(3, "android dev");
        myRecyclerViewAdapter.add(0, "android-er.blogspot.com");
        myRecyclerViewAdapter.add(4, "Peter");
        myRecyclerViewAdapter.add(4, "Paul");
        myRecyclerViewAdapter.add(4, "Mary");
        myRecyclerViewAdapter.add(4, "May");
        myRecyclerViewAdapter.add(4, "Divid");
        myRecyclerViewAdapter.add(4, "Frankie");
    }

    @Override
    public void onItemClick(RecyclerViewAdapter.ItemHolder item, int position) {
        Toast.makeText(this,
                position + " : " + item.getItemName(),
                Toast.LENGTH_SHORT).show();
    }
}


Other files, RecyclerViewAdapter.java and layout_item.xml, refer last exercise of Simple RecyclerView example.

Next:
Add and remove items to RecyclerView, with default animation

No comments:

Post a Comment