Вы находитесь на странице: 1из 6

RecyclerViewAdapter.

java
package com.megahandayani.recyclerview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

/**
* Created by Mega on 4/10/2018.
*/

public class RecyclerViewAdapter extends


RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private static final String TAG = "RecyclerViewAdapter";

private ArrayList<String> mImageNames = new ArrayList<>();


private ArrayList<String> mImages = new ArrayList<>();
private Context mContext;

public RecyclerViewAdapter(Context context, ArrayList<String>


imageNames, ArrayList<String> images) {
mImageNames = imageNames;
mImages = images;
mContext = context;
}

@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem
, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int
position) {
Log.d(TAG, "onBindViewHolder: called.");
Glide.with(mContext)
.asBitmap()
.load(mImages.get(position))
.into(holder.image);

holder.imageName.setText(mImageNames.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
Log.d(TAG, "onClick: clicked on:" +
mImageNames.get(position));

Toast.makeText(mContext, mImageNames.get(position),
Toast.LENGTH_SHORT).show();
}
});

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

public class ViewHolder extends RecyclerView.ViewHolder{

CircleImageView image;
TextView imageName;
RelativeLayout parentLayout;

public ViewHolder (View itemView){


super(itemView);
image = itemView.findViewById(R.id.image);
imageName = itemView.findViewById(R.id.image_name);
parentLayout = itemView.findViewById(R.id.parentLayout);
}
}
}

MainActivity.java
package com.megahandayani.recyclerview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";

//vare
private ArrayList<String> mNames = new ArrayList<>();
private ArrayList<String> mImageUrls = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: started.");
initImageBitmaps();
}

private void initImageBitmaps (){


Log.d(TAG, "intImageBitmaps: preparing bitmaps.");

mImageUrls.add("https://usercontent1.hubstatic.com/13443470_f520.jpg");
mNames.add("Kim Taehyung");

mImageUrls.add("https://cdn.idntimes.com/content-
images/community/2018/01/v-fansign-
b65c1217a2c644157793cd5e4cd6eb42_420x280.jpg");
mNames.add("V");

mImageUrls.add("https://encrypted-
tbn0.gstatic.com/images?q=tbn:ANd9GcSrwLpDFleJvVBSeMt11tqcfl1jkXi0dOUulKi
9XFhUIsg3P3Ft");
mNames.add("Cute V");

mImageUrls.add("https://encrypted-
tbn0.gstatic.com/images?q=tbn:ANd9GcSrwLpDFleJvVBSeMt11tqcfl1jkXi0dOUulKi
9XFhUIsg3P3Ft");
mNames.add("Boy");

mImageUrls.add("https://encrypted-
tbn0.gstatic.com/images?q=tbn:ANd9GcSrwLpDFleJvVBSeMt11tqcfl1jkXi0dOUulKi
9XFhUIsg3P3Ft");
mNames.add("Baby");

mImageUrls.add("https://encrypted-
tbn0.gstatic.com/images?q=tbn:ANd9GcSrwLpDFleJvVBSeMt11tqcfl1jkXi0dOUulKi
9XFhUIsg3P3Ft");
mNames.add("Big Boy");

mImageUrls.add("https://encrypted-
tbn0.gstatic.com/images?q=tbn:ANd9GcSrwLpDFleJvVBSeMt11tqcfl1jkXi0dOUulKi
9XFhUIsg3P3Ft");
mNames.add("BTS ");

initRecyclerView();
}

private void initRecyclerView(){


Log.d(TAG, "initRecyclerView: init recyclerview.");
RecyclerView recyclerView = findViewById(R.id.recyclerv_view);
RecyclerViewAdapter adapter = new RecyclerViewAdapter(this,
mNames, mImageUrls);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}

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.megahandayani.recyclerview.MainActivity">

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

</RelativeLayout>

layout_listitem.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:padding="15dp"
android:id="@+id/parentLayout">

<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/image"
android:src="@mipmap/ic_launcher"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BTS"
android:id="@+id/image_name"
android:layout_toRightOf="@id/image"
android:textColor="#000"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:textSize="17sp"/>

</RelativeLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.megahandayani.recyclerview">

<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>

Build.gradle(Module:app)
apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
applicationId "com.megahandayani.recyclerview"
minSdkVersion 20
targetSdkVersion 27
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:27.0.2'
implementation 'com.android.support.constraint:constraint-
layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.1'

//recyclerview
compile 'com.android.support:recyclerview-v7:27.0.2'

//glide
implementation 'com.github.bumptech.glide:glide:4.4.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'

//circle imageview
compile 'de.hdodenhof:circleimageview:2.2.0'
}

Вам также может понравиться