我的数据格式如下
{"foo1":"bar1","foo2":["bar2","bar3","bar4"]}
,{"foo1":"bar5","foo2":["bar6","bar7","bar8"]}
我正在使用recyclerview作为root对象,并希望在recyclelerview中使用foo3作为listview。它有可能吗?如何?
recyclerview列表项目布局示例
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvFoo1Value"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/lvFoo2Values"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ViewHolder就像这样
public class FooViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView tvFoo1Value;
public final ListView lvFoo2Values;
public ChallengeViewHolder(View view) {
super(view);
mView = view;
this.tvFoo1Value = (TextView) view.findViewById(R.id.tvFoo1Value);
this.lvFoo2Values = (ListView) view.findViewById(R.id.lvFoo2Values);
}}
ViewAdapter也是
public class FooRecyclerViewAdapter extends RecyclerView.Adapter<FooViewHolder> {
..
private View view;
@Override
public FooViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context ctx = parent.getContext();
view = LayoutInflater.from(ctx)
.inflate(R.layout.foo_list_item, parent, false);
return new FooViewHolder(view);
}
@Override
public void onBindViewHolder(FooViewHolder holder, int position) {
String[] foo2Arr = //comes from service
Context ctx = view.getContext(); //?? possible error here i think
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(ctx,android.R.layout.simple_list_item_1,foo2Arr);
holder.lvFoo2Values.setAdapter(listAdapter);
}
..
}
答案 0 :(得分:1)
首先,您需要将该JSON转换为java对象。您可以使用任何库(GSON,Jackson等)
你的课可能看起来像这样
public class TheJSON {
public String foo1;
public ArrayList<String> foo2;
}
获得Java对象中表示的JSON后,在RecyclerView
适配器中使用多种视图类型
public class TheAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<TheJSON> json_collection;
private ArrayList<String> the_strings;
private ArrayList<int> the_view_types;
public TheAdapter(ArrayList<TheJSON> json_collection) {
this.json_collection = json_collection;
for (int i = 0; i < json_collection; i++) {
the_string.add(json_collection.get(i).foo1);
the_view_types.add(1);
for (int j = 0; j < json_collection.get(i).foo2.size(); j++) {
the_string.add(json_collection.get(i).foo2.get(j));
the_view_types.add(2);
}
}
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 1:
return new Foo1ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.foo1_layout, parent, false));
case 2:
return new Foo2ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.foo2_layout, parent, false));
default:
return null;
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (the_view_types.get(position)) {
case 1:
Foo1ViewHolder foo1ViewHolder = (Foo1ViewHolder) holder;
foo1ViewHolder.setText(the_strings.get(position));
break;
case 2:
Foo2ViewHolder foo2ViewHolder = (Foo2ViewHolder) holder;
foo2ViewHolder.setText(the_strings.get(position));
break;
}
}
@Override
public int getItemCount() {
return the_strings.size();
}
@Override
public int getItemViewType(int position) {
return the_view_types.get(position);
}
public class Foo1ViewHolder extends RecyclerView.ViewHolder {
private TextView tvFoo1Value;
public Foo1ViewHolder(View itemView) {
super(itemView);
tvFoo1Value = (TextView) itemView.findViewById(R.id.tvFoo1Value);
}
public setText(String text) {
tvFoo1Value.setText(text);
}
}
public class Foo2ViewHolder extends RecyclerView.ViewHolder {
private TextView tvFoo2Value;
public Foo2ViewHolder(View itemView) {
super(itemView);
tvFoo2Value = (TextView) itemView.findViewById(R.id.tvFoo2Value);
}
public setText(String text) {
tvFoo2Value.setText(text);
}
}
}