Skip to content

Commit ef0ccff

Browse files
added cardview android example
1 parent 41aab56 commit ef0ccff

61 files changed

Lines changed: 1171 additions & 87 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CardviewExample/.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild

CardviewExample/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

CardviewExample/app/build.gradle

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 29
5+
buildToolsVersion "29.0.3"
6+
defaultConfig {
7+
applicationId "com.example.velmurugan.cardviewexample"
8+
minSdkVersion 24
9+
targetSdkVersion 29
10+
versionCode 1
11+
versionName "1.0"
12+
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
implementation fileTree(dir: "libs", include: ["*.jar"])
24+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
25+
implementation 'androidx.core:core-ktx:1.3.0'
26+
implementation 'androidx.appcompat:appcompat:1.1.0'
27+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
28+
testImplementation 'junit:junit:4.12'
29+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
30+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
31+
32+
implementation "androidx.cardview:cardview:1.0.0"
33+
implementation 'androidx.recyclerview:recyclerview:1.1.0'
34+
35+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.velmurugan.cardviewexample;
2+
3+
import android.content.Context;
4+
import android.support.test.InstrumentationRegistry;
5+
import android.support.test.runner.AndroidJUnit4;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
import static org.junit.Assert.*;
11+
12+
/**
13+
* Instrumented test, which will execute on an Android device.
14+
*
15+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
16+
*/
17+
@RunWith(AndroidJUnit4.class)
18+
public class ExampleInstrumentedTest {
19+
@Test
20+
public void useAppContext() throws Exception {
21+
// Context of the app under test.
22+
Context appContext = InstrumentationRegistry.getTargetContext();
23+
24+
assertEquals("com.example.velmurugan.cardviewexample", appContext.getPackageName());
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.velmurugan.cardviewexample">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.example.velmurugan.cardviewexample;
2+
3+
import android.os.Bundle;
4+
import android.widget.Toast;
5+
6+
import androidx.appcompat.app.AppCompatActivity;
7+
import androidx.recyclerview.widget.LinearLayoutManager;
8+
import androidx.recyclerview.widget.RecyclerView;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
public class MainActivity extends AppCompatActivity {
14+
15+
private RecyclerView recyclerView;
16+
private RecyclerViewAdapter recyclerViewAdapter;
17+
private List<Movie> movieList;
18+
@Override
19+
protected void onCreate(Bundle savedInstanceState) {
20+
super.onCreate(savedInstanceState);
21+
setContentView(R.layout.activity_main);
22+
23+
movieList = new ArrayList<>();
24+
prepareMovie();
25+
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
26+
recyclerViewAdapter = new RecyclerViewAdapter(movieList);
27+
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
28+
recyclerView.setLayoutManager(layoutManager);
29+
30+
31+
recyclerViewAdapter.setOnItemClickListener(new ClickListener<Movie>(){
32+
@Override
33+
public void onItemClick(Movie data) {
34+
Toast.makeText(MainActivity.this, data.getTitle(), Toast.LENGTH_SHORT).show();
35+
}
36+
});
37+
38+
recyclerView.setAdapter(recyclerViewAdapter);
39+
40+
}
41+
42+
43+
private void prepareMovie(){
44+
Movie movie = new Movie("Star Wars The Last Jedi",R.drawable.star_war);
45+
movieList.add(movie);
46+
movie = new Movie("Coco",R.drawable.coco);
47+
movieList.add(movie);
48+
movie = new Movie("Justice League ",R.drawable.justice_league);
49+
movieList.add(movie);
50+
movie = new Movie("Thor: Ragnarok",R.drawable.thor_ragnarok);
51+
movieList.add(movie);
52+
movie = new Movie("Star Wars The Last Jedi",R.drawable.star_war);
53+
movieList.add(movie);
54+
movie = new Movie("Coco",R.drawable.coco);
55+
movieList.add(movie);
56+
movie = new Movie("Justice League ",R.drawable.justice_league);
57+
movieList.add(movie);
58+
movie = new Movie("Thor: Ragnarok",R.drawable.thor_ragnarok);
59+
movieList.add(movie);
60+
movie = new Movie("Star Wars The Last Jedi",R.drawable.star_war);
61+
movieList.add(movie);
62+
movie = new Movie("Coco",R.drawable.coco);
63+
movieList.add(movie);
64+
movie = new Movie("Justice League ",R.drawable.justice_league);
65+
movieList.add(movie);
66+
movie = new Movie("Thor: Ragnarok",R.drawable.thor_ragnarok);
67+
movieList.add(movie);
68+
recyclerViewAdapter.notifyDataSetChanged();
69+
}
70+
71+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.velmurugan.cardviewexample;
2+
3+
public class Movie {
4+
5+
private String title;
6+
private int image;
7+
8+
public Movie(String title, int image) {
9+
this.title = title;
10+
this.image = image;
11+
}
12+
13+
public String getTitle() {
14+
return title;
15+
}
16+
17+
public void setTitle(String title) {
18+
this.title = title;
19+
}
20+
21+
public int getImage() {
22+
return image;
23+
}
24+
25+
public void setImage(int image) {
26+
this.image = image;
27+
}
28+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.example.velmurugan.cardviewexample;
2+
3+
import android.view.LayoutInflater;
4+
import android.view.View;
5+
import android.view.ViewGroup;
6+
import android.widget.ImageView;
7+
import android.widget.TextView;
8+
9+
import androidx.cardview.widget.CardView;
10+
import androidx.recyclerview.widget.RecyclerView;
11+
12+
import java.util.List;
13+
14+
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>{
15+
16+
private List<Movie> movieList;
17+
private ClickListener<Movie> clickListener;
18+
19+
RecyclerViewAdapter(List<Movie> movieList){
20+
this.movieList = movieList;
21+
}
22+
23+
@Override
24+
public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
25+
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_adapter_layout,parent,false);
26+
return new MyViewHolder(view);
27+
}
28+
29+
@Override
30+
public void onBindViewHolder(RecyclerViewAdapter.MyViewHolder holder, final int position) {
31+
32+
final Movie movie = movieList.get(position);
33+
34+
holder.title.setText(movie.getTitle());
35+
holder.image.setBackgroundResource(movie.getImage());
36+
holder.cardView.setOnClickListener(new View.OnClickListener() {
37+
@Override
38+
public void onClick(View v) {
39+
clickListener.onItemClick(movie);
40+
}
41+
});
42+
43+
44+
}
45+
46+
@Override
47+
public int getItemCount() {
48+
return movieList.size();
49+
}
50+
51+
public void setOnItemClickListener(ClickListener<Movie> movieClickListener) {
52+
this.clickListener = movieClickListener;
53+
}
54+
55+
public class MyViewHolder extends RecyclerView.ViewHolder {
56+
57+
private TextView title;
58+
private ImageView image;
59+
private CardView cardView;
60+
61+
public MyViewHolder(View itemView) {
62+
super(itemView);
63+
title = itemView.findViewById(R.id.title);
64+
image = itemView.findViewById(R.id.image);
65+
cardView = itemView.findViewById(R.id.carView);
66+
}
67+
}
68+
}
69+
70+
interface ClickListener<T> {
71+
void onItemClick(T data);
72+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportHeight="108"
6+
android:viewportWidth="108">
7+
<path
8+
android:fillType="evenOdd"
9+
android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z"
10+
android:strokeColor="#00000000"
11+
android:strokeWidth="1">
12+
<aapt:attr name="android:fillColor">
13+
<gradient
14+
android:endX="78.5885"
15+
android:endY="90.9159"
16+
android:startX="48.7653"
17+
android:startY="61.0927"
18+
android:type="linear">
19+
<item
20+
android:color="#44000000"
21+
android:offset="0.0" />
22+
<item
23+
android:color="#00000000"
24+
android:offset="1.0" />
25+
</gradient>
26+
</aapt:attr>
27+
</path>
28+
<path
29+
android:fillColor="#FFFFFF"
30+
android:fillType="nonZero"
31+
android:pathData="M66.94,46.02L66.94,46.02C72.44,50.07 76,56.61 76,64L32,64C32,56.61 35.56,50.11 40.98,46.06L36.18,41.19C35.45,40.45 35.45,39.3 36.18,38.56C36.91,37.81 38.05,37.81 38.78,38.56L44.25,44.05C47.18,42.57 50.48,41.71 54,41.71C57.48,41.71 60.78,42.57 63.68,44.05L69.11,38.56C69.84,37.81 70.98,37.81 71.71,38.56C72.44,39.3 72.44,40.45 71.71,41.19L66.94,46.02ZM62.94,56.92C64.08,56.92 65,56.01 65,54.88C65,53.76 64.08,52.85 62.94,52.85C61.8,52.85 60.88,53.76 60.88,54.88C60.88,56.01 61.8,56.92 62.94,56.92ZM45.06,56.92C46.2,56.92 47.13,56.01 47.13,54.88C47.13,53.76 46.2,52.85 45.06,52.85C43.92,52.85 43,53.76 43,54.88C43,56.01 43.92,56.92 45.06,56.92Z"
32+
android:strokeColor="#00000000"
33+
android:strokeWidth="1" />
34+
</vector>

0 commit comments

Comments
 (0)