Here we need to add the below dependencies files
dependencies {
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:+'
compile 'com.android.support:cardview-v7:23.0.+'
}
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="multicard.my.n.multicardrecyclerview.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
- card1.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:elevation="100dp"
card_view:cardBackgroundColor="#607D8B">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Card 1"
android:textColor="#ffffff" />
<TextView
android:id="@+id/temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textColor="#ffffff"
android:textSize="30sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
- Card2.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:elevation="100dp"
card_view:cardBackgroundColor="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Card 2" />
<TextView
android:id="@+id/headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="25sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:id="@+id/read_more"
android:background="#fff"
android:text="Read More" />
</LinearLayout>
</android.support.v7.widget.CardView>
- card3.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:elevation="100dp"
card_view:cardBackgroundColor="#00bcd4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Card3"
android:textColor="#ffffff" />
<TextView
android:id="@+id/score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textColor="#ffffff"
android:textSize="30sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
- MainActivity.java
package multicard.my.n.multicardrecyclerview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import static multicard.my.n.multicardrecyclerview.myCustomAdapter.card1;
import static multicard.my.n.multicardrecyclerview.myCustomAdapter.card2;
import static multicard.my.n.multicardrecyclerview.myCustomAdapter.card3;
/**
* Created by nithin
*/
public class MainActivity extends AppCompatActivity {
RecyclerView mRecyclerView;
private myCustomAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private String[] mDataset = {
"Card 1",
"Card 2",
"Card 3",
"Card 1"
};
int[] mDataSetTypes = {
card1,
card2,
card3,
card1
}; //view types
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new myCustomAdapter(mDataset, mDataSetTypes);
mRecyclerView.setAdapter(mAdapter);
}
}
- myCustomAdapter.java
package multicard.my.n.multicardrecyclerview;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by nithin
*/
public class myCustomAdapter extends RecyclerView.Adapter<myCustomAdapter.ViewHolder> {
private static final String TAG = "myCustomAdapter";
private String[] mDataSet;
private int[] mDataSetTypes;
public static final int card1 = 0;
public static final int card2 = 1;
public static final int card3 = 2;
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(View v) {
super(v);
}
}
public class WeatherViewHolder extends ViewHolder {
TextView temp;
public WeatherViewHolder(View v) {
super(v);
this.temp = (TextView) v.findViewById(R.id.temp);
}
}
public class ScoreViewHolder extends ViewHolder {
TextView score;
public ScoreViewHolder(View v) {
super(v);
this.score = (TextView) v.findViewById(R.id.score);
}
}
public class NewsViewHolder extends ViewHolder {
TextView headline;
Button read_more;
public NewsViewHolder(View v) {
super(v);
this.headline = (TextView) v.findViewById(R.id.headline);
this.read_more = (Button) v.findViewById(R.id.read_more);
}
}
public myCustomAdapter(String[] dataSet, int[] dataSetTypes) {
mDataSet = dataSet;
mDataSetTypes = dataSetTypes;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View v;
if (viewType == card1) {
v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card1, viewGroup, false);
return new WeatherViewHolder(v);
} else if (viewType == card2) {
v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card2, viewGroup, false);
return new NewsViewHolder(v);
} else {
v = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card3, viewGroup, false);
return new ScoreViewHolder(v);
}
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
if (viewHolder.getItemViewType() == card1) {
WeatherViewHolder holder = (WeatherViewHolder) viewHolder;
holder.temp.setText(mDataSet[position]);
}
else if (viewHolder.getItemViewType() == card2) {
NewsViewHolder holder = (NewsViewHolder) viewHolder;
holder.headline.setText(mDataSet[position]);
}
else {
ScoreViewHolder holder = (ScoreViewHolder) viewHolder;
holder.score.setText(mDataSet[position]);
}
}
@Override
public int getItemCount() {
return mDataSet.length;
}
@Override
public int getItemViewType(int position) {
return mDataSetTypes[position];
}
}
Source Code: https://github.com/Nithinpaul/MultiCardRecyclerView
Comments
Post a Comment