在 GridView 中显示工作多个布局

Working Multiple Layouts show in a GridView

我正在开发一个 Android 应用程序来显示仪表。有不同类型的仪表(gauge1.xmlgauge2.xml 等)。 我想要做的是使用 JSON 数组将不同类型的仪表 布局 xml 文件加载到主网格视图。

每个网格单元格根据 JSON 对象包含不同的布局。

我的JSON对象:

{"user1": [{"ui": "gauge", "id": "gauge1", "value": 69}, {"ui": 
    "gauge", "id": "gauge2", "value": 23}]}

Gauge1.xml

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

    <pl.pawelkleczkowski.customgauge.CustomGauge
        android:id="@+id/gauge1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:paddingBottom="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:paddingTop="20dp"
        app:gaugePointStartColor="@color/md_red_500"
        app:gaugePointEndColor="@color/md_red_500"
        app:gaugePointSize="6"
        app:gaugeStartAngle="135"
        app:gaugeStrokeCap="ROUND"
        app:gaugeStrokeColor="@color/md_grey_400"
        app:gaugeStrokeWidth="10dp"
        app:gaugeStartValue="0"
        app:gaugeEndValue="1000"
        app:gaugeSweepAngle="270" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/gauge1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="18dp"
        android:text="256"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="18dp"
        android:text="Temp"
        android:textSize="20dp"
        android:textStyle="bold" />


</RelativeLayout>

我该怎么做?

我正在使用 pkleczko's CustomGauge 作为仪表。

在适配器的 getView 方法中,根据需要扩充布局(gauge1.xml 或 gauge2.xml)。例如,在仪表 ID 上使用 if。

按照以下步骤操作:

  • 为 GridView 扩展基础适配器定义适配器
  • 在 getView 方法中检索方法提供的位置的项目
  • 根据检索到的项目,膨胀正确的布局
public class UsersAdapter extends BaseAdapter {

      private final Context mContext;
      private final User[] users;

      public UsersAdapter(Context context, User[] users) {
        this.mContext = context;
        this.users = users;
      }

      @Override
      public int getCount() {
        return users.length;
      }

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        User curUser = users[position];
        LayoutInflater inflater = LayoutInflater.from(context);
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (//curUser needs gauge1 layout//){
                convertView = inflater.inflate(R.layout.gauge1, parent, false);
                // do what you need with the layout gauge 1
            }else{
                convertView = inflater.inflate(R.layout.gauge2, parent, false);
                // do what you need with the layout gauge 1
            }   
        }
        return convertView;
      }

    }

尝试使用 Recycler 视图并将布局管理器更改为网格格式并像这样制作适配器:

注意:请不要深入我的编码我只是给你构建的方法。

public class SubCategoryAdapter 扩展 RecyclerView.Adapter {

private final int VIEW_GRID = 1;
private final String[] listdata;
private Context context;
Gson gson;


public SubCategoryAdapter(Context context, String[] listdata) {
    this.context = context;
    this.listdata = listdata;
    gson = new Gson();
}


@Override
public int getItemViewType(int position) {

    if (isPositionItem(position)){
        return 1;
    }
    else {
        return 2;
    }
}

private boolean isPositionItem(int position) {

// 根据您的回复在此处定义您的条件

    if(position==0) {
        return true;
    }else{
        return false;
    }
}

@Override
public int getItemCount() {
    return listdata.length;
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

      if (viewType == 1) {
        View v =  LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_bb_verticallist, parent, false);
        return new ViewHolder1(v);
    } else if (viewType == 2){
        View v =  LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_root_bb_gridlist, parent, false);
        return new ViewHolder2(v);
    }
    return null;


}


public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

    if (holder instanceof ViewHolder) {
        if(position <= listdata.length) {

            switch (holder.getItemViewType()){

                case 1:
                    // your viewholder1
                    break;

                case 2:
                 // your view holder2
                    break;

            }

        }

    }

}

public class ViewHolder extends RecyclerView.ViewHolder {

    TextView title;
    RecyclerView recycler_childView;

    public ViewHolder(View itemView) {
        super(itemView);

        title = (TextView)itemView.findViewById(R.id.title);
        recycler_childView = (RecyclerView) itemView.findViewById(R.id.list);

    }
}

public class ViewHolder2 扩展 RecyclerView.ViewHolder {

    TextView title;
    RecyclerView recycler_childView;

    public ViewHolder(View itemView) {
        super(itemView);

        title = (TextView)itemView.findViewById(R.id.title);
        recycler_childView = (RecyclerView) itemView.findViewById(R.id.list);

    }
}

}