我需要在自定义视图中创建RecyclerView
。按照我的代码:
public class CitySinglePageGridView extends LinearLayout {
Context context ;
View mView ;
RecyclerView recyclerView ;
RecyclerView.LayoutManager layoutManager ;
RecyclerView.Adapter adapter;
public CitySinglePageGridView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context ;
LayoutInflater inflater;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(R.layout.module_city_single_page_gridview,this,true);
recyclerView = (RecyclerView) mView.findViewById(R.id.cardList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
adapter = new CityCardviewAdapter();
recyclerView.setAdapter(adapter);
}
}
问题是当我运行应用程序时,在自定义视图中没有显示任何内容,但是当我在Fragment
或Activity
中使用此RecyclerView时,一切正常。
关注我的自定义适配器:
public class CityCardviewAdapter extends RecyclerView.Adapter<CityCardviewAdapter.ViewHolder> {
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_card_item_city, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 5;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
protected TextView title;
protected TextView desc ;
protected TextView distance ;
protected TextView likeCount ;
protected ImageView thumbnail ;
protected ImageView like ;
protected ImageView Distance ;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.title);
desc = (TextView) itemView.findViewById(R.id.desc);
distance = (TextView) itemView.findViewById(R.id.distance);
likeCount = (TextView) itemView.findViewById(R.id.likeCount);
thumbnail = (ImageView) itemView.findViewById(R.id.thumbnail);
like = (ImageView) itemView.findViewById(R.id.like);
Distance = (ImageView) itemView.findViewById(R.id.nav);
}
}
}
我将getItemCount
设置为5来测试应用程序。