UltimateRecyclerView 添加头部view无效?

这是fragmetn
@EFragment(R.layout.fragment_home)
public class HomeFragment extends BaseFragment {
protected LoadingDialog loading;
@ViewById
UltimateRecyclerView home_ultimateRecyclerView;
protected GroupGoods groupGoodses;
protected HomeRecyclerViewAdapter homeRecyclerViewAdapter ;
View headerView;
protected int [] groupIco=new int[]{R.mipmap.shape, R.mipmap.previewflaticons, R.mipmap.office_supplies,
R.mipmap.pneumatic_hydraulic, R.mipmap.mechanics, R.mipmap.protect,
R.mipmap.industrial_contro, R.mipmap.light};
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public HomeFragment() {
}
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
/**

  • 请求首页商品 */ public void getDataGroupGoods(){

VolleyUtils .getInstance(getActivity()).doStringRequest(Url.IndexFloors, new Response.Listener() {
@Override
public void onResponse(String response) {
groupGoodses=GsonUtils.GsonToBean(response,GroupGoods.class);
homeRecyclerViewAdapter.setData(groupGoodses);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
headerView=LayoutInflater.from(getActivity()).inflate(R.layout.header_view,null);
return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
getDataGroupGoods();
}
@AfterViews
public void initUltimaterecyclerview() {
homeRecyclerViewAdapter = new HomeRecyclerViewAdapter(getContext(), groupGoodses, groupIco);
home_ultimateRecyclerView.setHasFixedSize(false);
//设置布局管理器
home_ultimateRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
home_ultimateRecyclerView.setAdapter(homeRecyclerViewAdapter);
//设置点击事件
homeRecyclerViewAdapter.setOnclickListeners(new HomeRecyclerViewAdapter.OnclickListeners() {
@Override
public void setOnClick(String id, int type) {
switch (type) {
case HomeRecyclerViewAdapter.GOODS_TYPE:
Toast.makeText(getContext(), "商品" + id, Toast.LENGTH_LONG).show();
break;
case HomeRecyclerViewAdapter.GROUP_TYPE:
Toast.makeText(getContext(), "组" + id, Toast.LENGTH_LONG).show();
break;
}
}
});
home_ultimateRecyclerView.setParallaxHeader(headerView);

}
}

这是适配器adapter
/**

  • Created by arvin on 2016/11/18. */ public class HomeRecyclerViewAdapter extends UltimateViewAdapter { public static final int GROUP_TYPE=0; public static final int GOODS_TYPE=1; private Context context ; //首页分类含商品 private GroupGoods groupGoods; //分类图标 private int [] groupIco ; private OnclickListeners onclickListeners; public HomeRecyclerViewAdapter( Context context, GroupGoods groupGoods,int [] groupIco ) { this.context=context; this.groupGoods = groupGoods; this.groupIco=groupIco; } public void setData(GroupGoods groupGoods) { this.groupGoods = groupGoods; this.notifyDataSetChanged(); }

@Override
public HomeRecyclerViewHolder newFooterHolder(View view) {
return new HomeRecyclerViewHolder(view);
}

@Override
public HomeRecyclerViewHolder newHeaderHolder(View view) {
return new HomeRecyclerViewHolder(view);
}
@Override
public HomeRecyclerViewHolder onCreateViewHolder(ViewGroup parent)
{
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.home_recyclerview_item,parent,false);
return new HomeRecyclerViewHolder(view);
}
@Override
public int getAdapterItemCount() {
if(groupGoods==null)
{
return 0;
}else
{return groupGoods.getData().size();
}
}
@Override
public long generateHeaderId(int position) {
return 0;
}
/***

  • 绑定数据
  • @param holder
  • @param position */

@Override
public void onBindViewHolder(HomeRecyclerViewHolder holder, int position) {
// if (position < getItemCount() && (customHeaderView != null ? position <= groupGoods.getData().size() : position < groupGoods.getData().size()) && (customHeaderView != null ? position > 0 : true)) {
// position -= customHeaderView==null?0:1;
final GroupGoods.Group group = groupGoods.getData().get(position);
//设置首页分类图标
holder.group_name.setText(group.getName());
//设置分类图标
Drawable drawableleft =context.getResources().getDrawable(groupIco[position]);
/// 这一步必须要做,否则不会显示.
drawableleft.setBounds(0, 0, drawableleft.getMinimumWidth(), drawableleft.getMinimumHeight());
holder.group_name.setCompoundDrawables(drawableleft, null, null, null);
holder.goodsName1.setText(group.getGoods().get(0).getCaption());
holder.goodsName2.setText(group.getGoods().get(1).getCaption());
GlideImgManager.loadImage(context,group.getGoods().get(0).getPic(),holder.goodsImageView1);
GlideImgManager.loadImage(context,group.getGoods().get(1).getPic(),holder.goodsImageView2);
holder.price1.setText(group.getGoods().get(0).getPrice());
holder.price2.setText(group.getGoods().get(1).getPrice());
if(onclickListeners!=null)
{
holder.group_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onclickListeners.setOnClick(group.getId(),GROUP_TYPE);
}
});

holder.goods_name1_RelativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onclickListeners.setOnClick(group.getGoods().get(0).getId()+"",GOODS_TYPE);
}
});
holder.goods_name2_RelativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onclickListeners.setOnClick(group.getGoods().get(1).getId()+"",GOODS_TYPE);
}
});
}
}
// }
@Override
public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_view,parent,false);
return new HomeRecyclerViewHolder(view);
}

@Override
public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

if(customHeaderView!=null){
position-=1;
}
}

public void setOnclickListeners(OnclickListeners onclickListeners)
{
this.onclickListeners=onclickListeners;
}

class HomeRecyclerViewHolder extends UltimateRecyclerviewViewHolder
{
TextView price2;........
TextView goodsName1;

public HomeRecyclerViewHolder(View itemView) {
super(itemView);
price2= (TextView) itemView.findViewById(R.id.price2);
}
}

public interface OnclickListeners
{
void setOnClick(String id,int type);
}

}

大神们来帮我看看!

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