Recycler view without binding for binger.
If you are use ‘com.google.android.material:material:1.2.1’ then no need to add recyclerview dependency
Add Dependency on app build.gradle file.
dependencies {
implementation ‘androidx.recyclerview:recyclerview:1.1.0’
}
Main.xml with recycler view
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat 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" android:layout_marginTop="5dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:background="#F1F1F1" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" tools:listitem="@layout/item_view" tools:itemCount="5" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/> </androidx.appcompat.widget.LinearLayoutCompat>
Main.java file
import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.RecyclerView; import android.os.Bundle; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private RecyclerView recyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { recyclerView = findViewById(R.id.recycler); Adapter adapter = new Adapter(this,setData()); recyclerView.setAdapter(adapter); } private List<DataBean> setData() { List<DataBean> dataBeanList = new ArrayList<>(); for(int i=0;i<15;i++) { DataBean dataBean = new DataBean(); dataBean.setTitle("My Title"); dataBean.setDes("Lorem Ipsum is simply dummy text of the printing and typesetting industry."); dataBean.setImg(R.drawable.h3); dataBeanList.add(dataBean); } return dataBeanList; } }
Adapter.java file
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.core.widget.ImageViewCompat; import androidx.core.widget.TextViewCompat; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder>{ private Context context; private List<DataBean> dataBeanList; public Adapter(Context context,List<DataBean> dataBeanList) { this.context = context; this.dataBeanList = dataBeanList; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { LayoutInflater layoutInflater = LayoutInflater.from(context); View view = layoutInflater.inflate(R.layout.item_view,parent,false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { holder.title.setText(dataBeanList.get(position).getTitle()); holder.des.setText(dataBeanList.get(position).getDes()); holder.img.setImageResource(dataBeanList.get(position).getImg()); } @Override public int getItemCount() { return dataBeanList.size(); } class ViewHolder extends RecyclerView.ViewHolder{ TextView title,des; ImageView img; public ViewHolder(@NonNull View itemView) { super(itemView); title = itemView.findViewById(R.id.title); des = itemView.findViewById(R.id.des); img = itemView.findViewById(R.id.img); } } }
item.xml file
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat 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:layout_marginTop="5dp" android:background="#ffffff" android:orientation="horizontal"> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/img" android:layout_width="50dp" android:layout_height="50dp" app:srcCompat="@drawable/h3" /> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingRight="5dp" android:text="My title" /> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/des" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingRight="5dp" android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry." /> </androidx.appcompat.widget.LinearLayoutCompat> </androidx.appcompat.widget.LinearLayoutCompat>
DataBean.java file
public class DataBean { private int img; private String title; private String des; public int getImg() { return img; } public void setImg(int img) { this.img = img; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDes() { return des; } public void setDes(String des) { this.des = des; } }
Recycler view without binding for binger.
If you are use ‘com.google.android.material:material:1.2.1’ then no need to add recyclerview dependency
Add Dependency on app build.gradle file.
dependencies { implementation 'androidx.recyclerview:recyclerview:1.1.0' }
Main.xml with recycler view
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat 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" android:layout_marginTop="5dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:background="#F1F1F1" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" tools:listitem="@layout/item_view" tools:itemCount="5" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/> </androidx.appcompat.widget.LinearLayoutCompat>
Main.kt file
import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.recyclerview.widget.RecyclerView import java.util.* class MainActivity : AppCompatActivity() { private var recyclerView: RecyclerView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) recyclerView = findViewById(R.id.recycler) val adapter = Adapter(this, setData()) recyclerView!!.adapter = adapter } private fun setData(): List<DataBean>? { val dataBeanList: MutableList<DataBean> = ArrayList() for (i in 0..14) { val dataBean = DataBean(R.drawable.h3,"My Title","Lorem Ipsum is simply dummy text of the printing and typesetting industry.") dataBeanList.add(dataBean) } return dataBeanList } }
Adapter.kt file
import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.ImageView import android.widget.TextView import androidx.recyclerview.widget.RecyclerView class Adapter( var context: Context?, var dataBeanList: List<DataBean?>? ): RecyclerView.Adapter<Adapter.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val layoutInflater = LayoutInflater.from(context) val view = layoutInflater.inflate(R.layout.item_view, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.title.setText(dataBeanList!![position]!!.title) holder.des.setText(dataBeanList!![position]!!.des) holder.img.setImageResource(dataBeanList!![position]!!.img) } override fun getItemCount(): Int { return dataBeanList!!.size } class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { var title: TextView var des: TextView var img: ImageView init { title = itemView.findViewById(R.id.title) des = itemView.findViewById(R.id.des) img = itemView.findViewById(R.id.img) } } }
item.xml file
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat 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:layout_marginTop="5dp" android:background="#ffffff" android:orientation="horizontal"> <androidx.appcompat.widget.AppCompatImageView android:id="@+id/img" android:layout_width="50dp" android:layout_height="50dp" app:srcCompat="@drawable/h3" /> <androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingRight="5dp" android:text="My title" /> <androidx.appcompat.widget.AppCompatTextView android:id="@+id/des" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="5dp" android:paddingRight="5dp" android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry." /> </androidx.appcompat.widget.LinearLayoutCompat> </androidx.appcompat.widget.LinearLayoutCompat>
Create data file DataBean.kt file
data class DataBean (var img:Int, var title: String, var des: String)
Recycler view or List View in Flutter :
Create Project : file -> new flutter project
Create Assets folder in main Project folder and put image here
After that entry of assets folder in project ‘pubspec.yaml ‘ file
assets: - assets/images/
main.dart file
import 'DataBean.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(title: 'List View'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { List<DataBean> list = []; @override void initState() { super.initState(); for(int i=0;i<20;i++) { DataBean _bean = new DataBean("Your Title","Lorem Ipsum is simply dummy text of the printing and typesetting industry.","assets/images/h3.png"); list.add(_bean); } print(list[0].title); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: ListView.builder( scrollDirection: Axis.vertical, // itemCount: entries.length, itemCount: list.length, shrinkWrap: true, physics: ClampingScrollPhysics(), itemBuilder: (BuildContext context, int index) { return Column(children: [Row( mainAxisAlignment: MainAxisAlignment.start, children: [ Column(children: <Widget>[ Container( alignment: Alignment.topLeft, height: 80, width: 80, margin: EdgeInsets.only(left: 5, top: 5, bottom: 5, right: 5), child: Image.asset( list[index].Img, fit: BoxFit.fitHeight, height: 80, width: 80, )) ]), Expanded( child: Container( margin: EdgeInsets.only(top: 5, bottom: 5, right: 5), child: Column( mainAxisAlignment: MainAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text( list[index].title, ), Text( list[index].des, maxLines: 2, overflow: TextOverflow.ellipsis, ), ], ), )) ], ),Divider() ],);},) ); } }
DataBean.dart
class DataBean{ String title; String des; String Img; DataBean( this.title, this.des, this.Img ); }