Recycler View
with us
picaso
online image load in android dependencies
HashMap adapter & View Holder
RecyclerView is flexible and efficient version of ListView. It is an container for rendering larger data set of views that can be recycled and scrolled very efficiently.RecyclerView is like traditional ListView widget, but with more flexibility to customizes and optimized to work with larger datasets,
Picasso is an image library for Android. It's created and maintained by Square, and caters to image loading and processing. It simplifies the process of displaying images from external locations. like amazon , or other online shopping site it has by default image and than after load a main image by internet connectivity .
for create simple RecyclerView also add Picasso
first add a dependencies in app module
// RecyclerView
implementation 'com.android.support:recyclerview-v7:26.1.0'
// picaso
implementation 'com.squareup.picasso:picasso:2.3.2'
preview of add dependencies ;-
after add both dependencies click on sync now
now we started with create a new activity for RecyclerView
- Main Activity.xml
- Main Activity.java
- recyclerviewadapter.java
- list_item.xml
1)Main Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.store.recycler.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recylerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
preview of Main Activity.xml :-
2)Main Activity.java
package com.example.store.recycler;
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.HashMap;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView recylerview;
List <HashMap<String, Object>> dataList = new ArrayList<>();
String[] ampName = new String[]{"Niranjan", "Shivam", "vaibhav", "Shivam", "vaibhav"};
String[] ampdesi = new String[]{"CEO", "MD", "GE","CEO", "MD",};
String[] images = new String[]{ "http://www.freepngimg.com/download/recycle/2-2-recycle-png.png"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recylerview = (RecyclerView)findViewById(R.id.recylerview);
recylerview.setLayoutManager(new LinearLayoutManager( MainActivity.this,LinearLayoutManager.VERTICAL,false));
for (int i = 0 ; i<ampName.length ;i++){
HashMap<String, Object> map = new HashMap<>();
map.put("name", ampName[i]);
map.put("designation", ampdesi[i]);
map.put("image",images[0]);
dataList.add(map);
}
recylerview .setAdapter(new recycleviewadapter( MainActivity.this,dataList));
}
}
3)recyclerviewadapter.java
package com.example.store.recycler;
import android.content.Context;
import android.graphics.Movie;
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.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.HashMap;
import java.util.List;
/**
* Created by store on 23-02-2018.
*/
public class recycleviewadapter extends RecyclerView.Adapter<recycleviewadapter.MyViewHolder> {
private List<HashMap<String,Object>> employeeList;
Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView tv1 ,tv2;
ImageView img1;
Button btn1;
public MyViewHolder(View view) {
super(view);
tv1 = (TextView) view.findViewById(R.id.tv1);
tv2 = (TextView) view.findViewById(R.id.tv2);
img1 = (ImageView) view.findViewById(R.id.img1);
btn1 = (Button)view.findViewById(R.id.btn1);
}
}
public recycleviewadapter(Context context ,List<HashMap<String,Object>> employeeList) {
this.employeeList = employeeList;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
HashMap<String,Object> movie = employeeList.get(position);
holder.tv1.setText(movie.get("name").toString());
holder.tv2.setText(movie.get("designation").toString());
//holder.img1.setImageResource(Integer.parseInt(movie.get("image").toString()));
Picasso.with(context)
.load(movie.get("image").toString())
.placeholder(R.drawable.icon)
.error(R.drawable.icon)
.into(holder.img1);
}
@Override
public int getItemCount() {
return employeeList.size();
}
}
4)list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="wrap_content"
android:padding="10dp"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="@+id/img1"
android:layout_width="match_parent"
android:layout_height="150dp"
app:srcCompat="@drawable/icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:layout_gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:layout_gravity="center">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:layout_gravity="center">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click " />
</LinearLayout>
</LinearLayout>
</LinearLayout>
preview of list_item.xml :-
video preview for that activity:-
thank you
No comments:
Post a Comment