1

I am using recyclerview to load data from my server. There are no images only the texts will be loaded from the server. But the loading speed is quite slow.Now , I want to know if there are any ways to improve the load time. Although I checked several blogs and questions I wasn't able to fix my issue. I have checked this one: Fast load RecyclerView withing ScrollView , but wasn't able to fix my issue.

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout 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:id="@+id/swipe_layout"
    tools:context=".Activities.SwitchCompany">


    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:id="@+id/companySwitchRecyclerViewXML"/>

</android.support.v4.widget.SwipeRefreshLayout>

Java: My adapter class

public class RecyclerViewAdapterForCompanySwitch extends RecyclerView.Adapter<RecyclerViewAdapterForCompanySwitch.CompanyViewHolder> {
    private Context context;
    private ArrayList<CompanyModel> companyInformationArrayList;

    public RecyclerViewAdapterForCompanySwitch(Context context,ArrayList<CompanyModel> companyInformationArrayList){
        this.context=context;
        this.companyInformationArrayList =companyInformationArrayList;
    }

    @Override
    public CompanyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_row_style_for_switch_company_layout,parent,false);
        RecyclerViewAdapterForCompanySwitch.CompanyViewHolder companyViewHolder=new RecyclerViewAdapterForCompanySwitch.CompanyViewHolder(view);
        return companyViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull CompanyViewHolder holder, int position) {
        final CompanyModel companyModel= companyInformationArrayList.get(position);
        holder.companyName.setText(companyModel.getCompanyName());
        holder.goButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, HomeActivity.class).putExtra("companyID",companyModel.getId()).putExtra("companyName",companyModel.getCompanyName())
                .putExtra("companyEmail",companyModel.getEmail()));
            }
        });
    }

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

    public class CompanyViewHolder extends RecyclerView.ViewHolder{
        TextView companyName;
        Button goButton;
        public CompanyViewHolder(View itemView) {
            super(itemView);
            companyName=itemView.findViewById(R.id.companyNameXML);
            goButton=itemView.findViewById(R.id.goButtonXML);
        }
    }
}

My recyclerview_row_style_for_switch_company_layout code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/layout_shadow"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Company Name"
        android:textColor="@android:color/white"
        android:background="@color/colorPrimary"
        android:textStyle="bold"
        android:gravity="center"
        android:textSize="18sp"
        android:padding="5dp"
        android:layout_margin="5dp"
        android:id="@+id/companyNameXML"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#FFffffff"
        android:textSize="18sp"
        android:text="@string/buttontext"
        android:id="@+id/goButtonXML"
        android:textAllCaps="false"
        android:layout_margin="5dp"
        android:padding="5dp"/>
    <View
        style="@style/Divider"/>

</LinearLayout>
Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
Sakhawat Hossain
  • 454
  • 8
  • 23
  • Pls post your recyclerview_row_style_for_switch_company_layout.xml – Arti Patel Feb 22 '19 at 04:50
  • How do you download data from your server? Maybe that could be the problem, because RecyclerView code seems fine. – Natasa Andzic Feb 22 '19 at 04:48
  • what do you mean by faster loading . have you checked your response time in postman or something ? is it same as android ? cause it depends on multiple factor like connection speed , server speed , caching mechanism .. etc – Tejas Pandya Feb 22 '19 at 04:55
  • I am using retrofit to fetch the data from server. @Natasa Andzic – Sakhawat Hossain Feb 22 '19 at 05:02
  • The response time between postman and android is not same.Android is taking more time comparing to postman request time. – Sakhawat Hossain Feb 22 '19 at 05:03
  • Can u show the time different between postman and android? Please take note the time you received json object and the time you parsed that json to object successfully different. Do you mind to post your retrofit load data? – ToraCode Feb 22 '19 at 07:20

3 Answers3

3

Try using LiveData. these objects are thread-safe. Handle your data queries on a separate thread and then use the LiveData method "postValue" to transmit your values back to the observer. You can find very good examples from the official documentations on Android's website. They use the MVVM pattern.

zuko
  • 707
  • 5
  • 12
2

Perhaps you could try the Paging Library: https://developer.android.com/topic/libraries/architecture/paging

Your adapter is actually simple enough that it doesn't have any of the pitfalls that can impact loading speeds. This is especially true because you're simply setting text for one TextView and setting the function of a Button.

The simple TextView and Button should also mean you don't have a nested layout used for the View, which could impact performance when initially inflating the layout.

So if you're still experiencing slow load times, in my opinion, it should be related to:

  1. Server requiring optimizations

  2. Data network speeds

  3. Amount of data being loaded

For the 3rd reason, this is possible if you're simply loading too much data and having your app process too much data prior to showing it in the RecyclerView.

Therefore, the Paging Library is meant to help you manage and fetch the amount of data in smaller batches, because frequently, there's no reason to fetch 100 if you're only going to show only 10 at a time.

Jackey
  • 3,184
  • 1
  • 11
  • 12
  • Answer seems pretty obvious.I have used this paging library on another recyclerview.But for this problem i only have 8 rows to be fetched and set on the recyclerView. – Sakhawat Hossain Feb 22 '19 at 05:27
1

I assume that your recycler view takes time on data collection. It's batter to collect data on a separate thread and run asynchronously in the background and reconnects to the Activity when finished.

Specifically, run a query in the background and automatically re-runs it when data associated with the query changes.

When the async code is done, you should update the data, not the views. After updating the data, tell the adapter that the data changed. RecyclerView gets note of this and refresh your view.

Farid Haq
  • 3,728
  • 1
  • 21
  • 15
  • Ok this one sounds efficient to me.So you are suggesting me to create a thread and which will be running in the background to fetch the data.It would be more helpful if you can provide references. – Sakhawat Hossain Feb 22 '19 at 15:01