Skip to content

Commit

Permalink
Add RecyclerView demo
Browse files Browse the repository at this point in the history
  • Loading branch information
balysv committed Apr 30, 2015
1 parent 00f34fa commit 06afef8
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 20 deletions.
3 changes: 2 additions & 1 deletion demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ android {

dependencies {
compile project(':library')
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:recyclerview-v7:22.0.0'
}
4 changes: 4 additions & 0 deletions demo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
android:name=".DemoListActivity"
android:label="@string/app_name">
</activity>
<activity
android:name=".DemoRecyclerActivity"
android:label="@string/app_name">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

@Override public boolean onLongClick(View v) {
if(v.getId()==R.id.ripple_layout_1){
if (v.getId() == R.id.ripple_layout_1) {
Toast.makeText(this, "Long click not consumed", Toast.LENGTH_SHORT).show();
return false;
}
Expand All @@ -50,7 +50,7 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_list, menu);
getMenuInflater().inflate(R.menu.menu_button, menu);
return true;
}

Expand All @@ -61,6 +61,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
startActivity(new Intent(this, DemoListActivity.class));
finish();
return true;
} else if (id == R.id.switch_recycler) {
startActivity(new Intent(this, DemoRecyclerActivity.class));
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_button, menu);
getMenuInflater().inflate(R.menu.menu_list, menu);
return true;
}

Expand All @@ -60,6 +60,10 @@ public boolean onOptionsItemSelected(MenuItem item) {
startActivity(new Intent(this, DemoActivity.class));
finish();
return true;
} else if (id == R.id.switch_recycler) {
startActivity(new Intent(this, DemoRecyclerActivity.class));
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.balysv.materialripple.demo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.balysv.materialripple.MaterialRippleLayout;

import java.util.UUID;

public class DemoRecyclerActivity extends ActionBarActivity {

private final static String[] data;

static {
data = new String[50];
for (int i = 0; i < data.length; i++) {
data[i] = UUID.randomUUID().toString();
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo_recycler);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyAdapter());
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.switch_button) {
startActivity(new Intent(this, DemoActivity.class));
finish();
return true;
} else if (id == R.id.switch_list) {
startActivity(new Intent(this, DemoListActivity.class));
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

private static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

@Override
public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
final LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
return new MyViewHolder(
MaterialRippleLayout.on(inflater.inflate(R.layout.demo_recycler_item, viewGroup, false))
.rippleOverlay(true)
.rippleAlpha(0.2f)
.rippleColor(0xFF585858)
.rippleHover(true)
.create()
);
}

@Override
public void onBindViewHolder(MyViewHolder viewHolder, int i) {
viewHolder.text.setText(data[i]);
}

@Override
public int getItemCount() {
return data.length;
}
}

private static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener,
View.OnLongClickListener {

TextView text;

public MyViewHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(android.R.id.text1);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}

@Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Rippled item: " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}

@Override
public boolean onLongClick(View v) {
if (getAdapterPosition() % 2 == 0) {
Toast.makeText(v.getContext(), "long item: " + getAdapterPosition() + " and not consumed",
Toast.LENGTH_SHORT)
.show();
return false;
}
Toast.makeText(v.getContext(), "long item: " + getAdapterPosition() + " and consumed", Toast.LENGTH_SHORT)
.show();
return true;
}
}
}
18 changes: 8 additions & 10 deletions demo/src/main/res/layout/demo_list_item.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>

<com.balysv.materialripple.MaterialRippleLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<com.balysv.materialripple.MaterialRippleLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="70dp"
app:mrl_rippleOverlay="false"
app:mrl_rippleColor="#585858"
app:mrl_rippleAlpha="0.2"
app:mrl_rippleHover="true">
app:mrl_rippleColor="#585858"
app:mrl_rippleHover="true"
app:mrl_rippleOverlay="true">

<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:textSize="20sp"
android:paddingRight="8dp"
android:gravity="center_vertical"
android:background="@drawable/selector"
android:singleLine="true"
android:ellipsize="end"
android:paddingRight="8dp"
/>
android:singleLine="true"
android:textSize="20sp" />

</com.balysv.materialripple.MaterialRippleLayout>
5 changes: 5 additions & 0 deletions demo/src/main/res/layout/demo_recycler.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
14 changes: 14 additions & 0 deletions demo/src/main/res/layout/demo_recycler_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="70dp"
android:minHeight="70dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="center_vertical"
android:background="@drawable/selector"
android:ellipsize="end"
android:singleLine="true"
android:textSize="20sp" />
9 changes: 7 additions & 2 deletions demo/src/main/res/menu/menu_button.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/switch_button"
android:id="@+id/switch_list"
app:showAsAction="ifRoom|withText"
android:title="@string/switch_button"/>
android:title="@string/switch_list"/>

<item
android:id="@+id/switch_recycler"
app:showAsAction="ifRoom|withText"
android:title="@string/switch_recycler"/>
</menu>
9 changes: 7 additions & 2 deletions demo/src/main/res/menu/menu_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/switch_list"
android:id="@+id/switch_button"
app:showAsAction="ifRoom|withText"
android:title="@string/switch_list"/>
android:title="@string/switch_button"/>

<item
android:id="@+id/switch_recycler"
app:showAsAction="ifRoom|withText"
android:title="@string/switch_recycler"/>
</menu>
15 changes: 15 additions & 0 deletions demo/src/main/res/menu/menu_recycler.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?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/switch_button"
app:showAsAction="ifRoom|withText"
android:title="@string/switch_button"/>

<item
android:id="@+id/switch_list"
app:showAsAction="ifRoom|withText"
android:title="@string/switch_list"/>
</menu>
5 changes: 3 additions & 2 deletions demo/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Material Ripple Demo</string>
<string name="switch_list">Switch to ListView</string>
<string name="switch_button">Switch to Buttons</string>
<string name="switch_list">ListView</string>
<string name="switch_button">Buttons</string>
<string name="switch_recycler">RecyclerView</string>
</resources>

0 comments on commit 06afef8

Please sign in to comment.