I have used RecyclerView several times before, but this is the first time it is running too slowly.
In this case, the elements are represented by a simple LinearLayout with 3 views inside it:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/linearLayout" android:orientation="horizontal" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/tvTicketNumber" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:gravity="center" android:padding="8dp"/> <EditText android:id="@+id/etTotalSold" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:padding="8dp" android:gravity="center" /> <TextView android:id="@+id/tvSurplus" android:layout_width="0dp" android:layout_weight="1" android:padding="8dp" android:layout_height="match_parent" android:gravity="center"/> </LinearLayout>
RecyclerView uses the previous layout in its adapter:
public class TicketAdapter extends RecyclerView.Adapter<TicketAdapter.ViewHolder> { private ArrayList<Ticket> dataSet; // Define references to the views for each data item public static class ViewHolder extends RecyclerView.ViewHolder { public TextView tvTicketNumber, tvSurplus; public EditText etQuantity; public ViewHolder(View v) { super(v); tvTicketNumber = (TextView) v.findViewById(R.id.tvTicketNumber); etQuantity = (EditText) v.findViewById(R.id.etQuantity); tvSurplus = (TextView) v.findViewById(R.id.tvSurplus); } } public TicketAdapter() { dataSet = new ArrayList<>(); } public void setDataSet(ArrayList<Ticket> dataSet) { this.dataSet = dataSet; } private String twoDigits(final int i) { final String pre = (i<=9 ? "0" : ""); return pre + i; } @Override public TicketAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.table_row, parent, false); // set the view size, margins, padding and layout parameters return new ViewHolder(v); } // Replace the contents of a view (invoked by the layout manager) @Override public void onBindViewHolder(ViewHolder holder, int position) { // get element from the data set Ticket ticket = dataSet.get(position); // replace the contents of the view with that element holder.tvTicketNumber.setText(twoDigits(position)); holder.tvSurplus.setText("6"); } @Override public int getItemCount() { return dataSet.size(); } }
I used to use TableLayout with TableRows created programmatically, but I read that the layout should be used for a certain number of rows in XML.
I need to load a list of 100 elements in a fragment. But loading takes about 4 seconds. For this reason, I wrote some logic to show the progressBar, and then hide it and show scrollView (the recycler is inside it).
The fragmentation process was still slow, so I moved the code to the onViewCreated method. The transaction was still slow, and I added AsyncTask. With this last change, the transaction is faster, but the progressBar stops constantly and the buttons cannot be used (onPostExecute takes 4 seconds to load and show the recyclerView).
I want to show the progressBar animation, but onPostExecute runs in the user interface thread and the whole application stops for 4 seconds while the RecyclerView loads.
Please give me some ideas. Before I used elements with images downloaded from the Internet, and RecyclerView was faster. This is too weird.