Skip to content

Instantly share code, notes, and snippets.

@farbodsz
Last active June 5, 2020 11:40
Show Gist options
  • Save farbodsz/7646564f48ee708c1582c013e1de4f07 to your computer and use it in GitHub Desktop.
Save farbodsz/7646564f48ee708c1582c013e1de4f07 to your computer and use it in GitHub Desktop.
A basic example to demonstrate how RecyclerViews can be used
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2016 Farbod Salamat-Zadeh
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
style="@style/AppStyle.Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
/*
* Copyright 2016 Farbod Salamat-Zadeh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yourcompany.yourapp;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
public class ExampleViewHolder extends RecyclerView.ViewHolder {
TextView text1, text2;
ExampleViewHolder(View itemView) {
super(itemView);
text1 = (TextView) itemView.findViewById(R.id.text1);
text2 = (TextView) itemView.findViewById(R.id.text2);
}
}
private ArrayList<CustomClass> mCustomObjects;
public ExampleAdapter(ArrayList<CustomClass> arrayList) {
mCustomObjects = arrayList;
}
@Override
public int getItemCount() {
return mCustomObjects.size();
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_2, parent, false);
return new ExampleViewHolder(view);
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
CustomClass object = mCustomObjects.get(position);
// My example assumes CustomClass objects have getFirstText() and getSecondText() methods
String firstText = object.getFirstText()
String secondText = object.getSecondText()
holder.text1.setText(firstText);
holder.text2.setText(secondText);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
}
/*
* Copyright 2016 Farbod Salamat-Zadeh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yourcompany.yourapp;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class ExampleClickAdapter extends RecyclerView.Adapter<ExampleClickAdapter.ExampleClickViewHolder> {
public class ExampleClickViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView text1, text2;
ExampleClickViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
title = (TextView) itemView.findViewById(android.R.id.text1);
subtitle = (TextView) itemView.findViewById(android.R.id.text2);
}
@Override
public void onClick(View v) {
// The user may not set a click listener for list items, in which case our listener
// will be null, so we need to check for this
if (mOnEntryClickListener != null) {
mOnEntryClickListener.onEntryClick(v, getLayoutPosition());
}
}
}
private ArrayList<CustomClass> mCustomObjects;
public ExampleClickAdapter(ArrayList<CustomClass> arrayList) {
mCustomObjects = arrayList;
}
@Override
public int getItemCount() {
return mCustomObjects.size();
}
@Override
public ExampleClickViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_2, parent, false);
return new ExampleClickViewHolder(view);
}
@Override
public void onBindViewHolder(ExampleClickViewHolder holder, int position) {
CustomClass object = mCustomObjects.get(position);
// My example assumes CustomClass objects have getFirstText() and getSecondText() methods
String firstText = object.getFirstText()
String secondText = object.getSecondText()
holder.text1.setText(firstText);
holder.text2.setText(secondText);
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
private OnEntryClickListener mOnEntryClickListener;
public interface OnEntryClickListener {
void onEntryClick(View view, int position);
}
public void setOnEntryClickListener(OnEntryClickListener onEntryClickListener) {
mOnEntryClickListener = onEntryClickListener;
}
}
/*
* Copyright 2016 Farbod Salamat-Zadeh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yourcompany.yourapp;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import java.util.ArrayList;
public class RecyclerViewActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting up the Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Setting up the RecyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
assert recyclerView != null;
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// Getting your ArrayList - this will be up to you
ArrayList<CustomClass> yourObjects = getMyObjects();
// Standard RecyclerView implementation
ExampleAdapter adapter = new ExampleAdapter(yourObjects);
recyclerView.setAdapter(adapter);
// OR RecyclerView with a click listener
ExampleClickAdapter clickAdapter = new ExampleClickAdapter(yourObjects);
clickAdapter.setOnEntryClickListener(new ExampleClickAdapter.OnEntryClickListener() {
@Override
public void onEntryClick(View view, int position) {
// stuff that will happen when a list item is clicked
}
});
recyclerView.setAdapter(clickAdapter);
}
private ArrayList<CustomClass> getMyObjects() {
// This will return the ArrayList of your CustomClass objects
}
}
@isthisshivam
Copy link

It is good.

@alaincheng
Copy link

I implemented everything but i got an error on loading the screen.

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.alaincheng.loginapp.ExampleClickAdapter.onBindViewHolder(ExampleClickAdapter.java:77)
at com.example.alaincheng.loginapp.ExampleClickAdapter.onBindViewHolder(ExampleClickAdapter.java:29)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:

I just followed all the source code, any suggestions?

Kind regards

@andrewm898
Copy link

Hey,

The form of this code that won't throw the NullPointerException that @alaincheng is talking about requires a change to line 36 and 37;

Instead of
title = (TextView) itemView.findViewById(R.id.text1);
subtitle = (TextView) itemView.findViewById(R.id.text2);

you need to write

title = (TextView) itemView.findViewById(android.R.id.text1);
subtitle = (TextView) itemView.findViewById(android.R.id.text2);

Hope this helps

@farbodsz
Copy link
Author

@andrewm898

Hey,

The form of this code that won't throw the NullPointerException that @alaincheng is talking about requires a change to line 36 and 37;

Instead of
title = (TextView) itemView.findViewById(R.id.text1);
subtitle = (TextView) itemView.findViewById(R.id.text2);

you need to write

title = (TextView) itemView.findViewById(android.R.id.text1);
subtitle = (TextView) itemView.findViewById(android.R.id.text2);

Hope this helps

Good point. I've updated the file now so it should be ok.

@tag2biswa
Copy link

By changing this its not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment