Reputation:
I am building an app which shows a gridview of 2 columns, with the help of recyclerview
. My code is as follows:
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycle"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
custom_item_layout.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--
stroke with color and width for creating outer line
-->
<stroke
android:width="5dp"
android:color="#DBE2E9" />
</shape>
row_layout.xml
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content"
android:layout_height="180dp"
android:background="@drawable/custom_item_layout"
android:padding="5dp">
<!--
grid items for RecyclerView
-->
<ImageView
android:id="@+id/image"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
// ArrayList for all app genres
ArrayList appNames = new ArrayList<>(Arrays.asList( "Augmented reality",
"Beauty", "Books & Reference", "Comics", "Communication",
"Dating", "Entertainment", "Food & Drink",
"Games", "Kids", "Music & Audio", "Photography",
"Social", "Video Players & Editors"));
ArrayList appImages = new ArrayList<>(Arrays.asList(R.drawable.a_r, R.drawable.beauty, R.drawable.ic_books,
R.drawable.ic_smashing_book_icon_comic_book_icon_11553392583tcjc, R.drawable.comms, R.drawable.date,
R.drawable.entertainment, R.drawable.foods, R.drawable.games,
R.drawable.kids, R.drawable.music, R.drawable.camera,
R.drawable.social, R.drawable.video));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the reference of RecyclerView
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle);
// set a GridLayoutManager with default vertical orientation and 2 number of columns
GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),2);
recyclerView.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
// call the constructor of CustomAdapter to send the reference and data to Adapter
CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, appNames,appImages);
recyclerView.setAdapter(customAdapter); // set the Adapter to RecyclerView
}
}
CustomAdapter.java
public class CustomAdapter extends RecyclerView.Adapter {
ArrayList appNames;
ArrayList appImages;
Context context;
boolean allowed = false;
LottieAlertDialog.Builder alert;
public CustomAdapter(Context context, ArrayList appNames, ArrayList appImages) {
this.context = context;
this.appNames = appNames;
this.appImages = appImages;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// infalte the item Layout
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
// set the view's size, margins, paddings and layout parameters
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
MyViewHolder mholder=(MyViewHolder) holder;
mholder.image.setImageResource((Integer) appImages.get(position));
}
@Override
public int getItemCount() {
return appNames.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
this.image = (ImageView) itemView.findViewById(R.id.image);
}
}
}
All my vector assets are of size:
android:width="512dp"
android:height="512dp"
android:viewportWidth="512"
android:viewportHeight="512">
The problem is that , when I run the app it takes so long to load, and the device keeps on freezing. I have no clue as to why this is happening. How do I fix this?
Upvotes: 1
Views: 724
Reputation: 2782
Following @Faisal recommendation, here I show the steps:
1.) Add the following dependencies intro build.gradle module:
implementation 'com.github.bumptech.glide:glide:4.12.0'
// Glide v4 uses this new annotation processor -- see https://bumptech.github.io/glide/doc/generatedapi.html
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
2.) To load the picture:
Glide.with(yourimageview.getContext())
.load(Your string path or uri to the photo)
.into(yourimageview);
Too me it worked! Now it's really fast
Upvotes: 0
Reputation: 382
Use image processing libraries like Glide to load images into imageview . That might help
Upvotes: 1