@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.hmkcode.android.recyclerview"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="8"
|
||||
android:targetSdkVersion="21" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:label="@string/app_name" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<shape xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:shape="rectangle">
|
||||
<solid android:color="#ffffff" />
|
||||
<stroke
|
||||
android:width="1dip"
|
||||
android:color="#cccccc" />
|
||||
</shape>
|
||||
|
After Width: | Height: | Size: 1.6 KiB |
|
After Width: | Height: | Size: 1.8 KiB |
|
After Width: | Height: | Size: 1.8 KiB |
|
After Width: | Height: | Size: 7.5 KiB |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 2.0 KiB |
@ -0,0 +1,17 @@
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="com.hmkcode.android.recyclerview.MainActivity" >
|
||||
|
||||
|
||||
|
||||
<android.support.v7.widget.RecyclerView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/recyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity"
|
||||
/>
|
||||
</RelativeLayout>
|
||||
@ -0,0 +1,35 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="80dp"
|
||||
android:background="@drawable/border"
|
||||
>
|
||||
|
||||
<!-- icon -->
|
||||
<ImageView
|
||||
android:id="@+id/item_icon"
|
||||
android:layout_width="64dp"
|
||||
android:layout_height="64dp"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:layout_marginRight="8dp"
|
||||
android:layout_marginTop="1dp"
|
||||
android:layout_marginBottom="1dp"
|
||||
android:contentDescription="icon"
|
||||
android:src="@drawable/ic_launcher"
|
||||
/>
|
||||
|
||||
<!-- title -->
|
||||
<TextView
|
||||
android:id="@+id/item_title"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_toRightOf="@+id/item_icon"
|
||||
android:layout_alignBaseline="@+id/item_icon"
|
||||
android:textColor="@android:color/darker_gray"
|
||||
android:layout_marginLeft="8dp"
|
||||
android:layout_marginRight="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:textSize="22dp" />
|
||||
|
||||
</RelativeLayout>
|
||||
@ -0,0 +1,32 @@
|
||||
package com.hmkcode.android.recyclerview;
|
||||
|
||||
public class ItemData {
|
||||
|
||||
|
||||
private String title;
|
||||
private int imageUrl;
|
||||
|
||||
public ItemData(String title,int imageUrl){
|
||||
|
||||
this.title = title;
|
||||
this.imageUrl = imageUrl;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public int getImageUrl() {
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
public void setImageUrl(int imageUrl) {
|
||||
this.imageUrl = imageUrl;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
package com.hmkcode.android.recyclerview;
|
||||
|
||||
import android.support.v7.widget.DefaultItemAnimator;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.app.Activity;
|
||||
import android.os.Bundle;
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
|
||||
|
||||
|
||||
ItemData itemsData[] = { new ItemData("Help",R.drawable.help),
|
||||
new ItemData("Delete",R.drawable.content_discard),
|
||||
new ItemData("Cloud",R.drawable.collections_cloud),
|
||||
new ItemData("Favorite",R.drawable.rating_favorite),
|
||||
new ItemData("Like",R.drawable.rating_good),
|
||||
new ItemData("Rating",R.drawable.rating_important)};
|
||||
|
||||
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(this));
|
||||
|
||||
MyAdapter mAdapter = new MyAdapter(itemsData);
|
||||
recyclerView.setAdapter(mAdapter);
|
||||
recyclerView.setItemAnimator(new DefaultItemAnimator());
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,66 @@
|
||||
package com.hmkcode.android.recyclerview;
|
||||
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
|
||||
private ItemData[] itemsData;
|
||||
|
||||
public MyAdapter(ItemData[] itemsData) {
|
||||
this.itemsData = itemsData;
|
||||
}
|
||||
|
||||
// Create new views (invoked by the layout manager)
|
||||
@Override
|
||||
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
|
||||
int viewType) {
|
||||
// create a new view
|
||||
View itemLayoutView = LayoutInflater.from(parent.getContext())
|
||||
.inflate(R.layout.item_layout, null);
|
||||
|
||||
// create ViewHolder
|
||||
|
||||
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
|
||||
return viewHolder;
|
||||
}
|
||||
|
||||
// Replace the contents of a view (invoked by the layout manager)
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder viewHolder, int position) {
|
||||
|
||||
// - get data from your itemsData at this position
|
||||
// - replace the contents of the view with that itemsData
|
||||
|
||||
viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
|
||||
viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
|
||||
|
||||
|
||||
}
|
||||
|
||||
// inner class to hold a reference to each item of RecyclerView
|
||||
public static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
public TextView txtViewTitle;
|
||||
public ImageView imgViewIcon;
|
||||
|
||||
public ViewHolder(View itemLayoutView) {
|
||||
super(itemLayoutView);
|
||||
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.item_title);
|
||||
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.item_icon);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Return the size of your itemsData (invoked by the layout manager)
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return itemsData.length;
|
||||
}
|
||||
}
|
||||