You are on page 1of 21

Membuat .

java

Membuat .xml
Memunculkan gambar

Build.Gradle (Module : app)


Membuat res di menu

Membuat ids.xml di values


Source Code :
MainActivity.Java

package com.example.emiliyana.myrecyclerviewmila;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


private RecyclerView rvCategory;
private ArrayList<President> list;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

rvCategory = (RecyclerView) findViewById(R.id.rv_category);


rvCategory.setHasFixedSize(true);

list = new ArrayList<>();


list.addAll(PresidentData.getListData());

setActionBarTitle("Mode List");
showRecyclerList();
}

private void showRecyclerList() {


rvCategory.setLayoutManager(new LinearLayoutManager(this));
ListPresidentAdapter listPresidentAdapter = new ListPresidentAdapter(this);
listPresidentAdapter.setListPresident(list);
rvCategory.setAdapter(listPresidentAdapter);

ItemClickSupport.addTo(rvCategory).setOnItemClickListener(new
ItemClickSupport.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
showSelectedPresident(list.get(position));
}
});
}

private void showRecyclerGrid() {


rvCategory.setLayoutManager(new GridLayoutManager(this, 2));
GridPresidentAdapter gridPresidentAdapter = new GridPresidentAdapter(this);
gridPresidentAdapter.setListPresident(list);
rvCategory.setAdapter(gridPresidentAdapter);

ItemClickSupport.addTo(rvCategory).setOnItemClickListener(new
ItemClickSupport.OnItemClickListener() {
@Override
public void onItemClicked(RecyclerView recyclerView, int position, View v) {
showSelectedPresident(list.get(position));
}
});
}

private void showRecyclerCardView() {


rvCategory.setLayoutManager(new LinearLayoutManager(this));
CardViewPresidentAdapter cardViewPresidentAdapter = new
CardViewPresidentAdapter(this);
cardViewPresidentAdapter.setListPresident(list);
rvCategory.setAdapter(cardViewPresidentAdapter);
}

private void setActionBarTitle(String title) {


getSupportActionBar().setTitle(title);
}

private void showSelectedPresident(President president) {


Toast.makeText(this, "Kamu memilih " + president.getName(),
Toast.LENGTH_SHORT).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

String title = null;


switch (item.getItemId()) {
case R.id.action_list:
title = "Mode List";
showRecyclerList();
break;
case R.id.action_grid:
showRecyclerGrid();
title = "Mode Grid";
break;

case R.id.action_cardview:
title = "Mode CardView";
showRecyclerCardView();
break;
}
setActionBarTitle(title);
return super.onOptionsItemSelected(item);
}
}

CardViewPresidentAdapter.Java

package com.example.emiliyana.myrecyclerviewmila;

/**
* Created by mila on 24/05/2018.
*/

import android.content.Context;
import android.support.v7.widget.RecyclerView;
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 com.bumptech.glide.Glide;

import java.util.ArrayList;

public class CardViewPresidentAdapter extends


RecyclerView.Adapter<CardViewPresidentAdapter.CardViewViewHolder>{
private ArrayList<President> listPresident;
private Context context;

public CardViewPresidentAdapter(Context context) {


this.context = context;
}
public ArrayList<President> getListPresident() {
return listPresident;
}

public void setListPresident(ArrayList<President> listPresident) {


this.listPresident = listPresident;
}
@Override
public CardViewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_cardview_president, parent,
false);
CardViewViewHolder viewHolder = new CardViewViewHolder(view);
return viewHolder;
}

@Override
public void onBindViewHolder(CardViewViewHolder holder, int position) {

President p = getListPresident().get(position);

Glide.with(context)
.load(p.getPhoto())
.override(350, 550)
.into(holder.imgPhoto);

holder.tvName.setText(p.getName());
holder.tvRemarks.setText(p.getRemarks());

holder.btnFavorite.setOnClickListener(new CustomOnItemClickListener(position, new


CustomOnItemClickListener.OnItemClickCallback() {

@Override
public void onItemClicked(View view, int position) {
Toast.makeText(context, "Favorite "+getListPresident().get(position).getName(),
Toast.LENGTH_SHORT).show();
}
}));

holder.btnShare.setOnClickListener(new CustomOnItemClickListener(position, new


CustomOnItemClickListener.OnItemClickCallback() {

@Override
public void onItemClicked(View view, int position) {
Toast.makeText(context, "Share " + getListPresident().get(position).getName(),
Toast.LENGTH_SHORT).show();
}
}));
}
@Override
public int getItemCount() {
return getListPresident().size();
}

