0

I am new to android and i am trying to achieve the result shown in attached screenshot. I am using a RecyclerView to get the same result and to manage the positioning of items i am using GridLayoutManager. My problem is i am able to make the grid but i do not have any idea how to add text between two grids in recyclerview like the text "What's Hot" in the screenshot.

enter image description here

My code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.RecyclerView android:id = "@+id/my_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/imgsrc"
    android:src="@drawable/close" />


</RelativeLayout>

public class CategoryRecyclerAdapter extends RecyclerView.Adapter<CategoryRecyclerAdapter.MyViewHolder>{
List<CatetoryListModel> data = Collections.emptyList();
LayoutInflater inflater;
Context context;

public CategoryRecyclerAdapter(Context context,List<CatetoryListModel> data){
    inflater = LayoutInflater.from(context);
    this.data = data;
    this.context = context;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
   View view = inflater.inflate(R.layout.recycler_custom_row,parent,false);
    MyViewHolder myViewHolder = new MyViewHolder(view,context);
    return myViewHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

   // StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
  //  layoutParams.setFullSpan(true);

    CatetoryListModel current = data.get(position);
    //holder.title.setText(current.getCategoryName());
   // holder.desp.setText(current.getDescription());
    holder.icon.setImageResource(current.getImgSrc());


}

@Override
public int getItemCount() {
    return data.size();
}

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

    TextView title;
    TextView desp;
    ImageView icon;
    Context cntxt;
    public MyViewHolder(View itemView,Context c) {
        super(itemView);
        cntxt = c;
        itemView.setClickable(true);
        itemView.setOnClickListener(this);
       // title = (TextView)itemView.findViewById(R.id.category);
      //  desp = (TextView)itemView.findViewById(R.id.description);
        icon = (ImageView)itemView.findViewById(R.id.imgsrc);


    }

    @Override
    public void onClick(View v) {
        Toast.makeText(cntxt,"Hello",Toast.LENGTH_LONG).show();
    }
}

}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.home_fragment,container,false);
    recycler = (RecyclerView)view.findViewById(R.id.my_recycler_view);
    adapter = new CategoryRecyclerAdapter(getActivity(),getData());
    gridView = new GridLayoutManager(getActivity(),2);
 //   recycler.setHasFixedSize(false);
   // recycler.addItemDecoration(new Divider);
    gridView.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

        @Override
        public int getSpanSize(int position) {
            if(position == 0)
                return 2;

            return 1;
        }
    });

 //   gridView.setSpanCount(2);
    recycler.setLayoutManager(gridView);
    recycler.setAdapter(adapter);

    return view;
}

public static List<CatetoryListModel> getData(){
List<CatetoryListModel> data =new ArrayList<>();
 int[] icons = {R.drawable.banner,R.drawable.sale,R.drawable.ethnic,R.drawable.kurti,R.drawable.sarees};
    String[] titles = {"CLOSE","CLOSE","HELP","PLAY","SETTING"};
    String[] desp = {"CLOSE","CLOSE CLOSE","HELP CLOSE","PLAY CLOSE","SETTING CLOSE"};
    for(int i=0;i<icons.length;i++){

        CatetoryListModel singleItem = new CatetoryListModel(titles[i],desp[i],icons[i]);
        data.add(singleItem);
    }

    return data;
}
Kaif
  • 131
  • 1
  • 12

1 Answers1

2

You can add another type of item (say HEADER_TYPE) in the recycler View. Right now all your items in recycler view are of same type.

You can override public int getItemViewType(int position) function to return the appropriate type based on the position and in onCreateViewHolder you can inflate a different layout for HEADER_TYPE items and normal items.

Here is an example: Is there an addHeaderView equivalent for RecyclerView?

Community
  • 1
  • 1
pgiitu
  • 1,671
  • 1
  • 15
  • 23
  • Thanks for the answer. I have lots of text which should be in between the grids and also these are non clickable. Can i use the different HEADER_TYPE for this? – Kaif Oct 11 '15 at 16:01
  • Also can i add a dot pageindicator/ViewPagerIndicator inside the recycler view? – Kaif Oct 11 '15 at 16:55