2017-11-10 1 views
3

게이지를 표시하기 위해 Android 앱을 개발 중입니다. 게이지의 종류는 다양합니다 (gauge1.xml, 게이지 2.xml). 내가하고 싶은 게 레이아웃 xml 파일을 JSON 배열을 사용하여 주 눈금보기에로드하십시오.GridView에 다중 레이아웃 작업 표시

각 그리드 셀은 에 따라 다른 레이아웃을 포함합니다. 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을 사용하고 있습니다.

답변

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; 
     } 

    } 
0

시도는 휴지통보기 및 변경 layo를 사용하는 ut 관리자를 그리드 형식으로 만들고 다음과 같은 어댑터를 만드십시오.

참고 : 코딩 방법을 자세히 설명하지 않으면 빌드 방법을 제공하지 마십시오.

공용 클래스 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가 {

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); 

    } 
} 

RecyclerView.ViewHolder를 확장

공용 클래스 }