design view xml
1.content_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="nc.example.com.lunch.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:id="@+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="nc.example.com.lunch.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:id="@+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
2.item_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:background="@drawable/design"
xmlns:tools="http://schemas.android.com/tools">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_margin="3dp"
android:layout_height="match_parent"
card_view:cardCornerRadius="1dp">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/sandwich" />
<TextView
android:id="@+id/txtdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:text="With a marvellous taste"
android:textColor="@color/black"
android:textStyle="bold|italic" />
<TextView
android:id="@+id/d_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="290dp"
android:text="Price Rs....."
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="@+id/f_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
</android.support.v7.widget.CardView>
</LinearLayout>
java side
1.MainActivity.java
package nc.example.com.lunch;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
String[]F_name,D_name;
int[] Img_res={R.drawable.roti,R.drawable.naan,R.drawable.alooparatha,
R.drawable.dalfry,R.drawable.kadi,R.drawable.pp};
ArrayList<Dataprovider>arrayList=new ArrayList<Dataprovider>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=(RecyclerView)findViewById(R.id.recycler_view);
F_name=getResources().getStringArray(R.array.names);
D_name=getResources().getStringArray(R.array.price);
int i=0;
for (String name:F_name)
{
Dataprovider dataprovider=new Dataprovider(Img_res[i],name,D_name[i]);
arrayList.add(dataprovider);
i++;
}
adapter=new RecyclerAdapter(arrayList);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
}
2.DataProvider
package nc.example.com.lunch;
/**
* Created by nc on 5/10/2017.
*/
public class Dataprovider {
public Dataprovider(int img_res,String f_name,String d_name)
{
this.setImg_res(img_res);
this.setF_name(f_name);
this.setD_name(d_name);
}
private int img_res;
private String f_name,d_name;
public int getImg_res()
{
return img_res;
}
public void setImg_res(int img_res)
{
this.img_res = img_res;
}
public String getF_name()
{
return f_name;
}
public void setF_name(String f_name)
{
this.f_name = f_name;
}
public String getD_name()
{
return d_name;
}
public void setD_name(String d_name)
{
this.d_name = d_name;
}
}
3.RecyclerAdapter
package nc.example.com.lunch;
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;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {
private ArrayList<Dataprovider> arrayList=new ArrayList<Dataprovider>();
public RecyclerAdapter(ArrayList<Dataprovider>arrayList)
{
this.arrayList=arrayList;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout,parent,false);
RecyclerViewHolder recyclerViewHolder=new RecyclerViewHolder(view);
return recyclerViewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, int position) {
Dataprovider dataprovider=arrayList.get(position);
holder.imageView.setImageResource(dataprovider.getImg_res());
holder.f_name.setText(dataprovider.getF_name());
holder.d_name.setText(dataprovider.getD_name());
}
@Override
public int getItemCount() {
return arrayList.size();
}
public static class RecyclerViewHolder extends RecyclerView.ViewHolder
{
ImageView imageView;
TextView f_name,d_name;
public RecyclerViewHolder(View view)
{
super(view);
imageView=(ImageView)view.findViewById(R.id.img);
f_name=(TextView)view.findViewById(R.id.f_name);
d_name=(TextView)view.findViewById(R.id.d_name);
}
}
}
BuildGRadle
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "nc.example.com.lunch"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:cardview-v7:25.2.0'
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
testCompile 'junit:junit:4.12'
}
Good post,
ReplyDeleteEasy to understand