In the below example I have created a RecyclerView, in RecyclerView I have added three category (Image, Title and Subtitles). I have also added four diffrent item in RecyclerView. First I have added RecyclerView dependency to Grandle file. and in next step I have added RecyclerView in activity_main.xml layout, then I have created new list_item.xml layout.
After that I have created LinearLayout, In LinearLayout I have added ImageView and two TextView.
In next step I have created two class first is Item and second is MyAdapter. MyAdapter extend with RecyclerView and next In MyAdapter I have created MyViewHolder class MyViewHolder extends RecyclerView.ViewHolder class, now next I have used onBindViewHolder method after then next step: In MainActivity I have created a new method prepareItem() here I have put image, title and subtitle name value. You can see below program it will clearly describe you "How to add title , subtitle and Image in RecyclerView".
Step(1)I have added first RecyclerView dependency to Grandle file.
compile 'com.android.support:recyclerview-v7:21.0.+'
Step(2)activity_main.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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
Step(3)-Created a new list_item.xml layout-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/icon"
android:paddingRight="10dp"
android:paddingLeft="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:layout_toRightOf="@+id/icon"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:textColor="#CC0033"
android:textSize="24dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/subtitle"
android:layout_toRightOf="@+id/icon"
android:layout_below="@+id/title"
android:paddingLeft="14dp"
android:textColor="#3399FF"
android:textSize="14dp"/>
</LinearLayout>
Step(4)- Created a new Item class-
public class Item {
private int imageId;
private String title;
private String subtitle;
public Item(Integer imageId,String title,String subtitle){
this.imageId=imageId;
this.title =title;
this.subtitle=subtitle;
}
public int getImageId(){return imageId;}
public void setImageId(int imageId){
this.imageId=imageId;
}
public String getTitle(){return title;}
public void setTitle(String title){
this.title=title;
}
public String getSubtitle(){return subtitle;}
public void setSubtitle(String subtitle){
this.subtitle=subtitle;
}
}
Step(5)- Created MyAdapter class-
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private List<Item>itemList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, subtitle;
public ImageView icon;
private LinearLayout main;
public MyViewHolder(final View parent) {
super(parent);
title = (TextView) parent.findViewById(R.id.title);
subtitle = (TextView) parent.findViewById(R.id.subtitle);
icon = (ImageView) parent.findViewById(R.id.icon);
main = (LinearLayout) parent.findViewById(R.id.main);
main.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(itemView.getContext(), "Position:" + Integer.toString(getPosition()), Toast.LENGTH_SHORT).show();
}
});
}
}
public MyAdapter(List<Item>itemList){
this.itemList=itemList;
}
@Override
public MyAdapter.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) {
Item row=itemList.get(position);
holder.title.setText(row.getTitle());
holder.subtitle.setText(row.getSubtitle());
holder.icon.setImageResource(row.getImageId());
}
@Override
public int getItemCount() {
return itemList.size();
}
}
Step(6)-MainActivity-
public class MainActivity extends Activity {
private List<Item> itemList = new ArrayList<>();
private RecyclerView recyclerview;
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerview=(RecyclerView)findViewById(R.id.recycler_view);
mAdapter = new MyAdapter(itemList);
RecyclerView.LayoutManager mLayoutManger = new LinearLayoutManager(getApplicationContext());
recyclerview.setLayoutManager(mLayoutManger);
recyclerview.setItemAnimator(new DefaultItemAnimator());
recyclerview.setAdapter(mAdapter);
prepareItem();
}
private void prepareItem() {
Item item = new Item(R.drawable.two,"Wopro","Wipro is software compnay");
itemList.add(item);
item = new Item(R.drawable.three,"TCS","TCS is service base compnay");
itemList.add(item);
item = new Item(R.drawable.four,"Ericsson","Ericsson is a networking compnay ");
itemList.add(item);
item= new Item(R.drawable.no,"Evon","Evon is hub of software devlopment ");
itemList.add(item);
mAdapter.notifyDataSetChanged();
recyclerview.setAdapter(mAdapter);
}
}
0 Comment(s)