Hello Guys我有使用RecycleView添加Gridlayout的问题,但我不能将该行与Column对齐。
请您帮我理解如何按照我的代码对齐2列的行。
Tab1.class
private List<Consultorio> consultorioList;
private ClinicaAdapter adapter;
protected RecyclerView mRecyclerView;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab1, container, false);
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.itens_fragment);
GridLayoutManager manager = new GridLayoutManager(v.getContext(), 3);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return (3 - position % 3);
}
});
recyclerView.setLayoutManager(manager);
/*LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);*/
List<Consultorio> list = new ArrayList<Consultorio>();
Consultorio c0 = new Consultorio(R.mipmap.ic_launcher);
Consultorio c1 = new Consultorio(R.mipmap.ic_launcher);
Consultorio c2 = new Consultorio(R.mipmap.ic_launcher);
Consultorio c3 = new Consultorio(R.mipmap.ic_launcher);
Consultorio c4 = new Consultorio(R.mipmap.ic_launcher);
Consultorio c5 = new Consultorio(R.mipmap.ic_launcher);
Consultorio c6 = new Consultorio(R.mipmap.ic_launcher);
Consultorio c7 = new Consultorio(R.mipmap.ic_launcher);
list.add(c0);
list.add(c1);
list.add(c2);
list.add(c3);
list.add(c4);
list.add(c5);
list.add(c6);
list.add(c7);
adapter = new ClinicaAdapter(list);
recyclerView.setAdapter(adapter);
return v;
}
}
ClinicaAdapter.class
public class ClinicaAdapter extends RecyclerView.Adapter<ClinicaAdapter.ViewHolder> {
List<Consultorio> consultorioList;
Boolean isHomeList = false;
private List<Consultorio> mEspecialistas;
public ClinicaAdapter(List<Consultorio> data) {
mEspecialistas = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.clinica_medica_item, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//Consultorio consultorio = consultorioList.get(position);
holder.imageViewPhoto.setImageResource(R.mipmap.ic_launcher);
}
@Override
public int getItemCount() {
return mEspecialistas.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageViewPhoto;
public ViewHolder(View itemView) {
super(itemView);
imageViewPhoto = (ImageView) itemView.findViewById(R.id.clinc_img);
}
}
}
答案 0 :(得分:0)
这是我的问题的答案。
在Tab1.class中我输入了以下代码:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab1, container, false);
RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.itens_fragment);
GridLayoutManager manager = new GridLayoutManager(v.getContext(), 2, GridLayoutManager.VERTICAL, false);
manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return (position % 3 == 0 ? 2 : 1);
}
});
recyclerView.setLayoutManager(manager);
参考:http://pt.slideshare.net/devunwired/mastering-recyclerview-layouts