创建一个自定义的RecyclerView:头部和项目列表。

问题描述 投票:-1回答:1

你好,我想创建这个列表,如下图所示,尝试使用RecyclerView,但它没有工作!

https:/i.stack.imgur.comA3CaB.png。

我的api,我用的是retrofit。

[
  {
    "id": "1",
    "name": "TV",
    "list": [
      {
        "idp": "1",
        "namep": "TV 43"
      },
      {
        "idp": "2",
        "namep": "TV 32"
      }
    ]
  },
  {
    "id": "2",
    "name": "Couch",
    "list": [
      {
        "idp": "3",
        "namep": "Couch for 3 people"
      },
      {
        "idp": "4",
        "namep": "Couch for 2 people"
      }
    ]
  }
]
java android android-recyclerview recycler-adapter
1个回答
0
投票

主类

public class Main {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("list")
    @Expose
    private ArrayList<List> list = null;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<List> getList() {
        return list;
    }

    public void setList(ArrayList<List> list) {
        this.list = list;
    }
}

List.class

public class List {

    @SerializedName("idp")
    @Expose
    private String idp;
    @SerializedName("namep")
    @Expose
    private String namep;

    public String getIdp() {
        return idp;
    }

    public void setIdp(String idp) {
        this.idp = idp;
    }

    public String getNamep() {
        return namep;
    }

    public void setNamep(String namep) {
        this.namep = namep;
    }

}

header_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="#673AB7"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

列表_布局.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/listRV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/list_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textColor="#000000"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

主适配器

public class MainAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;
    ArrayList<Main> mainArrayList;

    public MainAdapter(ArrayList<Main> mainArrayList) {
        this.mainArrayList = mainArrayList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header_layout, parent, false);
            return new VHItem(v);
        } else if (viewType == TYPE_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_layout, parent, false);
            return new VHHeader(v);
        }

        throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof VHHeader) {
            Main main = getItem(position / 2);
            VHHeader vhHeader = (VHHeader) holder;
            vhHeader.header.setText(main.getName());
        }else if(holder instanceof VHItem) {
            Main main = getItem(position / 2);
            VHItem vhItem = (VHItem) holder;
            ListAdapter listAdapter = new ListAdapter(main.getList());
            vhItem.listRV.setLayoutManager(new LinearLayoutManager(holder.itemView.getContext()));
            vhItem.listRV.setAdapter(listAdapter);
        }
    }

    @Override
    public int getItemCount() {
        return mainArrayList.size() * 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (isPositionHeader(position))
            return TYPE_HEADER;

        return TYPE_ITEM;
    }

    private boolean isPositionHeader(int position) {
        return position%2 == 0;
    }

    private Main getItem(int position) {
        return mainArrayList.get(position);
    }

    class VHHeader extends RecyclerView.ViewHolder {
        TextView header;

        public VHHeader(View itemView) {
            super(itemView);
            header = itemView.findViewById(R.id.header);
        }
    }

    class VHItem extends RecyclerView.ViewHolder {
        RecyclerView listRV;

        public VHItem(View itemView) {
            super(itemView);
            listRV = itemView.findViewById(R.id.listRV);
        }
    }
}

列表适配器

public class ListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    ArrayList<List> listArrayList;

    public ListAdapter(ArrayList<List> listArrayList) {

        this.listArrayList = listArrayList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
           View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
        return new VHItem(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        VHItem vhItem = (VHItem) holder;
        vhItem.list_item.setText(listArrayList.get(position).getIdp() + " - " + listArrayList.get(position).getNamep());
    }

    @Override
    public int getItemCount() {
        return listArrayList.size();
    }


    class VHItem extends RecyclerView.ViewHolder {
        TextView list_item;

        public VHItem(View itemView) {
            super(itemView);
            list_item = itemView.findViewById(R.id.list_item);
        }
    }
}

调用MainAdapter。

MainAdapter mainAdapter = new MainAdapter(products);
recyclerPro.setHasFixedSize( true ); 
recyclerPro.setLayoutManager(new LinearLayoutManager(this)); 
recyclerPro.setAdapter(mainAdapter); 
© www.soinside.com 2019 - 2024. All rights reserved.