Recycler View dengan Metode Grid
hello temen2,
yuk lanjut belajar, pelajaran ini adalah pelajaran dari materi sebelumnya,
yaitu "Recycler View dengan Metode List"
1. langkah pertama, membuat layout baru bernama "item_grid_hero"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img_item_photo"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="1dp"
android:scaleType="centerCrop"
tools:scr="@color/colorAccent"/>
</LinearLayout>
2. langkah kedua, buat adapter dengan membuat class dengan nama "GridHeroAdapter"
package com.example.myrecyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import java.util.ArrayList;
public class GridHeroAdapter extends RecyclerView.Adapter<GridHeroAdapter.GridViewHolder> {
private ArrayList<Hero> listHero;
public GridHeroAdapter(ArrayList<Hero> list) {
this.listHero = list;
}
@NonNull
@Override
public GridViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_grid_hero, viewGroup, false);
return new GridViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final GridViewHolder holder, int position) {
Glide.with(holder.itemView.getContext())
.load(listHero.get(position).getPhoto())
.apply(new RequestOptions().override(350, 550))
.into(holder.imgPhoto);
}
@Override
public int getItemCount() {
return listHero.size();
}
class GridViewHolder extends RecyclerView.ViewHolder {
ImageView imgPhoto;
GridViewHolder(View itemView) {
super(itemView);
imgPhoto = itemView.findViewById(R.id.img_item_photo);
}
}
}
3. langkah ketiga, kita modifikasi pada class main activity kita
package com.example.myrecyclerview;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView rvHeroes;
private ArrayList<Hero> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rvHeroes = findViewById(R.id.rv_heroes);
rvHeroes.setHasFixedSize(true);
list.addAll(HeroesData.getListData());
showRecyclerList();
}
private void showRecyclerGrid(){
rvHeroes.setLayoutManager(new GridLayoutManager(this,2));
GridHeroAdapter gridHeroAdapter = new GridHeroAdapter(list);
rvHeroes.setAdapter(gridHeroAdapter);
}
private void showRecyclerList(){
rvHeroes.setLayoutManager(new LinearLayoutManager(this));
ListHeroAdapter listHeroAdapter = new ListHeroAdapter(list);
rvHeroes.setAdapter(listHeroAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
setMode(item.getItemId());
return super.onOptionsItemSelected(item);
}
public void setMode(int selectedMode){
switch (selectedMode){
case R.id.action_list:
showRecyclerList();
break;
case R.id.action_grid:
showRecyclerGrid();
break;
case R.id.action_cardview:
break;
}
}
}
bila sudah coba di run, lalu hasilnya akan seperti dibawah ini
bila tombol grid ditekan akan timbul tampilan grid seperti disamping....
Komentar
Posting Komentar