Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Recyclerview lag while scrolling down

I have a fragment with a recyclerview (GridlayoutManager) inside. The items in the recyclerview are not complicated or have images but while scrolling down the recyclerview lags (tested on a Nexus 5). I set hasFixedSize and hasStableIds to true Has anyone an idea why this happens? Thanks in advance.

Fragment

public class EditTimetableFragment extends Fragment implements TimetableRecyclerViewAdapter.ClickListener {

public EditTimetableFragment() {}

DAO dao;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;

RecyclerView recyclerViewTimetable;
Spinner spinnerSubject;
TimetableRecyclerViewAdapter timetableRecyclerViewAdapter;
TaskSubjectSpinnerAdapter taskSubjectSpinnerAdapter;
List<Subject> subjectList;
List<Timetable> timetableList;
FloatingActionButton fabAddSubject;
boolean saturday, sunday;
int dayLength, hourLength;
TextView textViewSa, textViewSu;
LinearLayout week, linearLayout;
View view;


public View getView() {
    return view;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this
    dao = new DAO(getActivity());
    getTimetableSettings();
    subjectList = dao.getAllSubjectsAlphabetical();
    timetableList = dao.getTimetableList(dayLength, saturday, sunday);

    view = inflater.inflate(R.layout.fragment_edit_timtable, container, false);

    Toast.makeText(getActivity(),"onCreateView",Toast.LENGTH_SHORT).show();
    linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout);
    fabAddSubject = (FloatingActionButton) view.findViewById(R.id.fabAddSubject);
    fabAddSubject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(getActivity(), AddSubjectActivity.class));
        }
    });

    textViewSa = (TextView) view.findViewById(R.id.textViewSa);
    textViewSu = (TextView) view.findViewById(R.id.textViewSu);
    week = (LinearLayout) view.findViewById(R.id.week);
    setRecyclerViewTimetable(view);
    setSpinnerSubject(view);

    if (saturday && sunday) {
        textViewSa.setVisibility(View.VISIBLE);
        textViewSu.setVisibility(View.VISIBLE);
    } else if (saturday && !sunday) {
        textViewSa.setVisibility(View.VISIBLE);
        textViewSu.setVisibility(View.GONE);
    } else if (!saturday && sunday) {

        textViewSa.setVisibility(View.GONE);
        textViewSu.setVisibility(View.VISIBLE);
    } else if (!saturday && !sunday) {

        textViewSa.setVisibility(View.GONE);
        textViewSu.setVisibility(View.GONE);
    }
    return view;
}

public void setRecyclerViewTimetable(View view) {
    recyclerViewTimetable = (RecyclerView) view.findViewById(R.id.recyclerViewTimetable);
    timetableRecyclerViewAdapter =
            new TimetableRecyclerViewAdapter(
                    getActivity(), true, saturday, sunday, dayLength);
    timetableRecyclerViewAdapter.setClickListener(this);
    recyclerViewTimetable.setHasFixedSize(true);
    recyclerViewTimetable.setLayoutManager(
            new GridLayoutManager(getActivity(), dayCount()));
    recyclerViewTimetable.setAdapter(timetableRecyclerViewAdapter);
}

private void setSpinnerSubject(View view) {
    spinnerSubject = (Spinner) view.findViewById(R.id.spinnerSubject);
    taskSubjectSpinnerAdapter = new TaskSubjectSpinnerAdapter(getActivity(), subjectList);
    spinnerSubject.setAdapter(taskSubjectSpinnerAdapter);
}

private void getTimetableSettings() {
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    editor = sharedPreferences.edit();
    saturday = sharedPreferences.getBoolean(getResources().getString(R.string.settingSaturday), false);
    sunday = sharedPreferences.getBoolean(getResources().getString(R.string.settingSunday), false);
    dayLength = sharedPreferences.getInt(getResources().getString(R.string.settingDay), 10);
    hourLength = sharedPreferences.getInt(getResources().getString(R.string.settingHour), 45);
    //Toast.makeText(getActivity(), "dayCount=" + dayCount() + " Sat" + saturday + " Sun" + sunday, Toast.LENGTH_SHORT).show();
}

private int dayCount() {
    int a = 0;
    if (saturday && sunday) {
        a = 7;
    } else if (saturday && !sunday) {
        a = 6;
    } else if (!saturday && sunday) {
        a = 6;
    } else if (!saturday && !sunday) {
        a = 5;
    }
    return a;
}

@Override
public void onResume() {
    super.onResume();
    getTimetableSettings();
    subjectList = dao.getAllSubjectsAlphabetical();
    timetableList = dao.getTimetableList(dayLength, saturday, sunday);

    setSpinnerSubject(view);
    setRecyclerViewTimetable(view);

    if (saturday && sunday) {
        textViewSa.setVisibility(View.VISIBLE);
        textViewSu.setVisibility(View.VISIBLE);
    } else if (saturday && !sunday) {
        textViewSa.setVisibility(View.VISIBLE);
        textViewSu.setVisibility(View.GONE);
    } else if (!saturday && sunday) {
        textViewSa.setVisibility(View.GONE);
        textViewSu.setVisibility(View.VISIBLE);
    } else if (!saturday && !sunday) {
        textViewSa.setVisibility(View.GONE);
        textViewSu.setVisibility(View.GONE);
    }
}

@Override
public void itemClicked(View v, int position) {
    timetableList = dao.getTimetableList(dayLength, saturday, sunday);
    subjectList = dao.getAllSubjectsAlphabetical();

    if (!subjectList.isEmpty()) {
        Timetable timetableOld = timetableList.get(position);
        Subject subject = subjectList.get(spinnerSubject.getSelectedItemPosition());
        Timetable timetableNew;
        if (timetableOld.getSubjectId().equals(subject.getId())) {
            timetableNew = dao.updateTimetable(timetableOld.getX(), timetableOld.getY(), "");
        } else {
            timetableNew = dao.updateTimetable(timetableOld.getX(), timetableOld.getY(), subject.getId());
        }
        timetableRecyclerViewAdapter.update(timetableNew, position);
    } else {
        Snackbar.make(linearLayout, R.string.AddSubjectsQuestion, Snackbar.LENGTH_LONG)
                .setAction(R.string.Yes, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(getActivity(), AddSubjectActivity.class));
                    }
                }).show();
    }
  }
}

Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.feserware.learn.Timetable.EditTimetableActivity">

<include
    android:id="@+id/week"
    layout="@layout/week" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewTimetable"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/linearLayout"
        android:layout_weight="1" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="false"
        android:layout_alignParentStart="true"
        android:layout_gravity="center_horizontal"
        android:background="@color/primary"
        android:minHeight="?attr/actionBarSize"
        android:orientation="horizontal"
        android:padding="19dp">

        <Spinner
            android:id="@+id/spinnerSubject"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />

    </LinearLayout>

    <com.melnykov.fab.FloatingActionButton
        android:id="@+id/fabAddSubject"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_margin="16dp"
        android:src="@android:drawable/ic_input_add"
        android:tint="#fff"
        app:fab_type="mini" />


</RelativeLayout>

Adapter

public class TimetableRecyclerViewAdapter
    extends RecyclerView.Adapter
    <TimetableRecyclerViewAdapter.timetableViewHolder> {

List<Timetable> timetableList;
Context context;
ClickListener clickListener;
LayoutInflater inflater;
DAO dao;
boolean showHour, saturday, sunday;
int hours;

public TimetableRecyclerViewAdapter(Context context, boolean showHour, boolean saturday, boolean sunday, int hours) {
    this.inflater = LayoutInflater.from(context);
    this.context = context;
    dao = new DAO(context);
    this.timetableList = dao.getTimetableList(hours, saturday, sunday);
    this.showHour = showHour;
    this.saturday = saturday;
    this.sunday = sunday;
    this.hours = hours;
    setHasStableIds(true);
}


@Override
public timetableViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.item_timetable, parent, false);
    timetableViewHolder holder = new timetableViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(timetableViewHolder holder, int position) {
    Timetable timetable = timetableList.get(position);
    Subject subject = dao.getSubject(timetable.getSubjectId());
    holder.textViewTimetableSubjectShort.setText("");
    holder.textViewTimetableRoom.setText("");


    if (subject == null) {
        dao.updateTimetable(timetable.getX(), timetable.getY(), "");
    }

    if (timetable.getSubjectId().isEmpty()) {
        holder.textViewTimetableSubjectShort.setText(timetable.getY() + "");
        holder.linearLayoutBackground.setBackgroundColor(Color.WHITE);
    } else {
        holder.textViewTimetableSubjectShort.setText(subject.getSubjectShort());
        holder.textViewTimetableRoom.setText(subject.getRoom());
        holder.linearLayoutBackground.setBackgroundColor(subject.getColor());
    }

}


@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemCount() {
    return timetableList.size();
}

public void setClickListener(ClickListener clickListener) {
    this.clickListener = clickListener;
}

class timetableViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView textViewTimetableSubjectShort, textViewTimetableRoom;
    LinearLayout linearLayoutBackground;

    public timetableViewHolder(View itemView) {
        super(itemView);
        textViewTimetableSubjectShort = (TextView) itemView.findViewById(R.id.textViewSubjectShort);
        textViewTimetableRoom = (TextView) itemView.findViewById(R.id.textViewSubjectRoom);
        linearLayoutBackground = (LinearLayout) itemView.findViewById(R.id.linearLayoutBackground);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (clickListener != null) {
            clickListener.itemClicked(v, getLayoutPosition());
        }
    }
}

public interface ClickListener {
    void itemClicked(View v, int postion);
}

public void update(Timetable timetable, int pos) {
    timetableList.set(pos, timetable);
    notifyItemChanged(pos);
  }
}

LogCat

 09-11 17:48:32.098  31535-31535/com.feserware.learn D/TimetableRecyclerVH﹕ Constructor
09-11 17:48:32.148  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.148  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.156  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 0
09-11 17:48:32.159  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.159  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.165  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 1
09-11 17:48:32.169  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.170  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.175  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 2
09-11 17:48:32.178  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.178  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.184  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 3
09-11 17:48:32.188  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.189  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.195  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 4
09-11 17:48:32.203  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.203  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.209  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 5
09-11 17:48:32.211  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.211  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.217  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 6
09-11 17:48:32.222  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.222  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.228  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 7
09-11 17:48:32.231  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.231  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.237  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 8
09-11 17:48:32.239  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.239  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.246  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 9
09-11 17:48:32.252  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.252  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.259  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 10
09-11 17:48:32.261  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.261  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.264  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 11
09-11 17:48:32.266  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.266  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.269  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 12
09-11 17:48:32.272  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.272  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.276  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 13
09-11 17:48:32.278  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.278  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.281  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 14
09-11 17:48:32.288  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.288  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.292  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 15
09-11 17:48:32.320  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.320  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.326  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 16
09-11 17:48:32.328  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.328  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.333  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 17
09-11 17:48:32.335  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.335  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.342  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 18
09-11 17:48:32.344  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.344  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.349  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 19
09-11 17:48:32.358  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.358  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.367  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 20
09-11 17:48:32.370  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.370  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.376  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 21
09-11 17:48:32.378  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.378  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.383  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 22
09-11 17:48:32.385  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.385  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.393  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 23
09-11 17:48:32.395  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.395  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.400  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 24
09-11 17:48:32.422  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.422  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.430  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 25
09-11 17:48:32.432  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.432  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.437  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 26
09-11 17:48:32.443  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.443  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.450  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 27
09-11 17:48:32.456  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.457  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.464  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 28
09-11 17:48:32.466  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:32.466  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:32.469  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 29
09-11 17:48:32.631  31535-31589/com.feserware.learn D/OpenGLRenderer﹕ endAllStagingAnimators on 0xa1b9ed00 (RippleDrawable) with handle 0xa188dfb0
09-11 17:48:35.406  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:35.406  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:35.410  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 30
09-11 17:48:35.429  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:35.429  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:35.432  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 31
09-11 17:48:35.446  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:35.446  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:35.450  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 32
09-11 17:48:35.464  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:35.464  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:35.469  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 33
09-11 17:48:35.482  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:35.483  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:35.487  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 34
09-11 17:48:35.505  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 35
09-11 17:48:35.509  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 36
09-11 17:48:35.524  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 37
09-11 17:48:35.538  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 38
09-11 17:48:35.552  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:35.552  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:35.557  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 39
09-11 17:48:35.575  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 40
09-11 17:48:35.580  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 41
09-11 17:48:35.596  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 42
09-11 17:48:35.611  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 43
09-11 17:48:35.632  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:35.632  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:35.637  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 44
09-11 17:48:35.654  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 45
09-11 17:48:35.668  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 46
09-11 17:48:35.682  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 47
09-11 17:48:35.696  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 48
09-11 17:48:35.711  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 49
09-11 17:48:35.764  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 50
09-11 17:48:35.781  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 51
09-11 17:48:35.797  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 52
09-11 17:48:35.813  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 53
09-11 17:48:35.827  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:35.827  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:35.831  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 54
09-11 17:48:35.863  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 55
09-11 17:48:35.912  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 56
09-11 17:48:35.926  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 57
09-11 17:48:35.943  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 58
09-11 17:48:35.962  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 59
09-11 17:48:35.981  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 60
09-11 17:48:35.999  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 61
09-11 17:48:36.016  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 62
09-11 17:48:36.037  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 63
09-11 17:48:36.053  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:36.053  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:36.057  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 64
09-11 17:48:36.077  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 65
09-11 17:48:36.091  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 66
09-11 17:48:36.108  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 67
09-11 17:48:36.125  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 68
09-11 17:48:36.143  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 69
09-11 17:48:36.161  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 70
09-11 17:48:36.176  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 71
09-11 17:48:36.191  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 72
09-11 17:48:36.206  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 73
09-11 17:48:36.228  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:36.228  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:36.232  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 74
09-11 17:48:36.249  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 75
09-11 17:48:36.264  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 76
09-11 17:48:36.278  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 77
09-11 17:48:36.293  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 78
09-11 17:48:36.306  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 79
09-11 17:48:36.324  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 80
09-11 17:48:36.342  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 81
09-11 17:48:36.357  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 82
09-11 17:48:36.371  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 83
09-11 17:48:36.383  31535-31535/com.feserware.learn D/TtRecycler﹕ timetableViewHolder
09-11 17:48:36.383  31535-31535/com.feserware.learn D/TtRecycler﹕ onCreateViewHolder
09-11 17:48:36.387  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 84
09-11 17:48:36.410  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 85
09-11 17:48:36.424  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 86
09-11 17:48:36.438  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 87
09-11 17:48:36.454  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 88
09-11 17:48:36.470  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 89
09-11 17:48:36.498  31535-31535/com.feserware.learn I/Choreographer﹕ Skipped 31 frames!  The application may be doing too much work on its main thread.
09-11 17:48:36.539  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 90
09-11 17:48:36.556  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 91
09-11 17:48:36.571  31535-31535/com.feserware.learn D/TtRecycler﹕ onBindViewHolder | 92
like image 217
Johann Feser Avatar asked Sep 10 '15 16:09

