我在为RecyclerView
设置标题时遇到问题。我在RecyclerView
适配器中使用了以下代码来设置标题视图,但它只显示了我的列表中的标题视图,而没有给list_item布局充气。
这是我的代码:
package com.example.samarthkejriwal.easyroads;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.samarthkejriwal.easyroads.Rv_adapter.MyViewHolder;
import java.util.ArrayList;
/**
* Created by samarthkejriwal on 10/08/17.
*/
public class Rv_adapter extends RecyclerView.Adapter<MyViewHolder> {
private ArrayList<PlacesResponse.CustomA> stLstStores;
private ArrayList<Modal> models;
private static final int TYPE_HEAD=0;
private static final int TYPE_LIST=1;
public Rv_adapter(ArrayList<PlacesResponse.CustomA> stores, ArrayList<Modal> storeModels) {
stLstStores = stores;
models = storeModels;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType==TYPE_LIST) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_listitem, parent, false);
return new MyViewHolder(itemView,viewType);
}
else if(viewType==TYPE_HEAD)
{
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_header, parent, false);
return new MyViewHolder(itemView,viewType);
}
return null;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position)
{
Log.i("adapter_posn",holder.getAdapterPosition()+"");
Log.i("view type",holder.view_type+"");
if(holder.view_type == TYPE_LIST) {
holder.res_name.setText(stLstStores.get(holder.getAdapterPosition()-1).name);
// holder.res_address.setText(models.get(holder.getAdapterPosition()).address);
// holder.res_phone.setText(models.get(holder.getAdapterPosition()).phone_no);
// holder.res_rating.setText(String.valueOf(models.get(holder.getAdapterPosition()).rating));
// holder.res_distance.setText(String.valueOf(models.get(holder.getAdapterPosition()).distance));
}
else if (holder.view_type == TYPE_HEAD)
{
// holder.current_location.setText(String.valueOf(models.get(holder.getAdapterPosition()).current_location));
}
}
@Override
public int getItemCount() {
Log.i("count: ",stLstStores.size()+"");
Log.i("count1",models.size()+"");
return stLstStores.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView res_name;
TextView res_rating;
TextView res_address;
TextView res_phone;
TextView res_distance;
TextView current_location;
int view_type;
public MyViewHolder(View itemView,int viewType) {
super(itemView);
if(viewType == TYPE_LIST) {
view_type=1;
this.res_name = (TextView) itemView.findViewById(R.id.name);
this.res_rating = (TextView) itemView.findViewById(R.id.rating);
this.res_address = (TextView) itemView.findViewById(R.id.address);
this.res_phone = (TextView) itemView.findViewById(R.id.phone);
this.res_distance = (TextView) itemView.findViewById(R.id.distance);
}
else if(viewType == TYPE_HEAD){
view_type = 0;
this.current_location = (TextView) itemView.findViewById(R.id.location_tv);
}
}
}
}
以下是RecyclerView
的屏幕截图:
在屏幕截图中,只有整个RecyclerView显示标题视图
答案 0 :(得分:1)
将此添加到方法
@Override
public int getItemViewType(int position) {
if(position==0){
return TYPE_HEAD;
} else {
return TYPE_LIST;
}
}
而不是函数
中使用的view_type