2015-10-08 4 views
0

Я новичок в android, и я пытаюсь добиться результата, показанного на прилагаемом скриншоте. Я использую RecyclerView, чтобы получить тот же результат и управлять позиционированием элементов, которые я использую GridLayoutManager. Моя проблема в том, что я могу сделать сетку, но я не знаю, как добавить текст между двумя сетками в recyclerview, как текст «What's Hot» на скриншоте.Обычный текст заголовка внутри recycler view android

enter image description here

Мой код:

<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; 
} 

ответ

1

Вы можете добавить другой тип элемента (скажем, HEADER_TYPE) в ресайклера View. Сейчас все ваши объекты в ресайклере имеют одинаковый тип.

Вы можете переопределить функцию public int getItemViewType(int position), чтобы вернуть соответствующий тип в зависимости от позиции, а в onCreateViewHolder вы можете раздувать другой макет для предметов HEADER_TYPE и обычных предметов.

Вот пример: Is there an addHeaderView equivalent for RecyclerView?

+0

Спасибо за ответ. У меня есть много текста, который должен находиться между сетками, а также они не могут быть интерактивными. Могу ли я использовать для этого другой HEADER_TYPE? – Kaif

+0

Также я могу добавить указатель точечной страницы/ViewPagerIndicator внутри просмотра recycler? – Kaif

Смежные вопросы