Johann Feser


1 Answers

Ok I do not know what exactly is wrong with your code but what I can do is provide you with a sample recycler code I use, am sure you can do a comparison with yours and figure our where you might be going wrong, it is an expandable recycler with the child having a horizontal listview with images in it, I do not experience lags I use a huawei Y300, thats a low end phone, i chose that phone bacause i knew if my code would work great on it then it would on most phones so here is a sample of my Expandable recycler adapter, my code though suffers from memory usage it shoots to about 20mb after loading about 20 items each with 5 images

public class ExpandableCategoryAdapter extends ExpandableRecyclerAdapter<CategoryParentViewHolder, CategoryChildViewHolder> {
/**
 * Primary constructor. Sets up {@link #mParentItemList} and {@link #mItemList}.
 * <p>
 * Changes to {@link #mParentItemList} should be made through add/remove methods in
 * {@link ExpandableRecyclerAdapter}
 *
 * @param parentItemList List of all {@link ParentListItem} objects to be
 *                       displayed in the RecyclerView that this
 *                       adapter is linked to
 */
private LayoutInflater mInflater;
private CategoryChildViewHolder childViewHolder;
private Context context;
private UserDataHolderDB db;

//click onitemlisteners
//I chose these because they
//readily provide the IDS/position
private OnItemClickListener addItemListener;
private OnItemClickListener gotoListener;
public ExpandableCategoryAdapter(OnItemClickListener addItemListener,  OnItemClickListener gotoListener,Context context, ArrayList<ExpandableCategoryItem> parentItemList) {
    super(parentItemList);
    this.addItemListener = addItemListener;
    this.gotoListener = gotoListener;
    this.context = context;
    mInflater = LayoutInflater.from(context);
    db = new UserDataHolderDB(context);
}

//ofcos the interface to implement from
public interface OnItemClickListener {
    public void onItemClick(View view, int position);
}

@Override
public CategoryParentViewHolder onCreateParentViewHolder(ViewGroup parentViewGroup) {
    View view = mInflater.inflate(R.layout.parent_category, parentViewGroup, false);
    return new CategoryParentViewHolder(view);
}

@Override
public CategoryChildViewHolder onCreateChildViewHolder(ViewGroup childViewGroup) {
    View view = mInflater.inflate(R.layout.cloth_min_adapter, childViewGroup, false);
    return new CategoryChildViewHolder(view);
}

@Override
public void onBindParentViewHolder(CategoryParentViewHolder parentViewHolder, int position, ParentListItem parentListItem) {
    parentViewHolder.setIsRecyclable(true);
    ExpandableCategoryItem parentData = (ExpandableCategoryItem) parentListItem;
    parentViewHolder.mCrimeTitleTextView.setText(parentData.getmDescription());

    try {
        System.err.println("Palette value: " + parentData.getmSideBarColor());
        if (parentData.getmSideBarColor() != null) {
            parentViewHolder.mSideBar.setBackgroundColor(Integer.parseInt(parentData.getmSideBarColor()));
            parentViewHolder.mSideBar.invalidate();

        }
    }catch (Exception e)
    {
        e.printStackTrace();
        System.err.println("Palette value could not be decoded");
    }

    if(!parentViewHolder.isExpanded())
    {

            try{
                if(childViewHolder.linearLayout != null) {
                    childViewHolder.linearLayout.removeAllViews();

                    if(parentData.getmSideBarColor() != null)
                    {

                        if(parentData.getmSideBarColor().isEmpty())
                        {

                        }

                    }
               // parentViewHolder.mSideBar.getBackground() =
                }
            }catch (Exception e)
            {
                e.printStackTrace();
            }
        //trash child to save memory

    }

    try {
        //reduce bitmap size to save on memory
        Bitmap bitmap = BitmapFactory.decodeFile(parentData.getmCategoryImgPath());
        bitmap = ResizeImage.getResizedBitmap(bitmap, 100, 100);
        parentViewHolder.mCategoryImage.setImageBitmap(bitmap);
    }catch (Exception e)
    {
        e.printStackTrace();
    }

    /
    System.err.println("Parent ID"+parentData.getId());
    System.err.println("Parent Description at Given ID: "+parentData.getmDescription());

    parentViewHolder.mItemsShow.setText(db.categoryItemsNumber(parentData.getId()));
}

@Override
public void onBindChildViewHolder(CategoryChildViewHolder childViewHolder, int position, Object childListItem) {


    //Button being assigned the Listener
    childViewHolder.gotTo.setOnClickListener(v -> gotoListener.onItemClick(v, position));
    childViewHolder.addNewItem.setOnClickListener(v -> addItemListener.onItemClick(v, position));



    ChildItem data = (ChildItem) childListItem;
    this.childViewHolder = childViewHolder;
    int rootViewController = 0;

    try {
        childViewHolder.linearLayout.removeAllViews();
    }catch (Exception w)
    {
        w.printStackTrace();
    }
    if(data.getImgUri() != null) {

        for (String item : data.getImgUri()) {

            try {
                //stop adding images we are past our limit
                if (rootViewController != 5) {

                    if(item.equals("") || !item.isEmpty()) {
                        Bitmap bitmap = BitmapFactory.decodeFile(item);
                        bitmap = ResizeImage.getResizedBitmap(bitmap, 100, 100);
                        Bitmap bitmap2 = ImageCornerMoulder.getRoundedCornerBitmap(bitmap, calculatePercentage(5, bitmap.getHeight()));

                        //add images we still have enough space
                        rootViewController = (rootViewController + 1);
                        View rootView = LayoutInflater.from(context).inflate(R.layout.expandable_image_item, null);
                        ImageView imageView = (ImageView) rootView.findViewById(R.id.clothItem);
                        rootView.setTag(new ImageObject(Uri.parse(item)));
                        imageView.setImageBitmap(bitmap2);
                        if(bitmap != bitmap2)
                        {
                            bitmap.recycle();
                        }

                        childViewHolder.linearLayout.addView(rootView, 0);

                    }
                }
            } catch (Exception e) {
                e.printStackTrace();


                //set default image
                    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.clothes);
                    bitmap = ResizeImage.getResizedBitmap(bitmap, 100, 100);
                    Bitmap bitmap2 = ImageCornerMoulder.getRoundedCornerBitmap(bitmap, calculatePercentage(5, bitmap.getHeight()));
                    //add images we still have enough space
                    rootViewController = (rootViewController + 1);
                    View rootView = LayoutInflater.from(context).inflate(R.layout.expandable_image_item, null);
                    ImageView imageView = (ImageView) rootView.findViewById(R.id.clothItem);


                imageView.setImageBitmap(bitmap);
                    if(bitmap != bitmap) {
                        bitmap.recycle();
                    }
                    childViewHolder.linearLayout.addView(rootView, 0);


            }
        }

    }


        childViewHolder.linearLayout.invalidate();



}


private int calculatePercentage(int percentage, int target)
{
    int k = (int)(target*(percentage/100.0f));
    return k;

}

 }

how i use it

  adapter = new ExpandableCategoryAdapter(addItems,goTo,getBaseContext(),  data arraylist);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
                        recyclerView.setAdapter(adapter);
like image 143
life evader Avatar answered Oct 26 '22 00:10

life evader