我有两个片段,将使用recyclerview显示gridview 如果我使用acitivity,我的代码没有任何问题
但如果我使用片段,我得到的视图总是空白
我有两个片段,将使用recyclerview显示gridview 如果我使用acitivity,我的代码没有任何问题
但如果我使用片段,我得到的视图总是空白的 是否有人可以帮助我?
这是我的代码
GridAdapter.java
package dev.id.sample.grid;
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.MyViewHolder> {
private Context mContext;
private List<Grid> gridList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, count;
public ImageView thumbnail, overflow;
public MyViewHolder(View view) {
super(view);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
}
}
public GridAdapter(Context mContext, List<Grid> gridList) {
this.mContext = mContext;
this.gridList = gridList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.album_card, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
Grid grid = gridList.get(position);
// loading grid cover using Glide library
Glide.with(mContext).load(grid.getThumbnail()).into(holder.thumbnail);
holder.thumbnail.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
}
});
}
@Override
public int getItemCount() {
return gridList.size();
}
}
fragment.java
public class xxx extends Fragment {
private RecyclerView recyclerView;
private GridAdapter adapter;
private List<Grid> gridList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bank, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
gridList = new ArrayList<>();
adapter = new GridAdapter(getActivity(), gridList);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
prepareAlbums();
// Inflate the layout for this fragment
return view;
}
private void prepareAlbums() {
int[] covers = new int[]{
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher,
R.mipmap.ic_launcher
};
Grid
a = new Grid("cccc", 13, covers[0]);
gridList.add(a);
a = new Grid("cccc", 8, covers[1]);
gridList.add(a);
a = new Grid("cccc", 8, covers[2]);
gridList.add(a);
a = new Grid("cccc", 8, covers[3]);
gridList.add(a);
a = new Grid("cccc", 8, covers[4]);
gridList.add(a);
a = new Grid("cccc", 8, covers[5]);
gridList.add(a);
adapter.notifyDataSetChanged();
}
/**
* RecyclerView item decoration - give equal margin around grid item
*/
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration {
private int spanCount;
private int spacing;
private boolean includeEdge;
public GridSpacingItemDecoration(int spanCount, int spacing, boolean includeEdge) {
this.spanCount = spanCount;
this.spacing = spacing;
this.includeEdge = includeEdge;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int position = parent.getChildAdapterPosition(view); // item position
int column = position % spanCount; // item column
if (includeEdge) {
outRect.left = spacing - column * spacing / spanCount; // spacing - column * ((1f / spanCount) * spacing)
outRect.right = (column + 1) * spacing / spanCount; // (column + 1) * ((1f / spanCount) * spacing)
if (position < spanCount) { // top edge
outRect.top = spacing;
}
outRect.bottom = spacing; // item bottom
} else {
outRect.left = column * spacing / spanCount; // column * ((1f / spanCount) * spacing)
outRect.right = spacing - (column + 1) * spacing / spanCount; // spacing - (column + 1) * ((1f / spanCount) * spacing)
if (position >= spanCount) {
outRect.top = spacing; // item top
}
}
}
}
/**
* Converting dp to pixel
*/
private int dpToPx(int dp) {
Resources r = getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
}