solarrobor solarrobor - 4 months ago 24
Android Question

ver 23.4.0 RecyclerView List not working correctly in Android 4.3

My problem is that the RecyclerView list appears but starts somewhere in the middle of the list and displays just a single row at a time instead of multiple rows. I can scroll up to the first item in the list but I cannot scroll down. I have found some 'questions' on stackoverflow concerning RecyclerView usage in Android 4.3 but nobody seems to have a similar problem to me. With an earlier version of RecyclerView it worked fine. Using a Samsung Galaxy SIII.

I using the recyclerview in my app as follows:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:name="na.com.company.myapp.List"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
tools:context="na.com.company.myapp.List"
tools:listitem="@layout/a_row"/>


Each row in the list is made up of the following:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView android:id="@+id/common_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:paddingTop="10dp"
android:textSize="18sp"
android:textStyle="bold"/>

<TextView android:id="@+id/latin_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/common_name"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:paddingBottom="10dp"
android:textStyle="italic"/>

<CheckBox android:id="@+id/select_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingBottom="10dp"
android:paddingTop="10dp"/>

</RelativeLayout>


The code within the AppCompatActivity derived class is as follows (in the onCreate function):

RecyclerView recyclerView = (RecyclerView)findViewById(R.id.list);
LinearLayoutManager linear_layout = new LinearLayoutManager(this);
linear_layout.setOrientation(LinearLayoutManager.VERTICAL);
linear_layout.setAutoMeasureEnabled(true);
recyclerView.setLayoutManager(linear_layout);
Drawable dividerDrawable = ContextCompat.getDrawable(this, R.drawable.list_divider);
RecyclerView.ItemDecoration divider_decoration = new ListDivider(dividerDrawable);
recyclerView.addItemDecoration(divider_decoration);


The app build.gradle file contains:

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:support-v4:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'


The ViewHolder code:

public class TheViewHolder extends RecyclerView.ViewHolder implements OnClickListener
{
private View the_view = null;
private CheckBox selected_item = null;

public TheViewHolder(final View individual_view)
{
super(individual_view);
the_view = individual_view;
the_view.setOnClickListener(this);
selected_item = (CheckBox)the_view.findViewById(R.id.select_box);
}

public void AttachCursor(final Cursor data_cursor, String[] columns, int[] views, boolean is_selected)
{
TextView a_view = (TextView) the_view.findViewById(views[0]);
a_view.setText(data_cursor.getString(data_cursor.getColumnIndex(columns[0])));
a_view = (TextView) the_view.findViewById(views[1]);
a_view.setText(data_cursor.getString(data_cursor.getColumnIndex(columns[1])) + " " + data_cursor.getString(data_cursor.getColumnIndex(columns[2])));
selected_item.setChecked(is_selected);
}

@Override
public void onClick(View clicked_view)
{
selected_item.setChecked(true);
}
}


and the Adapter (query_cursor.getCount() in the getItemCount function currently returns a value of 691):

public class SimpleCursorRecyclerViewAdapter extends RecyclerView.Adapter<TheViewHolder>
{
private Cursor query_cursor = null;
private int layout_resource = 0;
private String[] table_columns;
private int[] layout_views;
private View layout_view = null;
private boolean checkbox_list[] = null;

public SimpleCursorRecyclerViewAdapter(Context context, int layout_id, Cursor data_cursor, String[] columns, int[] views)
{
layout_resource = layout_id;
query_cursor = data_cursor;
table_columns = columns;
layout_views = views;
}

public Cursor get_item(final int position)
{
if (query_cursor != null && !query_cursor.isClosed())
{
query_cursor.moveToPosition(position);
}

return query_cursor;
}

@Override
public int getItemCount()
{
int size = 0;
if (query_cursor != null)
{
size = query_cursor.getCount();
checkbox_list = new boolean[size];
}
return size;
}

@Override
public void onBindViewHolder(TheViewHolder view_holder, int position)
{
Cursor current_cursor = get_item(position);
view_holder.AttachCursor(current_cursor, table_columns, layout_views, checkbox_list[position]);
}

@Override
public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
if (layout_view == null)
{
layout_view = LayoutInflater.from(parent.getContext()).inflate(layout_resource, parent, false);
}
return new TheViewHolder(layout_view);
}
}

Answer

The problem lay with my code, in particular this method:

@Override
public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    if (layout_view == null)
    {
        layout_view = LayoutInflater.from(parent.getContext()).inflate(layout_resource, parent, false);
    }
    return new TheViewHolder(layout_view);
}

The list now appears is it should be when I rewrote the onCreateViewHolder as:

 @Override
 public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
 {
    layout_view = LayoutInflater.from(parent.getContext()).inflate(layout_resource, parent, false);
    return new TheViewHolder(layout_view);
}
Comments