public class CardViewViewHolder extends RecyclerView.ViewHolder{


ImageView imgPhoto;
TextView tvName, tvRemarks;
Button btnFavorite, btnShare;
public CardViewViewHolder(View itemView) {
super(itemView);
imgPhoto = (ImageView)itemView.findViewById(R.id.img_item_photo);
tvName = (TextView)itemView.findViewById(R.id.tv_item_name);
tvRemarks = (TextView)itemView.findViewById(R.id.tv_item_remarks);
btnFavorite = (Button)itemView.findViewById(R.id.btn_set_favorite);
btnShare = (Button)itemView.findViewById(R.id.btn_set_share);
}
}
}

CustomOnItemClickListener.Java

package com.example.emiliyana.myrecyclerviewmila;

/**
* Created by mila on 24/05/2018.
*/

import android.view.View;

public class CustomOnItemClickListener implements View.OnClickListener {


private int position;
private OnItemClickCallback onItemClickCallback;
public CustomOnItemClickListener(int position, OnItemClickCallback
onItemClickCallback) {
this.position = position;
this.onItemClickCallback = onItemClickCallback;
}

@Override
public void onClick(View view) {
onItemClickCallback.onItemClicked(view, position);
}
public interface OnItemClickCallback {
void onItemClicked(View view, int position);
}
}
GridPresidentAdapter.Java

package com.example.emiliyana.myrecyclerviewmila;

/**
* Created by mila on 24/05/2018.
*/

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class GridPresidentAdapter extends


RecyclerView.Adapter<GridPresidentAdapter.GridViewHolder> {
private Context context;
private ArrayList<President> listPresident;

public ArrayList<President> getListPresident() {


return listPresident;
}

public void setListPresident(ArrayList<President> listPresident) {


this.listPresident = listPresident;
}

public GridPresidentAdapter(Context context) {


this.context = context;
}

@Override
public GridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_grid_president, parent, false);
GridViewHolder gridViewHolder = new GridViewHolder(view);
return gridViewHolder;
}

@Override
public void onBindViewHolder(GridViewHolder holder, int position) {
Glide.with(context)
.load(getListPresident().get(position).getPhoto())
.override(350, 550)
.into(holder.imgPhoto);
}

@Override
public int getItemCount() {
return getListPresident().size();
}
public class GridViewHolder extends RecyclerView.ViewHolder{
ImageView imgPhoto;
public GridViewHolder(View itemView) {
super(itemView);
imgPhoto = (ImageView)itemView.findViewById(R.id.img_item_photo);
}
}
}

ItemClickSupport.Java

package com.example.emiliyana.myrecyclerviewmila;

/**
* Created by mila on 24/05/2018.
*/

import android.support.v7.widget.RecyclerView;
import android.view.View;

public class ItemClickSupport {


private final RecyclerView mRecyclerView;
private OnItemClickListener mOnItemClickListener;
private OnItemLongClickListener mOnItemLongClickListener;

private ItemClickSupport(RecyclerView recyclerView) {


mRecyclerView = recyclerView;
mRecyclerView.setTag(R.id.item_click_support, this);
mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener);
}

