Recyclerview Example
-----------MainActivity.java-----------------------
---------mainactivity.xml----------------------------
-------CalenderCardViewAdapter----------------------------
---------------build.gradle(dependency)-----------------------
-----------MainActivity.java-----------------------
package com.abc.jsonrecycle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; private CardViewAdapter cardViewAdapter; private List<MainModel> cardList = new ArrayList<>(); int images[] = new int[]{R.drawable.newimage, R.drawable.virat, R.drawable.image, R.drawable.image1,R.drawable.first}; int images1[] = new int[]{R.drawable.newimage, R.drawable.virat, R.drawable.image, R.drawable.image1,R.drawable.team}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); recyclerView = (RecyclerView) findViewById(R.id.RecyclerView); cardList.add(new MainModel("Sohel", "10 march 2017","Team India", images[4], images1[4])); cardList.add(new MainModel("Dollop", "11 march 2017", "Team India", images[1], images1[4])); cardList.add(new MainModel("Meenakshi", "12 march 2017", "Team India", images[2], images1[4])); cardList.add(new MainModel("Shirsh", "13 march 2017", "Team India", images[3], images1[4])); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); recyclerView.setLayoutManager(layoutManager); cardViewAdapter = new CardViewAdapter(cardList); recyclerView.setAdapter(cardViewAdapter); } }
---------mainactivity.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.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/RecyclerView"> </android.support.v7.widget.RecyclerView> </RelativeLayout>
-------CalenderCardViewAdapter----------------------------
package com.abc.jsonrecycle; import android.content.Context; 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.List; /** * Created by munnu on 09/03/2017. */ public class CardViewAdapter extends RecyclerView.Adapter<CardViewAdapter.MyViewHolder> { List<MainModel> clickCategoryList; Context context; public CardViewAdapter(List<MainModel> clickCategoryList) { this.clickCategoryList = clickCategoryList; } public class MyViewHolder extends RecyclerView.ViewHolder { TextView nameTextView, Date, Text; ImageView imageView, imageView1; public MyViewHolder(View view) { super(view); nameTextView = (TextView) view.findViewById(R.id.nameTextView); Date = (TextView) view.findViewById(R.id.Date); Text = (TextView) view.findViewById(R.id.Text); imageView = (ImageView) view.findViewById(R.id.imageView); imageView1 = (ImageView) view.findViewById(R.id.imageView1); } } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { context = parent.getContext(); View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.cardviewadapter, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(MyViewHolder holder, final int position) { final MainModel clickCategoryListObject = clickCategoryList.get(position); holder.nameTextView.setText(clickCategoryListObject.getName()); holder.Date.setText(clickCategoryListObject.getText()); holder.Text.setText(clickCategoryListObject.getText()); holder.imageView.setImageResource(clickCategoryListObject.getImage()); holder.imageView1.setImageResource(clickCategoryListObject.getImage1()); } @Override public int getItemCount() { return clickCategoryList.size(); } }------------------------cadrviewadapter.xml---------------------
<?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"
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"> <android.support.v7.widget.CardView
android:id="@+id/CardView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:elevation="10dp"
app:cardCornerRadius="10dp"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <ImageView
android:id="@+id/imageView"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:scaleType="fitXY"
android:src="@drawable/image" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_toRightOf="@+id/imageView"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:orientation="vertical"> <TextView
android:id="@+id/nameTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="@color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" /> <TextView
android:id="@+id/Date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Date"
android:textColor="@color/colorAccent"
android:textSize="16sp"
android:textStyle="normal" />
</LinearLayout>
<TextView
android:id="@+id/Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView"
android:layout_marginLeft="5dp"
android:text="M.S.Dhoni"
android:textColor="@color/colorPrimary"
android:textSize="16sp" /> <ImageView
android:id="@+id/imageView1"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Text"
android:src="@drawable/newimage" /> </RelativeLayout> </android.support.v7.widget.CardView> </RelativeLayout>
---------------build.gradle(dependency)-----------------------
compile 'com.android.support:appcompat-v7:25.0.1'testCompile 'junit:junit:4.12'compile 'com.android.support:design:25.0.1'compile 'com.android.support:recyclerview-v7:25.0.1'compile 'com.android.support:cardview-v7:25.0.1'
No comments:
Post a Comment