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>
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.