我正在开发一个Android应用程序,我使用RecyclerView来显示项目列表。我从服务器获取列表为json.So我的问题是在这个列表中我得到另一个列表作为item.That是如果我的主要arraylist包含标题和材料,材料是另一个arraylist。所以你能建议一个解决方案,在recyclerview中显示一个列表。
以下代码是我的适配器
public class CurriculumAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context mContext;
private ArrayList<Curriculum> mArrayListCurriculum;
public CurriculumAdapter(Context mContext, ArrayList<Curriculum> mArrayListCurriculum) {
this.mContext = mContext;
this.mArrayListCurriculum = mArrayListCurriculum;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_key_features, parent,false);
return new KeyFeatureViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (holder instanceof KeyFeatureViewHolder) {
((KeyFeatureViewHolder) holder).mTextViewFeatureTitle.setText(mArrayListCurriculum.get(position).getTitle());
}
}
@Override
public int getItemCount() {
return mArrayListCurriculum == null ? 0 : mArrayListCurriculum.size();
}
public static class KeyFeatureViewHolder extends RecyclerView.ViewHolder {
public TextView mTextViewFeatureTitle;
public KeyFeatureViewHolder(View itemView) {
super(itemView);
mTextViewFeatureTitle = (TextView) itemView.findViewById(R.id.txtFeature);
}
}
}
下面的代码是带有虚拟arraylist数据的片段
public class CourseCurriculumFragment extends Fragment {
private FragmentInterface mFragmentInterface;
private ArrayList<Curriculum> mArrayListCurriculum;
private ArrayList<Material> mArrayListMaterial;
private RecyclerView mRecyclerViewCurriculum;
private LinearLayoutManager mLinearLayoutManager;
private CurriculumAdapter mCurriculumAdapter;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_course_curriculum, container, false);
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
initView(view);
}
private void initView(View view) {
mArrayListMaterial = new ArrayList<>();
mArrayListCurriculum = new ArrayList<>();
populateMaterials();
populateKeyFeatures();
mRecyclerViewCurriculum = (RecyclerView) view.findViewById(R.id.recyclerViewCurriculum);
mCurriculumAdapter = new CurriculumAdapter(getActivity(), mArrayListCurriculum);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerViewCurriculum.setLayoutManager(mLinearLayoutManager);
mRecyclerViewCurriculum.setAdapter(mCurriculumAdapter);
mRecyclerViewCurriculum.setItemAnimator(new DefaultItemAnimator());
}
private void populateMaterials() {
mArrayListMaterial.add(new Material("12:00","pdf","","Sample Text","0"));
mArrayListMaterial.add(new Material("12:00","pdf","","Sample Text","0"));
}
private void populateKeyFeatures() {
mArrayListCurriculum.add(new Curriculum("UNIT 1",mArrayListMaterial));
mArrayListCurriculum.add(new Curriculum("UNIT 2",mArrayListMaterial));
mArrayListCurriculum.add(new Curriculum("UNIT 3",mArrayListMaterial));
}
}
答案 0 :(得分:1)
持有者中的bind
方法是将数据传递给它的好方法。
在您的情况下,此bind
方法应将Curriculum
和Material
对象作为参数。
在适配器的onBindViewHolder
方法中,您应该调用此bind
方法,而不是访问持有者的变量。
在you KeyFeatureViewHolder
类中的方法实现中,您应该使用这些传递的参数并将它们显示在相应的UI元素中。
最后,要将Material
对象数据转换为适配器,请将ArrayList<Material>
添加为构造函数参数,就像使用Curriculum
一样。
答案 1 :(得分:1)
使用标题RecyclerView
作为标题title
作为标题,materials
作为标题的项目。请看this example。
答案 2 :(得分:1)
您需要为自己设计自定义列表。例如,拿一个像这样的对象。
public class ListItem {
public curriculumName = null;
public materialName = null;
}
现在在解析JSON字符串后填充此列表。获取您的第一个Curriculum
并填充此对象
private ArrayList<ListItem> mListItemArray = new ArrayList<ListItem> ();
for(curriculum : mArrayListCurriculum) {
ListItem mListItemHead = new ListItem();
mListItemHead.curriculumName = curriculum.getName();
// Set the header here
mListItemArray.add(mListItemHead);
for(material : curriculum.getMaterials()){
ListItem mListItem = new ListItem();
mListItem.materialName = material.getName();
// Add materials here
mListItemArray.add(mListItem);
}
}
现在,您有一个包含标题和材料的列表。当materialName
中的mListItemArray
为空时,它会识别出这是一个标头,反之亦然。
现在的诀窍是修改RecyclerView
的适配器,以便将正确的视图绑定到列表中的项目。
您可以从this answer找到有关如何实现此期望行为的指示。
基本上,我们的想法是修改您的getItemViewType
,以便在onBindViewHolder
中传递正确的观点。您的getItemViewType
可能如下所示。
@Override
public int getItemViewType(int position) {
if (mListItemArray.get(position).curriculumName != null) {
// This is where we'll add header.
return HEADER_VIEW;
}
return super.getItemViewType(position);
}