How to make Recyclerview with Recyclerview Adopter in android App ?
RecyclerView overview. The RecyclerView widget is a more advanced and flexible version of ListView. In the RecyclerView model, several different components work together to display your data. The overall container for your user interface is a RecyclerView object that you add to your layout.
New We Are Making first layout file in Recycleview
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/itemrecycylemen"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
now we Are Recycle view layout file this file hold in recyclevie use agin and agin
in a app.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="@+id/homelayoute"
app:cardCornerRadius="10dp"
android:layout_margin="5dp"
android:background="@drawable/productebackground"
android:layout_width="match_parent"
android:layout_height="300dp"
android:padding="2dp">
<LinearLayout
android:orientation="vertical"
android:background="@drawable/productebackground"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_weight="2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_marginBottom="5dp"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:background="@drawable/buttonback"
app:cardCornerRadius="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:scaleType="fitXY"
android:id="@+id/homrproductimg"
android:src="@drawable/logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:gravity="center_vertical"
android:layout_gravity="center"
android:layout_weight="1"
android:id="@+id/homeproductname"
android:text="@string/ITEMNAME"
android:textSize="15dp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:layout_gravity="end"
android:layout_width="28dp"
android:layout_height="28dp"
android:id="@+id/mostlikepost"
android:src="@drawable/vector_heart_red"
/>
<!-- <LinearLayout-->
<!-- android:orientation="horizontal"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="wrap_content">-->
<!-- <TextView-->
<!-- android:text="Available in:"-->
<!-- android:textSize="18dp"-->
<!-- android:textColor="@color/black"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- <TextView-->
<!-- android:layout_marginLeft="5dp"-->
<!-- android:text="1"-->
<!-- android:textSize="18dp"-->
<!-- android:textColor="@color/black"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- </LinearLayout>-->
<!-- <LinearLayout-->
<!-- android:background="@drawable/buttonback"-->
<!-- android:gravity="center"-->
<!-- android:layout_gravity="end"-->
<!-- android:orientation="horizontal"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="30dp">-->
<!-- <TextView-->
<!-- android:id="@+id/addclick"-->
<!-- android:textStyle="bold"-->
<!-- android:background="@drawable/buttonback"-->
<!-- android:text="Add"-->
<!-- android:gravity="center"-->
<!-- android:textSize="15dp"-->
<!-- android:textColor="@color/White"-->
<!-- android:layout_width="50dp"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- <Button-->
<!-- android:id="@+id/decrement"-->
<!-- android:background="@drawable/buttonback"-->
<!-- android:visibility="gone"-->
<!-- android:text="-"-->
<!-- android:textSize="18dp"-->
<!-- android:textStyle="bold"-->
<!-- android:textColor="@color/White"-->
<!-- android:layout_width="30dp"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- <TextView-->
<!-- android:id="@+id/addvalue"-->
<!-- android:visibility="gone"-->
<!-- android:layout_marginLeft="5dp"-->
<!-- android:text="0"-->
<!-- android:textSize="18dp"-->
<!-- android:textColor="@color/White"-->
<!-- android:layout_width="wrap_content"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- <Button-->
<!-- android:id="@+id/increment"-->
<!-- android:textStyle="bold"-->
<!-- android:background="@drawable/buttonback"-->
<!-- android:text="+"-->
<!-- android:textSize="18dp"-->
<!-- android:textColor="@color/White"-->
<!-- android:layout_width="30dp"-->
<!-- android:layout_height="wrap_content"/>-->
<!-- </LinearLayout>-->
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="20dp">
<ImageView
android:layout_gravity="center"
android:layout_width="12dp"
android:layout_height="12dp"
android:src="@drawable/rupy"
/>
<TextView
android:layout_weight="2"
android:textStyle="bold"
android:gravity="start|center_vertical"
android:text="390"
android:textSize="15dp"
android:textColor="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:background="@drawable/substract"
android:layout_weight="2"
android:gravity="start|center_vertical"
android:text="120"
android:textSize="12dp"
android:singleLine="true"
android:textColor="@color/gray"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:layout_weight="1"
android:gravity="start|center_vertical"
android:text="(Included Tex)"
android:textSize="12sp"
android:textColor="@color/gray"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>Now we Are making Recycle view Adopter in this projectpackage com.vivid.myshop.home;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import com.vivid.myshop.R;
import com.vivid.myshop.fullcontantview.fullitemview;
import java.util.ArrayList;
import static com.vivid.myshop.R.id.homrproductimg;
public class homeAdopter extends RecyclerView.Adapter<homeAdopter.MyViewHolder> {
ArrayList personNames;
ArrayList personImages;
Activity fragmentActivity;
homeItem[] homeItem;
public static final String SHARED_PREF_NAME = "bucket";
int a=0;
// private Clickebucketvalue clickebucketvalue;
public homeAdopter(Activity fragmentActivity, homeItem[] homeItem) {
this.fragmentActivity=fragmentActivity;
this.homeItem=homeItem;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
View view=layoutInflater.inflate(R.layout.recycyleviewitem,parent,false);
return new homeAdopter.MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, final int position) {
final homeItem homeitem= homeItem[position];
holder.productname.setText(homeItem[position].getProductName());
holder.productImage.setImageResource(homeItem[position].getImageurl());
// holder.itemView.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Intent intent=new Intent(fragmentActivity, fullitemview.class);
// fragmentActivity.startActivity(intent);
// }
//});
}
@Override
public int getItemCount() {
return homeItem.length;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView productname,addclick;
ImageView productImage;
CardView homelayout;
TextView peoducevalue;
// Button increment,decrement;
// final SharedPreferences sharedPreferences1 = fragmentActivity.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
// public static final String b=" 0";
public MyViewHolder(@NonNull View itemView) {
super(itemView);
productImage=itemView.findViewById(homrproductimg);
productname=itemView.findViewById(R.id.homeproductname);
homelayout=itemView.findViewById(R.id.homelayoute);
peoducevalue=itemView.findViewById(R.id.addvalue);
// increment=itemView.findViewById(R.id.increment);
// decrement=itemView.findViewById(R.id.decrement);
// addclick=itemView.findViewById(R.id.addclick);
//
// increment.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// addclick.setVisibility(View.GONE);
// decrement.setVisibility(View.VISIBLE);
// a=a+1;
// if(a>=0)
// {
// sharedprefences();
// //Creating editor to store values to shared preferences
// peoducevalue.setVisibility(View.VISIBLE);
// // Toast.makeText(fragmentActivity,"Button clicked first time!"+a, Toast.LENGTH_LONG).show();
// peoducevalue.setText(""+a);
// }
// else
// {
// }
// }
// });
// decrement.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// a=a-1;
// if(a>=0){
// peoducevalue.setText(""+a);
// sharedprefences();
// if(a==0){
// // peoducevalue.setText(""+0);
// decrement.setVisibility(View.GONE);
// peoducevalue.setVisibility(View.GONE);
// // peoducevalue.setText(""+a);
// addclick.setVisibility(View.VISIBLE);
// }
// }
// else{
// Toast.makeText(fragmentActivity,"No Item Added In Card", Toast.LENGTH_LONG).show();
// decrement.setVisibility(View.GONE);
// peoducevalue.setVisibility(View.GONE);
// addclick.setVisibility(View.VISIBLE);
// }
//
// }
// });
}
}
// public interface Clickebucketvalue{
// void onClicked(homeItem item );
// }
}Now we Are Strat Codeing in Main Activity filepackage com.bspl.suriyasareeparselldelevery;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {RecyclerView recyclerView;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);recyclerView=findViewById(R.id.itemrecycylemen);homeItem[] homeItem=new homeItem[]{new homeItem("cloth","1","red","l",R.drawable.shop,"sdjfsdfhshffjskjdf"),
new homeItem("cloth","2","red","l",R.drawable.shop,"sdjfsdfhshffjskjdf"),
new homeItem("cloth","3","red","l",R.drawable.shop,"sdjfsdfhshffjskjdf"),
new homeItem("cloth","4","red","l",R.drawable.shop,"sdjfsdfhshffjskjdf"),
new homeItem("cloth","5","red","l",R.drawable.shop,"sdjfsdfhshffjskjdf"),
new homeItem("cloth","6","red","l",R.drawable.shop,"sdjfsdfhshffjskjdf"),
new homeItem("cloth","7","red","l",R.drawable.shop,"sdjfsdfhshffjskjdf"),
new homeItem("cloth","8","red","l",R.drawable.shop,"sdjfsdfhshffjskjdf"),
new homeItem("cloth","9","red","l",R.drawable.shop,"sdjfsdfhshffjskjdf"),
};LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());}
recyclerView.setLayoutManager(linearLayoutManager);
homeAdopter customAdapter = new homeAdopter(getActivity(), homeItem);
recyclerView.setAdapter(customAdapter);
}
but if any wont to use Recycleview as a GridView in app
then use this code
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
recyclerView.setLayoutManager(gridLayoutManager);
homeAdopter customAdapter = new homeAdopter(getActivity(), homeItem);
recyclerView.setAdapter(customAdapter);click hear for nexthttps://androidcoderak06.blogspot.com/2020/10/akm

Comments
Post a Comment