Skip to content

Instantly share code, notes, and snippets.

@satadii11
Last active March 15, 2019 09:34
Show Gist options
  • Save satadii11/68fb76f3e72188cabf694eb4922f2f72 to your computer and use it in GitHub Desktop.
Save satadii11/68fb76f3e72188cabf694eb4922f2f72 to your computer and use it in GitHub Desktop.
smkcodingday4materi
Langkah pembuatan :
1. build.gradle
2. activity_main.xml
3. item_row.xml
4. item_header.xml
5. activity_card_detail.xml
6. CardModel.java
7. MainActivity.java
8. CardAdapter.java
9. CardDetailActivity.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:id="@+id/carddetail_imageview_poster"
android:layout_width="match_parent"
android:layout_height="196dp"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/carddetail_textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<TextView
android:id="@+id/carddetail_textview_rarity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="@+id/carddetail_textview_series"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/main_swiperefresh_cards"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/main_recyclerview_cards"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.coba.recyclerviewlanjutan">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".card.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".carddetail.CardDetailActivity" />
</application>
</manifest>
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.coba.recyclerviewlanjutan"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.squareup.okhttp3:okhttp:3.12.0'
implementation 'com.google.code.gson:gson:2.8.5'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
package com.coba.recyclerviewlanjutan.card;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.coba.recyclerviewlanjutan.R;
import com.coba.recyclerviewlanjutan.carddetail.CardDetailActivity;
import com.coba.recyclerviewlanjutan.model.CardModel;
import java.util.List;
public class CardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int CARD_HEADER = 0;
private final int CARD_ITEM = 1;
Context context;
List<CardModel> cards;
public CardAdapter(Context context, List<CardModel> cards) {
this.context = context;
this.cards = cards;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == CARD_HEADER) {
View view = LayoutInflater.from(context).inflate(R.layout.item_header, null);
return new CardHeaderViewHolder(view);
} else {
View view = LayoutInflater.from(context).inflate(R.layout.item_row, null);
return new CardViewHolder(view);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
final int itemType = getItemViewType(position);
CardModel card = cards.get(position);
if (itemType == CARD_HEADER) {
Glide.with(context).load(card.getImageUrl()).into(((CardHeaderViewHolder) holder).imageViewPoster);
((CardHeaderViewHolder) holder).textViewTitle.setText(card.getName());
} else {
Glide.with(context).load(card.getImageUrl()).into(((CardViewHolder) holder).imageViewPoster);
((CardViewHolder) holder).textViewTitle.setText(card.getName());
((CardViewHolder) holder).textViewRarity.setText(card.getRarity());
}
}
@Override
public int getItemViewType(int position) {
if (position == 0) {
return CARD_HEADER;
} else {
return CARD_ITEM;
}
}
@Override
public int getItemCount() {
return cards.size();
}
class CardViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imageViewPoster;
TextView textViewTitle;
TextView textViewRarity;
CardViewHolder(View itemView) {
super(itemView);
imageViewPoster = itemView.findViewById(R.id.card_imageview_poster);
textViewTitle = itemView.findViewById(R.id.card_textview_title);
textViewRarity = itemView.findViewById(R.id.card_textview_rarity);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent(context, CardDetailActivity.class);
intent.putExtra("name", cards.get(getAdapterPosition()).getName());
intent.putExtra("series", cards.get(getAdapterPosition()).getSeries());
intent.putExtra("rarity", cards.get(getAdapterPosition()).getRarity());
intent.putExtra("poster", cards.get(getAdapterPosition()).getImageUrl());
context.startActivity(intent);
}
}
class CardHeaderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imageViewPoster;
TextView textViewTitle;
public CardHeaderViewHolder(View itemView) {
super(itemView);
imageViewPoster = itemView.findViewById(R.id.card_header_imageview_poster);
textViewTitle = itemView.findViewById(R.id.card_header_textview_title);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent(context, CardDetailActivity.class);
intent.putExtra("name", cards.get(getAdapterPosition()).getName());
intent.putExtra("series", cards.get(getAdapterPosition()).getSeries());
intent.putExtra("rarity", cards.get(getAdapterPosition()).getRarity());
intent.putExtra("poster", cards.get(getAdapterPosition()).getImageUrl());
context.startActivity(intent);
}
}
}
package com.coba.recyclerviewlanjutan.carddetail;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.coba.recyclerviewlanjutan.R;
public class CardDetailActivity extends AppCompatActivity {
ImageView imageViewPoster;
TextView textViewName;
TextView textViewSeries;
TextView textViewRarity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_detail);
imageViewPoster = findViewById(R.id.carddetail_imageview_poster);
textViewName = findViewById(R.id.carddetail_textview_title);
textViewSeries = findViewById(R.id.carddetail_textview_series);
textViewRarity = findViewById(R.id.carddetail_textview_rarity);
try {
Intent intent = getIntent();
Glide.with(this).load(intent.getStringExtra("poster")).into(imageViewPoster);
textViewName.setText(intent.getStringExtra("name"));
textViewSeries.setText(intent.getStringExtra("series"));
textViewRarity.setText(intent.getStringExtra("rarity"));
} catch (Exception e) {
Log.d("Error", "Error Message : " + e.getMessage());
}
}
}
package com.coba.recyclerviewlanjutan.model;
import java.util.List;
public class CardModel {
private String id;
private String name;
private String imageUrl;
private String rarity;
private String series;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
public String getRarity() {
return rarity;
}
public void setRarity(String rarity) {
this.rarity = rarity;
}
public String getSeries() {
return series;
}
public void setSeries(String series) {
this.series = series;
}
public class CardResultModel {
private List<CardModel> cards;
public List<CardModel> getCards() {
return cards;
}
public void setCards(List<CardModel> cards) {
this.cards = cards;
}
}
}
<?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="match_parent"
android:orientation="vertical"
android:padding="8dp">
<ImageView
android:id="@+id/card_header_imageview_poster"
android:layout_width="match_parent"
android:layout_height="156dp"
android:scaleType="centerCrop" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="8dp"
android:text="The First Card"
android:textSize="16dp"
android:textStyle="bold" />
<TextView
android:id="@+id/card_header_textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/card_imageview_poster"
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/card_textview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@id/card_imageview_poster"
android:ellipsize="end"
android:maxLines="2"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/card_textview_rarity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/card_textview_title"
android:layout_marginLeft="8dp"
android:layout_toRightOf="@id/card_imageview_poster"
android:textSize="14dp" />
</RelativeLayout>
package com.coba.recyclerviewlanjutan.card;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.Toast;
import com.coba.recyclerviewlanjutan.R;
import com.coba.recyclerviewlanjutan.model.CardModel;
import com.google.gson.Gson;
import java.io.IOException;
import java.util.ArrayList;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
OkHttpClient client;
SwipeRefreshLayout swipeRefreshCards;
RecyclerView recyclerViewCards;
LinearLayoutManager linearLayoutManager;
CardAdapter cardAdapter;
ArrayList<CardModel> cards = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
client = new OkHttpClient();
setupSwipeRefresh();
setupRecyclerView();
populateCardData();
}
private void populateCardData() {
swipeRefreshCards.setRefreshing(true);
cards.clear();
Request request = new Request.Builder()
.url("https://api.pokemontcg.io/v1/cards")
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
swipeRefreshCards.setRefreshing(false);
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()) {
Gson gson = new Gson();
final CardModel.CardResultModel result = gson.fromJson(response.body().string(), CardModel.CardResultModel.class);
runOnUiThread(new Runnable() {
@Override
public void run() {
swipeRefreshCards.setRefreshing(false);
cards.addAll(result.getCards());
cardAdapter.notifyDataSetChanged();
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
swipeRefreshCards.setRefreshing(false);
Toast.makeText(MainActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
});
}
}
});
}
private void setupSwipeRefresh() {
swipeRefreshCards = findViewById(R.id.main_swiperefresh_cards);
swipeRefreshCards.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
populateCardData();
}
});
}
private void setupRecyclerView() {
linearLayoutManager = new LinearLayoutManager(this) {
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
};
recyclerViewCards = findViewById(R.id.main_recyclerview_cards);
cardAdapter = new CardAdapter(this, cards);
recyclerViewCards.setAdapter(cardAdapter);
recyclerViewCards.setLayoutManager(linearLayoutManager);
recyclerViewCards.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
recyclerViewCards.setItemAnimator(new DefaultItemAnimator());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment