Cardview in android studio example
Its a new widget for android, which can be used to display a card sort of a layout in android.Android CardView is such a view which has all material design properties, most importantly showing shadows according the elevation. The best part about this view is that it extends FrameLayout and it can be displayed on all the platforms of android since it’s available through the Support v7 library.
Please check more about recylerview and check out my recyclerView example
This is my example cardview with recylerview :
Android SDK doesn't includes the cardview class,first need to add the cardview support library to your build.graddle file.
Android SDK doesn't includes the cardview class,first need to add the cardview support library to your build.graddle file.
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:cardview-v7:23.0.+'
compile 'com.android.support:recyclerview-v7:23.0.0'
Declare CardView Layout
RecyclerView Lyaout:
Items.java
ClickListener.java
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:cardView="http://schemas.android.com/apk/res-auto"
android:id="@+id/carView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/imageView"/>
<TextView
android:id="@+id/price"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/name"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
RecyclerView Lyaout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CardViewActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
</RelativeLayout>
Items.java
package comre.example.velmurugan.cardviewdemo;
/**
* Created by velmurugan on 20/2/16.
*/
public class Items {
private String name;
private int price,itemImage;
Items(String mName,int mPrice,int mItemImage){
this.name = mName;
this.price = mPrice;
this.itemImage = mItemImage;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setPrice(int price) {
this.price = price;
}
public int getPrice() {
return price;
}
public int getItemImage() {
return itemImage;
}
public void setItemImage(int itemImage) {
this.itemImage = itemImage;
}
}
ClickListener.java
import android.view.View;
/**
* Created by velmurugan on 20/2/16.
*/
public interface ClickListener {
void onClick(View view,int position);
void onLongPress(View view,int position);
}
RecyclerViewAdapter.java
package comre.example.velmurugan.cardviewdemo;
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 org.w3c.dom.Text;
import java.util.List;
/**
* Created by velmurugan on 20/2/16.
*/
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>{
List<Items> itemsList;
RecyclerViewAdapter(List<Items> mItemList){
this.itemsList = mItemList;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView name,price;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.name);
price = (TextView)itemView.findViewById(R.id.price);
image = (ImageView)itemView.findViewById(R.id.imageView);
}
}
@Override
public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cardview, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(RecyclerViewAdapter.MyViewHolder holder, int position) {
Items items = itemsList.get(position);
holder.name.setText(items.getName());
holder.price.setText(String.valueOf(items.getPrice()));
holder.image.setBackgroundResource(items.getItemImage());
}
@Override
public int getItemCount() {
return itemsList.size();
}
}
RecyclerView touch listener
package comre.example.velmurugan.cardviewdemo;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
/**
* Created by velmurugan on 20/2/16.
*/
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ClickListener clickListener;
RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener){
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongPress(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
MainActivity.java
package comre.example.velmurugan.cardviewdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<Items> itemsList;
private RecyclerViewAdapter recyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
itemsList = new ArrayList<>();
recyclerViewAdapter = new RecyclerViewAdapter(itemsList);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, int position) {
Toast.makeText(getApplicationContext(), itemsList.get(position).getName() + " selected ", Toast.LENGTH_SHORT).show();
}
@Override
public void onLongPress(View view, int position) {
Toast.makeText(getApplicationContext(), itemsList.get(position).getName() + " long pressed ", Toast.LENGTH_SHORT).show();
}
}));
prepareItems();
}
private void prepareItems(){
Items items = new Items("Pen",20,R.mipmap.pen);
itemsList.add(items);
items = new Items("Pencil",10,R.mipmap.pencil);
itemsList.add(items);
items = new Items("Book",15,R.mipmap.book);
itemsList.add(items);
items = new Items("Note",40,R.mipmap.note);
itemsList.add(items);
}
}
Screenshot:
Download source code
No comments:
Post a Comment