简体   繁体   English

如何在回收站视图中使用列表视图?

[英]How to use listview in recyclerview?

My data format is like this我的数据格式是这样的
{"foo1":"bar1","foo2":["bar2","bar3","bar4"]} ,{"foo1":"bar5","foo2":["bar6","bar7","bar8"]}

I'am using recyclerview for root object and want to use foo3 as listview in recyclerview.我正在使用 recyclerview 作为根对象,并希望在 recyclerview 中使用 foo3 作为列表视图。 Is it possible and how?有可能吗?

Example of recyclerview list item layout 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 is like this 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);
}}

Also ViewAdapter is 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);
}
..
}

First, you'll need to convert that JSON into a java object.首先,您需要将该 JSON 转换为 java 对象。 You could use any library for this (GSON, Jackson, etc.)您可以为此使用任何库(GSON、Jackson 等)

Your class will probably look like this你的班级可能看起来像这样

public class TheJSON {
    public String foo1;
    public ArrayList<String> foo2;
}

Once you get the JSON represented in Java objects, then use multiple view types in your RecyclerView adapter一旦你在 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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM