Skip to content

Commit 851f7d9

Browse files
committed
Android RecylerView
1 parent 81d3426 commit 851f7d9

5 files changed

Lines changed: 58 additions & 29 deletions

File tree

Binary file not shown.

com.vogella.android.recyclerview/project.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@
1212

1313
# Project target.
1414
target=android-21
15-
android.library.reference.1=../aaaa
Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,41 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:id="@+id/textView1"
4-
android:layout_width="wrap_content"
5-
android:layout_height="wrap_content"
6-
android:text="Large Text"
7-
android:textAppearance="?android:attr/textAppearanceLarge" />
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:layout_width="fill_parent"
3+
android:layout_height="?android:attr/listPreferredItemHeight"
4+
android:padding="6dip" >
5+
6+
<ImageView
7+
android:id="@+id/icon"
8+
android:layout_width="wrap_content"
9+
android:layout_height="fill_parent"
10+
android:layout_alignParentBottom="true"
11+
android:layout_alignParentTop="true"
12+
android:layout_marginRight="6dip"
13+
android:contentDescription="TODO"
14+
android:src="@drawable/ic_launcher" />
15+
16+
<TextView
17+
android:id="@+id/secondLine"
18+
android:layout_width="fill_parent"
19+
android:layout_height="26dip"
20+
android:layout_alignParentBottom="true"
21+
android:layout_alignParentRight="true"
22+
android:layout_toRightOf="@id/icon"
23+
android:ellipsize="marquee"
24+
android:singleLine="true"
25+
android:text="Description"
26+
android:textSize="12sp" />
27+
28+
<TextView
29+
android:id="@+id/firstLine"
30+
android:layout_width="fill_parent"
31+
android:layout_height="wrap_content"
32+
android:layout_above="@id/secondLine"
33+
android:layout_alignParentRight="true"
34+
android:layout_alignParentTop="true"
35+
android:layout_alignWithParentIfMissing="true"
36+
android:layout_toRightOf="@id/icon"
37+
android:gravity="center_vertical"
38+
android:text="Example application"
39+
android:textSize="16sp" />
40+
41+
</RelativeLayout>

com.vogella.android.recyclerview/src/com/vogella/android/recyclerview/MainActivity.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44

55
import android.app.Activity;
66
import android.os.Bundle;
7-
import android.support.v7.widget.DefaultItemAnimator;
87
import android.support.v7.widget.LinearLayoutManager;
98
import android.support.v7.widget.RecyclerView;
10-
import android.support.v7.widget.RecyclerView.ItemDecoration;
119

1210
public class MainActivity extends Activity {
1311
private RecyclerView recyclerView;
@@ -22,15 +20,16 @@ protected void onCreate(Bundle savedInstanceState) {
2220

2321
// use this setting to improve performance if you know that changes
2422
// in content do not change the layout size of the RecyclerView
25-
recyclerView.setHasFixedSize(true);
23+
// recyclerView.setHasFixedSize(true);
2624

2725
// use a linear layout manager
2826
mLayoutManager = new LinearLayoutManager(this);
2927
recyclerView.setLayoutManager(mLayoutManager);
30-
recyclerView.setHasFixedSize(true);
31-
recyclerView.setItemAnimator(new DefaultItemAnimator());
32-
RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
33-
recyclerView.addItemDecoration(itemDecoration);
28+
// Use the default animator
29+
// recyclerView.setItemAnimator(new DefaultItemAnimator());
30+
// you could add item decorators
31+
// RecyclerView.ItemDecoration itemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
32+
// recyclerView.addItemDecoration(itemDecoration);
3433

3534
ArrayList<String> input = new ArrayList<String>();
3635
for (int i = 0; i < 100; i++) {

com.vogella.android.recyclerview/src/com/vogella/android/recyclerview/MyAdapter.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.vogella.android.recyclerview;
22

33
import java.util.ArrayList;
4-
import java.util.List;
54

65
import android.support.v7.widget.RecyclerView;
76
import android.view.LayoutInflater;
@@ -10,20 +9,21 @@
109
import android.view.ViewGroup;
1110
import android.widget.TextView;
1211

13-
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>
14-
implements View.OnClickListener {
12+
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
1513
private ArrayList<String> mDataset;
1614

1715
// Provide a reference to the views for each data item
1816
// Complex data items may need more than one view per item, and
1917
// you provide access to all the views for a data item in a view holder
2018
public class ViewHolder extends RecyclerView.ViewHolder {
2119
// each data item is just a string in this case
22-
public TextView mTextView;
20+
public TextView txtHeader;
21+
public TextView txtFooter;
2322

24-
public ViewHolder(TextView v) {
23+
public ViewHolder(View v) {
2524
super(v);
26-
mTextView = v;
25+
txtHeader = (TextView) v.findViewById(R.id.firstLine);
26+
txtFooter = (TextView) v.findViewById(R.id.secondLine);
2727
}
2828
}
2929

@@ -48,8 +48,7 @@ public MyAdapter(ArrayList<String> myDataset) {
4848
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
4949
int viewType) {
5050
// create a new view
51-
TextView v = (TextView) LayoutInflater.from(parent.getContext())
52-
.inflate(R.layout.rowlayout, parent, false);
51+
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
5352
// set the view's size, margins, paddings and layout parameters
5453
ViewHolder vh = new ViewHolder(v);
5554
return vh;
@@ -61,13 +60,15 @@ public void onBindViewHolder(ViewHolder holder, int position) {
6160
// - get element from your dataset at this position
6261
// - replace the contents of the view with that element
6362
final String name = mDataset.get(position);
64-
holder.mTextView.setText(mDataset.get(position));
65-
holder.mTextView.setOnClickListener(new OnClickListener() {
63+
holder.txtHeader.setText(mDataset.get(position));
64+
holder.txtHeader.setOnClickListener(new OnClickListener() {
6665
@Override
6766
public void onClick(View v) {
6867
remove(name);
6968
}
7069
});
70+
71+
holder.txtFooter.setText("Footer: " + mDataset.get(position));
7172

7273
}
7374

@@ -77,8 +78,4 @@ public int getItemCount() {
7778
return mDataset.size();
7879
}
7980

80-
@Override
81-
public void onClick(View v) {
82-
83-
}
8481
}

0 commit comments

Comments
 (0)