Sometimes we need to show the list with different views so for this we can inflate two layouts in RecyclerView. To know how it is achieved follow the steps mentioned below:-
- Create two layouts xml files I have named them "programs_recyler_view" and "programs_recyler_view_fifth_multiple" following are the code for them
programs_recyler_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/program_mainlayout"
android:layout_width="match_parent"
android:background="@android:color/transparent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_between"
android:orientation="horizontal"
android:weightSum="2">
<RelativeLayout
android:id="@+id/rl_programsview_left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="4dp"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:layout_marginBottom="4dp">
<ImageView
android:scaleType="fitXY"
android:id="@+id/left_programe_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/left_programe_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<RelativeLayout
android:layout_marginRight="2dp"
android:layout_marginBottom="4dp"
android:id="@+id/rl_programsview_right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_weight="1">
<ImageView
android:scaleType="fitXY"
android:id="@+id/right_programe_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/right_programe_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
programs_recyler_view_fifth_multiple
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="@dimen/margin_between"
android:background="@android:color/transparent"
android:layout_width="match_parent" android:layout_height="match_parent">
<RelativeLayout
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:id="@+id/fifth_programe_m"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:layout_marginBottom="4dp">
<ImageView
android:scaleType="fitXY"
android:id="@+id/fifth_multiple_programe_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/fifth_multiple_programe_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
- Now inflate the layout according to the design you wish with the help of RecyclerView Adapter, below is the code for the adapter
public class ProgramsRecyclerViewAdapter extends RecyclerView.Adapter<ProgramsRecyclerViewAdapter.ViewHolder> {
private ArrayList<ProgramsFeedItem> list;
// private Context mContext;
private OnClickListener mOnClickEvent;
private ImageLoader imageLoader;
private DisplayImageOptions options;
private int orignal_height;
private int orignal_width;
private int five_width;
private int five_height;
String id,id_one;
public ProgramsRecyclerViewAdapter(ArrayList<ProgramsFeedItem> list, OnClickListener listener, int width) {
int widthone=width-10;
five_width=width-8;
five_height=(five_width/16)*9;
orignal_width=widthone/2;
int height=orignal_width/16;
orignal_height=height*9;
this.list = list;
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.imgnot_found)
.showImageOnFail(R.drawable.imgnot_found)
.considerExifParams(true)
.considerExifParams(true)
.build();
mOnClickEvent = listener;
}
@Override
public int getItemViewType(int position) {
return position;
}
// Create new views (invoked by the layout manager)
@Override
public ProgramsRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView;
int a = (viewType + 1) % 3;
if (a == 0) {
itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.programs_recyler_view_fifth_multiple, null);
} else {
itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.programs_recyler_view, null);
}
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
ImageView thumb, thumb_one, thumb_five;
int a = (position + 1) % 3;
int b = position % 2;
thumb = (ImageView) viewHolder.view.findViewById(R.id.left_programe_image_view);
thumb_one = (ImageView) viewHolder.view.findViewById(R.id.right_programe_image_view);
thumb_five = (ImageView) viewHolder.view.findViewById(R.id.fifth_multiple_programe_image_view);
if (a == 0) {
try {
RelativeLayout.LayoutParams parmss = new RelativeLayout.LayoutParams(five_width,five_height);
thumb_five.setLayoutParams(parmss);
AppUtility.getIconImage(thumb_five, list.get(position).getThumb());
id=list.get(position).getId();
id_one=list.get(position).getId_one();
} catch (Exception e) {
e.printStackTrace();
}
} else {
String thumburl=list.get(position).getThumb();
String thumburlone=list.get(position).getThumb_one();
if(thumburl.equals("null"))
{
}
else
{
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(orignal_width,orignal_height);
thumb.setLayoutParams(parms);
id=list.get(position).getId();
AppUtility.getIconImage(thumb, list.get(position).getThumb());
}
if(thumburlone.equals("null"))
{
}else {
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(orignal_width,orignal_height);
thumb_one.setLayoutParams(parms);
AppUtility.getIconImage(thumb_one, list.get(position).getThumb_one());
id_one =list.get(position).getId_one();
}
}
}
// inner class to hold a reference to each item of RecyclerView
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this casej
public View view;
public ViewHolder(View v) {
super(v);
view = v;
if (view.getId() == R.id.program_mainlayout) {
RelativeLayout layoutLeft = (RelativeLayout) view.findViewById(R.id.rl_programsview_left);
RelativeLayout layoutRight = (RelativeLayout) view.findViewById(R.id.rl_programsview_right);
layoutLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(list.size()!=0)
{
mOnClickEvent.onItemClickListener(getAdapterPosition(),list.get(getAdapterPosition()).getChannel(),list.get(getAdapterPosition()).getId(),list.get(getAdapterPosition()).getUrl(),list.get(getAdapterPosition()).getOffset());
}
}
});
layoutRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(list.size()!=0)
{
mOnClickEvent.onItemClickListener(getAdapterPosition(),list.get(getAdapterPosition()).getChannel_one(),list.get(getAdapterPosition()).getId_one(),list.get(getAdapterPosition()).getUrl_one(),list.get(getAdapterPosition()).getOffset_one());
}
else
{
}
}
});
}
else{
RelativeLayout layoutfifth=(RelativeLayout)view.findViewById(R.id.fifth_programe_m);
layoutfifth.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(list.size()!=0)
{
mOnClickEvent.onItemClickListener(getAdapterPosition(),list.get(getAdapterPosition()).getChannel(),list.get(getAdapterPosition()).getId(),list.get(getAdapterPosition()).getUrl(),list.get(getAdapterPosition()).getOffset());
}}
});
}
}
}
// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount() {
return list.size();
}
public interface OnClickListener {
void onItemClickListener(int position, String subPosition,String id,String url,String offset);
}
}
0 Comment(s)