Recycler View dengan Metode CardView
hello temen2,
yuk kita selesaikan projek kita,
sekarang kita akan menggunakan Recycler View dengan metode CardView
ini merupakan lanjutan dari materi "Recycler View dengan metode Grid"
1. langkah pertama,buat layout baru dengan nama "item_cardview_hero"
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginTop="4dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="4dp"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:padding="8dp">
<ImageView
android:id="@+id/img_item_photo"
android:layout_width="150dp"
android:layout_height="220dp"
android:layout_marginBottom="4dp"
android:scaleType="centerCrop"
tools:src="@color/colorAccent"/>
<TextView
android:id="@+id/tv_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:layout_toEndOf="@+id/img_item_photo"
android:layout_toRightOf="@+id/img_item_photo"
android:textSize="16sp"
android:textStyle="bold"
tools:text="@string/heroes_name"/>
<TextView
android:id="@+id/tv_item_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/layout_button"
android:layout_below="@+id/tv_item"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_toEndOf="@+id/img_item_photo"
android:layout_toRightOf="@+id/img_item_photo"
android:ellipsize="end"
android:maxLines="5"
tools:text="@string/detail"/>
<LinearLayout
android:id="@+id/layout_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_toEndOf="@id/img_item_photo"
android:layout_toRightOf="@id/img_item_photo"
android:orientation="horizontal">
<Button
android:id="@+id/btn_set_favorite"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/favorite"
android:textSize="12sp"/>
<Button
android:id="@+id/btn_set_share"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/share"
android:textSize="12sp"/>
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
2. langkah kedua, buat kelas adapter dengan nama "CardViewHeroAdapter" di class java
package com.example.myrecyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
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 CardViewHeroAdapter extends RecyclerView.Adapter<CardViewHeroAdapter.CardViewViewHolder> {
private ArrayList<Hero> listHero;
public CardViewHeroAdapter(ArrayList<Hero> list){
this.listHero = list;
}
@NonNull
@Override
public CardViewViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_cardview_hero, viewGroup, false);
return new CardViewViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final CardViewViewHolder holder, int position) {
Hero hero = listHero.get(position);
Glide.with(holder.itemView.getContext())
.load(hero.getPhoto())
.apply(new RequestOptions().override(350, 550))
.into(holder.imgPhoto);
holder.tvName.setText(hero.getName());
holder.tvDetail.setText(hero.getDetail());
holder.btnFavorite.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Toast.makeText(holder.itemView.getContext(), "Favorite" +
listHero.get(holder.getAdapterPosition()).getName(), Toast.LENGTH_SHORT).show();
}
});
holder.btnShare.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(holder.itemView.getContext(), "Share" +
listHero.get(holder.getAdapterPosition()).getName(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return listHero.size();
}
public class CardViewViewHolder extends RecyclerView.ViewHolder {
ImageView imgPhoto;
TextView tvName, tvDetail;
Button btnFavorite, btnShare;
public CardViewViewHolder(@NonNull View itemView){
super(itemView);
imgPhoto = itemView.findViewById(R.id.img_item_photo);
tvName = itemView.findViewById(R.id.tv_item);
tvDetail = itemView.findViewById(R.id.tv_item_detail);
btnFavorite = itemView.findViewById(R.id.btn_set_favorite);
btnShare = itemView.findViewById(R.id.btn_set_share);
}
}
}
3. langkah ketiga, melengkapi kode di main activity class
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<>();
private void setActionBarTitle(String title){
if (getSupportActionBar() !=null){
getSupportActionBar().setTitle(title);
}
}
private String title = "Mode List";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setActionBarTitle(title);
rvHeroes = findViewById(R.id.rv_heroes);
rvHeroes.setHasFixedSize(true);
list.addAll(HeroesData.getListData());
showRecyclerList();
}
private void showRecyclerCardView(){
rvHeroes.setLayoutManager(new LinearLayoutManager(this));
CardViewHeroAdapter cardViewHeroAdapter = new CardViewHeroAdapter(list);
rvHeroes.setAdapter(cardViewHeroAdapter);
}
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:
showRecyclerCardView();
break;
}
setActionBarTitle(title);
}
}
dan bila sudah di run, maka akan tampil tampilan berikut...
tekan titik tiga..
akan tampil tiga pilihan..
ada mode grid...
ada mode cardview juga..
selamat mencoba...
Komentar
Posting Komentar