Skip to content

Commit 8b10c2e

Browse files
committed
add recyclerview example
1 parent 0065176 commit 8b10c2e

File tree

7 files changed

+242
-1
lines changed

7 files changed

+242
-1
lines changed

app/build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@ android {
1919
}
2020
}
2121

22+
ext {
23+
supportLibraryVersion = '26.1.0'
24+
}
25+
2226
dependencies {
2327
compile fileTree(dir: 'libs', include: ['*.jar'])
24-
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
28+
compile "com.android.support:appcompat-v7:$supportLibraryVersion"
29+
compile "com.android.support:recyclerview-v7:$supportLibraryVersion"
2530
compile 'com.android.support:support-annotations:25.3.1'
2631
compile project(':expandabletextview')
2732

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package at.blogc.android.activities;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.Nullable;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.support.v7.widget.LinearLayoutManager;
7+
import android.support.v7.widget.RecyclerView;
8+
9+
import at.blogc.android.adapters.RecyclerViewAdapter;
10+
import at.blogc.android.views.R;
11+
12+
/**
13+
* Copyright (C) 2017 Cliff Ophalvens (Blogc.at)
14+
*
15+
* Licensed under the Apache License, Version 2.0 (the "License");
16+
* you may not use this file except in compliance with the License.
17+
* You may obtain a copy of the License at
18+
*
19+
* http://www.apache.org/licenses/LICENSE-2.0
20+
*
21+
* Unless required by applicable law or agreed to in writing, software
22+
* distributed under the License is distributed on an "AS IS" BASIS,
23+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24+
* See the License for the specific language governing permissions and
25+
* limitations under the License.
26+
*/
27+
public class RecyclerViewActivity extends AppCompatActivity
28+
{
29+
private RecyclerView recyclerView;
30+
31+
@Override
32+
protected void onCreate(@Nullable final Bundle savedInstanceState)
33+
{
34+
super.onCreate(savedInstanceState);
35+
this.setContentView(R.layout.activity_recyclerview);
36+
37+
// find views
38+
this.recyclerView = this.findViewById(R.id.recyclerview);
39+
40+
// create layout manager
41+
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
42+
this.recyclerView.setLayoutManager(layoutManager);
43+
44+
// bind adapter to recyclerview
45+
final RecyclerViewAdapter adapter = new RecyclerViewAdapter();
46+
this.recyclerView.setAdapter(adapter);
47+
}
48+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package at.blogc.android.adapters;
2+
3+
import android.content.Context;
4+
import android.support.annotation.NonNull;
5+
import android.support.v7.widget.RecyclerView;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
10+
import at.blogc.android.adapters.viewholders.ExpandableTextViewHolder;
11+
import at.blogc.android.views.R;
12+
13+
/**
14+
* Copyright (C) 2017 Cliff Ophalvens (Blogc.at)
15+
*
16+
* Licensed under the Apache License, Version 2.0 (the "License");
17+
* you may not use this file except in compliance with the License.
18+
* You may obtain a copy of the License at
19+
*
20+
* http://www.apache.org/licenses/LICENSE-2.0
21+
*
22+
* Unless required by applicable law or agreed to in writing, software
23+
* distributed under the License is distributed on an "AS IS" BASIS,
24+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25+
* See the License for the specific language governing permissions and
26+
* limitations under the License.
27+
*/
28+
public class RecyclerViewAdapter extends RecyclerView.Adapter<ExpandableTextViewHolder>
29+
{
30+
private LayoutInflater layoutInflater;
31+
32+
private LayoutInflater getLayoutInflater(@NonNull final Context context)
33+
{
34+
if (this.layoutInflater == null)
35+
{
36+
this.layoutInflater = LayoutInflater.from(context);
37+
}
38+
39+
return this.layoutInflater;
40+
}
41+
42+
@Override
43+
public ExpandableTextViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType)
44+
{
45+
final View itemView = this.getLayoutInflater(parent.getContext()).inflate(R.layout.listitem_recyclerview, parent, false);
46+
47+
return new ExpandableTextViewHolder(itemView);
48+
}
49+
50+
@Override
51+
public void onBindViewHolder(final ExpandableTextViewHolder holder, final int position)
52+
{
53+
holder.bindPosition(position);
54+
}
55+
56+
@Override
57+
public int getItemCount()
58+
{
59+
return 100;
60+
}
61+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package at.blogc.android.adapters.viewholders;
2+
3+
import android.support.v7.widget.RecyclerView;
4+
import android.view.View;
5+
import android.view.animation.OvershootInterpolator;
6+
import android.widget.Button;
7+
8+
import at.blogc.android.views.ExpandableTextView;
9+
import at.blogc.android.views.R;
10+
11+
/**
12+
* Copyright (C) 2017 Cliff Ophalvens (Blogc.at)
13+
*
14+
* Licensed under the Apache License, Version 2.0 (the "License");
15+
* you may not use this file except in compliance with the License.
16+
* You may obtain a copy of the License at
17+
*
18+
* http://www.apache.org/licenses/LICENSE-2.0
19+
*
20+
* Unless required by applicable law or agreed to in writing, software
21+
* distributed under the License is distributed on an "AS IS" BASIS,
22+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23+
* See the License for the specific language governing permissions and
24+
* limitations under the License.
25+
*/
26+
public class ExpandableTextViewHolder extends RecyclerView.ViewHolder
27+
implements View.OnClickListener
28+
{
29+
private final ExpandableTextView expandableTextView;
30+
private final Button buttonToggle;
31+
32+
public ExpandableTextViewHolder(final View itemView)
33+
{
34+
super(itemView);
35+
36+
// find views
37+
this.expandableTextView = itemView.findViewById(R.id.expandableTextView);
38+
this.buttonToggle = itemView.findViewById(R.id.button_toggle);
39+
40+
// set interpolators for both expanding and collapsing animations
41+
this.expandableTextView.setInterpolator(new OvershootInterpolator());
42+
43+
// toggle the ExpandableTextView
44+
this.buttonToggle.setOnClickListener(this);
45+
}
46+
47+
public void bindPosition(final int position)
48+
{
49+
final String loremIpsum = position + ": " + this.itemView.getContext().getString(R.string.lorem_ipsum);
50+
this.expandableTextView.setText(loremIpsum);
51+
}
52+
53+
//region View.OnClickListener interface
54+
55+
@Override
56+
public void onClick(final View v)
57+
{
58+
this.buttonToggle.setText(this.expandableTextView.isExpanded() ? R.string.expand : R.string.collapse);
59+
this.expandableTextView.toggle();
60+
}
61+
62+
//endregion
63+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (C) 2017 Cliff Ophalvens (Blogc.at)
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<android.support.v7.widget.RecyclerView
17+
xmlns:android="http://schemas.android.com/apk/res/android"
18+
android:id="@+id/recyclerview"
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"/>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (C) 2017 Cliff Ophalvens (Blogc.at)
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
<RelativeLayout
17+
xmlns:android="http://schemas.android.com/apk/res/android"
18+
xmlns:app="http://schemas.android.com/apk/res-auto"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:paddingBottom="@dimen/activity_vertical_margin"
22+
android:paddingLeft="@dimen/activity_horizontal_margin"
23+
android:paddingRight="@dimen/activity_horizontal_margin"
24+
android:paddingTop="@dimen/activity_vertical_margin">
25+
26+
<at.blogc.android.views.ExpandableTextView
27+
android:id="@+id/expandableTextView"
28+
android:layout_width="match_parent"
29+
android:layout_height="wrap_content"
30+
android:text="@string/lorem_ipsum"
31+
android:maxLines="5"
32+
android:ellipsize="end"
33+
app:animation_duration="750"/>
34+
35+
<Button
36+
android:id="@+id/button_toggle"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:layout_below="@+id/expandableTextView"
40+
android:layout_marginTop="@dimen/activity_vertical_margin"
41+
android:text="@string/expand"/>
42+
43+
</RelativeLayout>

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ buildscript {
1414
allprojects {
1515
repositories {
1616
jcenter()
17+
google()
1718
}
1819
}
1920

0 commit comments

Comments
 (0)