How to get recyclerview to work


Hi, i know recyclerview is suggested to use when working with list in Android. It is a good replacement of listview. However, i can't get it to work in my application. Please tell me procedures or steps to successfully setup the recyclerview.

By dany2012 asked on 2015-06-18
answer to this question

yuk.dara
By yuk.dara

RecycleView is part of material design. So, in RecyclerView setup process, first you have to apply a material theme to you application. You modify your styles.xml file to use material style as shown below.
<resources>   
<style name="MaterialTheme" parent="MyTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="MyTheme.Base" parent="Theme.AppCompat">
<item name="android:windowNoTitle">true</item>
<item name="colorPrimary">#26A69A</item>
<item name="colorPrimaryDark">#004D40</item>
<item name="colorAccent">#B2DFDB</item>
<!-- <item name="windowActionBar">false</item> -->
</style>
</resources>

Then you open AndroidManifest.xml file and apply the style to your application via android:theme="@style/MaterialTheme".
After that, you add recyclerview support library to dependencies list in build.gradle file.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:+'
}

In the activit_main.xml file, you add a RecycleView view.
<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:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity"
>
<EditText
android:id="@+id/txtsearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">

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

In the layout directory, you create list_item.xml file to define row template of the list. In the file, you add a TextView to display.
<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:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity"
>
<EditText
android:id="@+id/txtsearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search"
/>
<android.support.v7.widget.RecyclerView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>

In the main/java/your_project_package directory, you create RAdapter java class as shown below. It is an adapter used to bind data to the RecyclerView.
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class RAdapter extends RecyclerView.Adapter<RAdapter.ViewHolder>{
private ArrayList<String> mDataset;
public RAdapter(ArrayList<String> mDataset){
this.mDataset=mDataset;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txt;
public ViewHolder(View v) {
super(v);
// Create reference to the TextView
txt = (TextView) v.findViewById(R.id.txtitem);
}
}
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
// inflate list_item layout file
View v = LayoutInflater.from(parent.getContext()).inflate
(R.layout.list_item, parent, false);
// Create ViewHolder instance
ViewHolder vh = new ViewHolder(v);
return vh;
}

public void onBindViewHolder(ViewHolder holder, int position) {
// Bind data to TextView
final String name = mDataset.get(position);
holder.txt.setText(mDataset.get(position));
holder.txt.setText( mDataset.get(position));
}
@Override
public int getItemCount() {
return mDataset.size();
}
}

In the onCreate() method of the MainActivity class, you write code to create a reference to the RecyclerView, and initialize list data.
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.Arrays;

public class MainActivity extends ActionBarActivity {
private RecyclerView recyclerView;
private RAdapter adapter;
private LayoutManager layoutManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create a reference to the RecyclerView
recyclerView=(RecyclerView)findViewById(R.id.recyclerview);
// use a linear layout manager
layoutManager= new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
// Initialize list data
String[] items=new String[]{"Canada","China","Japan","USA"};
ArrayList<String> listItems=new ArrayList<>(Arrays.asList(items));
adapter=new RAdapter(listItems);
recyclerView.setAdapter(adapter);
}
}


2015-06-18 Reply





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.