【问题标题】:Plain header text inside recycler view android回收站视图android中的纯标题文本
【发布时间】:2015-10-09 03:12:30
【问题描述】:

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

我的代码:

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

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

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

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

    这里是一个例子:Is there an addHeaderView equivalent for RecyclerView?

    【讨论】:

    • 感谢您的回答。我有很多文本应该在网格之间,而且这些都是不可点击的。我可以为此使用不同的 HEADER_TYPE 吗?
    • 我还可以在回收站视图中添加一个点 pageindicator/ViewPagerIndicator 吗?
    猜你喜欢
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多