回收站视图内的普通标题文本android

时间:2015-10-08 19:03:51

标签: android android-recyclerview

我是Android的新手,我正在尝试实现附加屏幕截图中显示的结果。我使用RecyclerView获得相同的结果并管理我正在使用的项目GridLayoutManager的定位。我的问题是我能够制作网格,但我不知道如何在recyclerview中的两个网格之间添加文本,如截图中的文字“What's Hot”。

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 个答案:

答案 0 :(得分:2)

您可以在回收站视图中添加其他类型的项目(例如HEADER_TYPE)。现在,您在回收商视图中的所有商品都属于同一类型。

您可以覆盖public int getItemViewType(int position)函数以根据位置返回相应的类型,并在onCreateViewHolder中为HEADER_TYPE项目和普通项目填充不同的布局。

以下是一个示例:Is there an addHeaderView equivalent for RecyclerView?