private View.OnClickListener mOnClickListener = new View.OnClickListener() {

@Override
public void onClick(View v) {
if (mOnItemClickListener != null) {
RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
mOnItemClickListener.onItemClicked(mRecyclerView,
holder.getAdapterPosition(), v);
}
}
};
private View.OnLongClickListener mOnLongClickListener = new
View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
if (mOnItemLongClickListener != null) {
RecyclerView.ViewHolder holder = mRecyclerView.getChildViewHolder(v);
return mOnItemLongClickListener.onItemLongClicked(mRecyclerView,
holder.getAdapterPosition(), v);
}
return false;
}
};
private RecyclerView.OnChildAttachStateChangeListener mAttachListener
= new RecyclerView.OnChildAttachStateChangeListener() {

@Override
public void onChildViewAttachedToWindow(View view) {
if (mOnItemClickListener != null) {
view.setOnClickListener(mOnClickListener);
}
if (mOnItemLongClickListener != null) {
view.setOnLongClickListener(mOnLongClickListener);
}
}

@Override
public void onChildViewDetachedFromWindow(View view) {
}
};
public static ItemClickSupport addTo(RecyclerView view) {
ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
if (support == null) {
support = new ItemClickSupport(view);
}
return support;
}
public static ItemClickSupport removeFrom(RecyclerView view) {
ItemClickSupport support = (ItemClickSupport) view.getTag(R.id.item_click_support);
if (support != null) {
support.detach(view);
}
return support;
}
public ItemClickSupport setOnItemClickListener(OnItemClickListener listener) {
mOnItemClickListener = listener;
return this;
}
public ItemClickSupport setOnItemLongClickListener(OnItemLongClickListener listener)
{
mOnItemLongClickListener = listener;
return this;
}
private void detach(RecyclerView view) {
view.removeOnChildAttachStateChangeListener(mAttachListener);
view.setTag(R.id.item_click_support, null);
}
public interface OnItemClickListener {
void onItemClicked(RecyclerView recyclerView, int position, View v);
}
public interface OnItemLongClickListener {
boolean onItemLongClicked(RecyclerView recyclerView, int position, View v);
}
}

ListPresidentAdapter.Java

package com.example.emiliyana.myrecyclerviewmila;

/**
* Created by mila on 24/05/2018.
*/
import android.content.Context;
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 java.util.ArrayList;

public class ListPresidentAdapter extends


RecyclerView.Adapter<ListPresidentAdapter.CategoryViewHolder>{
private Context context;

public ArrayList<President> getListPresident() {


return listPresident;
}
public void setListPresident(ArrayList<President> listPresident) {
this.listPresident = listPresident;
}

private ArrayList<President>listPresident;

public ListPresidentAdapter(Context context) {


this.context = context;
}

@Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemRow =
LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row_president, parent, false);
return new CategoryViewHolder(itemRow);
}

@Override
public void onBindViewHolder(CategoryViewHolder holder, int position) {

holder.tvName.setText(getListPresident().get(position).getName());
holder.tvRemarks.setText(getListPresident().get(position).getRemarks());

Glide.with(context)
.load(getListPresident().get(position).getPhoto())
.override(55, 55)
.crossFade()
.into(holder.imgPhoto);
}

@Override
public int getItemCount() {
return getListPresident().size();
}

class CategoryViewHolder extends RecyclerView.ViewHolder{


TextView tvName;
TextView tvRemarks;
ImageView imgPhoto;

public CategoryViewHolder(View itemView) {


super(itemView);
tvName = (TextView)itemView.findViewById(R.id.tv_item_name);
tvRemarks = (TextView)itemView.findViewById(R.id.tv_item_remarks);
imgPhoto = (ImageView)itemView.findViewById(R.id.img_item_photo);
}
}
}
President.Java

package com.example.emiliyana.myrecyclerviewmila;

/**
* Created by mila on 24/05/2018.
*/
public class President {
private String name, remarks, photo;

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public String getRemarks() {


return remarks;
}

public void setRemarks(String remarks) {


this.remarks = remarks;
}

public String getPhoto() {


return photo;
}

public void setPhoto(String photo) {


this.photo = photo;
}
}

PresidentData.Java

package com.example.emiliyana.myrecyclerviewmila;

/**
* Created by mila on 24/05/2018.
*/
import java.util.ArrayList;

