工作多个布局显示在GridView中

时间:2017-11-10 07:38:33

标签: android xml android-layout android-gridview

我正在开发一款Android应用来展示仪表。有不同类型的仪表( gauge1.xml gauge2.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作为规范。

2 个答案:

答案 0 :(得分:0)

在适配器的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;
      }

    }

答案 1 :(得分:0)

尝试使用Recycler视图并将布局管理器更改为网格格式并制作适配器,如下所示:

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

公共类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);

    }
}

公共类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);

    }
}

}