public class PresidentData {


public static String[][] data = new String[][]{
{"Soekarno", "Presiden Pertama RI",
"https://upload.wikimedia.org/wikipedia/commons/thumb/0/01/Presiden_Sukarno.jpg/4
18px-Presiden_Sukarno.jpg"},
{"Soeharto", "Presiden Kedua RI",
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/President_Suharto%2C
_1993.jpg/468px-President_Suharto%2C_1993.jpg"},
{"Bacharuddin Jusuf Habibie", "Presiden Ketiga RI",
"https://upload.wikimedia.org/wikipedia/commons/thumb/f/f1/Bacharuddin_Jusuf_Ha
bibie_official_portrait.jpg/520px-Bacharuddin_Jusuf_Habibie_official_portrait.jpg"},
{"Abdurrahman Wahid", "Presiden Keempat RI",
"https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/President_Abdurrahma
n_Wahid_-_Indonesia.jpg/486px-President_Abdurrahman_Wahid_-_Indonesia.jpg"},
{"Megawati Soekarnoputri", "Presiden Kelima RI",
"https://upload.wikimedia.org/wikipedia/commons/thumb/8/88/President_Megawati_Su
karnoputri_-_Indonesia.jpg/540px-President_Megawati_Sukarnoputri_-
_Indonesia.jpg"},
{"Susilo Bambang Yudhoyono", "Presiden Keenam RI",
"https://upload.wikimedia.org/wikipedia/commons/5/58/Presiden_Susilo_Bambang_Yu
dhoyono.png"},
{"Joko Widodo", "Presiden Ketujuh RI",
"https://upload.wikimedia.org/wikipedia/commons/1/1c/Joko_Widodo_2014_official_po
rtrait.jpg"}
};

public static ArrayList<President> getListData(){


President president = null;
ArrayList<President> list = new ArrayList<>();
for (int i = 0; i <data.length; i++) {
president = new President();
president.setName(data[i][0]);
president.setRemarks(data[i][1]);
president.setPhoto(data[i][2]);

list.add(president);
}

return list;
}
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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="com.example.emiliyana.myrecyclerviewmila.MainActivity">

<android.support.v7.widget.RecyclerView
android:id="@+id/rv_category"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>

Item_cardview_president.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginBottom="4dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="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" />

<TextView
android:id="@+id/tv_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_toRightOf="@id/img_item_photo"
android:text="Name"
android:textSize="16sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_item_remarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_item_name"
android:layout_marginBottom="8dp"
android:layout_marginLeft="@dimen/activity_vertical_margin"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:layout_toRightOf="@id/img_item_photo"
android:text="Remarks" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
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="Favorite"
android:textSize="11sp" />

<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="Share"
android:textSize="11sp" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>

Item_grid_president.xml

<?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:orientation="vertical">

<ImageView
android:id="@+id/img_item_photo"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_margin="1dp"
android:scaleType="centerCrop" />
</LinearLayout>

Item_row_president.xml

<?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:orientation="horizontal"
android:padding="@dimen/activity_vertical_margin">

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/img_item_photo"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_marginRight="@dimen/activity_horizontal_margin" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/img_item_photo"
android:orientation="vertical">

<TextView
android:id="@+id/tv_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Name"
android:textSize="16sp"
android:textStyle="bold" />

<TextView
android:id="@+id/tv_item_remarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Remarks" />
</LinearLayout>
</RelativeLayout>

Colors.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>

Dimens.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="activity_horizontal_margin">16dp</dimen>
</resources>

Strings.xml

<resources>
<string name="app_name">MyRecyclerViewMila</string>
</resources>

Styles.xml

<resources>

<!-- Base application theme. -->


<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

Ids.xml

<?xml version="1.0" encoding="utf-8"?>


<resources>
<item name="item_click_support" type="id" />
</resources>
Build.Gradle (Module: app)

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.emiliyana.myrecyclerviewmila"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-
rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'de.hdodenhof:circleimageview:2.1.0'
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:cardview-v7:25.0.0'
}

Menu_main.xml

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_list"
android:title="List"
app:showAsAction="never" />
<item
android:id="@+id/action_grid"
android:title="Grid"
app:showAsAction="never" />
<item
android:id="@+id/action_cardview"
android:title="with CardView"
app:showAsAction="never" />

</menu>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.emiliyana.myrecyclerviewmila">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

You might also like