Android菜鸟,仅实现了想要的效果,如有其它方案,欢迎分享,感谢
其中:listView分为红色框内的两种子布局(开始和未开始状态);绿色框为GridView,分为枚红色框中的几种状态
代码:
page1_demo.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"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/gray_white"
tools:context=".patrol.gtDemo.activity.pages.Page1Activity">
<ListView
android:id="@+id/listTask"
android:divider="@null"
android:dividerHeight="1.0dip"
android:scrollbars="none"
android:listSelector="#000000"
android:cacheColorHint="@android:color/transparent"
android:fadingEdge="none"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dip"
android:layout_marginBottom="14dip"
/>
</RelativeLayout>
page1Activity.java
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import com.ab.activity.AbActivity;
import com.ab.view.titlebar.AbTitleBar;
import com.andbase.R;
import com.andbase.global.MyApplication;
import com.andbase.patrol.gtDemo.adapter.pagesAdapter.Page1ListViewAdapter;
import com.andbase.patrol.gtDemo.model.Page1FormDemo;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lmx on 2018/1/30.
*/
public class Page1Activity extends AbActivity {
private MyApplication application;
//ListView加载不同布局
private ListView listview;
public List<Page1FormDemo> listInfo = null;
private Page1Activity context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setAbContentView(R.layout.page1_demo);
AbTitleBar mAbTitleBar = this.getTitleBar();
mAbTitleBar.setTitleText("page1");
mAbTitleBar.setLogo(R.drawable.button_selector_back);
mAbTitleBar.setTitleBarBackground(R.drawable.top_bg);
mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
mAbTitleBar.setLogoLine(R.drawable.line);
this.setTitleBarOverlay(true);
//加载不同布局
this.listview = (ListView) findViewById(R.id.listTask);
context = this;
application = (MyApplication)abApplication;
//加载
loadListView();
}
/**
* 加载listView数据
*/
public void loadListView(){
try {
listInfo = new ArrayList<Page1FormDemo>();
Page1FormDemo form1 = new Page1FormDemo();
form1.setPropertyType(0);//数值
form1.setTaskName("任务名称一");
form1.setTaskTime("2018-02-10 09:00~12:00");
form1.setTaskState("状态");
listInfo.add(form1);
Page1FormDemo form2 = new Page1FormDemo();
form2.setPropertyType(1);
form2.setTaskName("任务名称二");
form2.setTaskTime("2018-02-10 09:00~12:00");
form2.setTaskState("状态");
listInfo.add(form2);
}catch(Exception ex)
{
Log.e("JSON Error: ", ex.toString());
}
ListView listView = (ListView) findViewById(R.id.listTask);
Page1ListViewAdapter page1ListViewAdapter = new Page1ListViewAdapter(context,listInfo);
listView.setAdapter(page1ListViewAdapter);
}
}
Page1ListViewAdapter.java
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import com.andbase.R;
import com.andbase.patrol.gtDemo.activity.pages.Page1Activity;
import com.andbase.patrol.gtDemo.model.Page1FormDemo;
import com.andbase.patrol.gtDemo.model.PointStateDemo;
import java.util.ArrayList;
import java.util.List;
/**
* Created by lmx on 2018/2/26.
*/
public class Page1ListViewAdapter extends BaseAdapter {
private Page1Activity mContext;
private List<Page1FormDemo> lists;
private LayoutInflater layinf;
//GridView加载不同布局
public List<PointStateDemo> listPoint = null;
public Page1ListViewAdapter(Page1Activity context, List<Page1FormDemo> lists){
this.mContext = context;
this.lists = lists;
layinf = LayoutInflater.from(context);
}
@Override
public int getCount() {
return lists.size();
}
@Override
public int getItemViewType(int position) {
//根据position返回指定的布局类型,比如0、1,根据这个返回值加载不同布局
return lists.get(position).getPropertyType();
}
@Override
public int getViewTypeCount() {
//这里是adapter里有几种布局
return 2;
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
Page1FormDemo form = lists.get(position);
//第1个布局:进行中
ViewDoingHolder doingHolder = null;
//第2个布局:已完成
ViewDoneHolder doneHolder = null;
if (form == null) {
return null;
}
if (convertView == null) {
switch (form.getPropertyType()){
case 0:
convertView = layinf.inflate(R.layout.page1_item1_demo, parent, false);
//使用减少findView的次数
doingHolder = new Page1ListViewAdapter.ViewDoingHolder();
doingHolder.taskName = (TextView) convertView.findViewById(R.id.taskNameDoing);
doingHolder.taskTime = (TextView) convertView.findViewById(R.id.taskTimeDoing);
doingHolder.taskState = (TextView) convertView.findViewById(R.id.taskStateDoing);
doingHolder.GridList = (GridView) convertView.findViewById(R.id.GridListDoing);
doingHolder.okBtn = (Button) convertView.findViewById(R.id.okBtn);
doingHolder.noBtn = (Button) convertView.findViewById(R.id.noBtn);
//设置标记
convertView.setTag(doingHolder);
break;
case 1:
convertView = layinf.inflate(R.layout.page1_item2_demo, parent, false);
//使用减少findView的次数
doneHolder = new Page1ListViewAdapter.ViewDoneHolder();
doneHolder.taskName = (TextView) convertView.findViewById(R.id.taskNameDone);
doneHolder.taskTime = (TextView) convertView.findViewById(R.id.taskTimeDone);
doneHolder.taskState = (TextView) convertView.findViewById(R.id.taskStateDone);
doneHolder.GridList = (GridView) convertView.findViewById(R.id.GridListDone);
doneHolder.okBtn = (Button) convertView.findViewById(R.id.okBtn);
doneHolder.noBtn = (Button) convertView.findViewById(R.id.noBtn);
//设置标记
convertView.setTag(doneHolder);
break;
}
} else {
switch (form.getPropertyType()){
case 0:
doingHolder = (Page1ListViewAdapter.ViewDoingHolder) convertView.getTag();
break;
case 1:
doneHolder = (Page1ListViewAdapter.ViewDoneHolder) convertView.getTag();
break;
}
}
/**
* 根据不同布局加载不同数据
* */
switch (form.getPropertyType()){
case 0:
doingHolder.taskName.setText(form.getTaskName());
doingHolder.taskTime.setText(form.getTaskTime());
doingHolder.taskState.setText(form.getTaskState());
//GridView
listPoint = new ArrayList<PointStateDemo>();
try {
PointStateDemo form1 = new PointStateDemo();
form1.setPatrolStatusType(0);
form1.setPatrolStatus("0");
form1.setPointName("1");
listPoint.add(form1);
PointStateDemo form2 = new PointStateDemo();
form2.setPatrolStatusType(0);
form2.setPatrolStatus("0");
form2.setPointName("2");
listPoint.add(form2);
PointStateDemo form3 = new PointStateDemo();
form3.setPatrolStatusType(0);
form3.setPatrolStatus("0");
form3.setPointName("3");
listPoint.add(form3);
PointStateDemo form4 = new PointStateDemo();
form4.setPatrolStatusType(0);
form4.setPatrolStatus("0");
form4.setPointName("4");
listPoint.add(form4);
PointStateDemo form5 = new PointStateDemo();
form5.setPatrolStatusType(0);
form5.setPatrolStatus("0");
form5.setPointName("5");
listPoint.add(form5);
PointStateDemo form6 = new PointStateDemo();
form6.setPatrolStatusType(1);
form6.setPatrolStatus("1");
form6.setPointName("6");
listPoint.add(form6);
PointStateDemo form7 = new PointStateDemo();
form7.setPatrolStatusType(1);
form7.setPatrolStatus("1");
form7.setPointName("7");
listPoint.add(form7);
PointStateDemo form8 = new PointStateDemo();
form8.setPatrolStatusType(1);
form8.setPatrolStatus("1");
form8.setPointName("8");
listPoint.add(form8);
PointStateDemo form9 = new PointStateDemo();
form9.setPatrolStatusType(1);
form9.setPatrolStatus("1");
form9.setPointName("9");
listPoint.add(form9);
PointStateDemo form10 = new PointStateDemo();
form10.setPatrolStatusType(1);
form10.setPatrolStatus("1");
form10.setPointName("10");
listPoint.add(form10);
PointStateDemo form11 = new PointStateDemo();
form11.setPatrolStatusType(1);
form11.setPatrolStatus("1");
form11.setPointName("11");
listPoint.add(form11);
PointStateDemo form12 = new PointStateDemo();
form12.setPatrolStatusType(1);
form12.setPatrolStatus("1");
form12.setPointName("12");
listPoint.add(form12);
PointStateDemo form13 = new PointStateDemo();
form13.setPatrolStatusType(1);
form13.setPatrolStatus("1");
form13.setPointName("13");
listPoint.add(form13);
PointStateDemo form14 = new PointStateDemo();
form14.setPatrolStatusType(1);
form14.setPatrolStatus("1");
form14.setPointName("14");
listPoint.add(form14);
PointStateDemo form15 = new PointStateDemo();
form15.setPatrolStatusType(1);
form15.setPatrolStatus("1");
form15.setPointName("15");
listPoint.add(form15);
}catch(Exception ex) {
Log.e("JSON Error: ", ex.toString());
}
Page1Adapter page1Adapter = new Page1Adapter(mContext,listPoint);
doingHolder.GridList.setAdapter(page1Adapter);
break;
case 1:
doneHolder.taskName.setText(form.getTaskName());
doneHolder.taskTime.setText(form.getTaskTime());
doneHolder.taskState.setText(form.getTaskState());
//GridView
listPoint = new ArrayList<PointStateDemo>();
try {
PointStateDemo form1 = new PointStateDemo();
form1.setPatrolStatusType(0);
form1.setPatrolStatus("0");
form1.setPointName("1");
listPoint.add(form1);
PointStateDemo form2 = new PointStateDemo();
form2.setPatrolStatusType(0);
form2.setPatrolStatus("0");
form2.setPointName("2");
listPoint.add(form2);
PointStateDemo form3 = new PointStateDemo();
form3.setPatrolStatusType(0);
form3.setPatrolStatus("0");
form3.setPointName("3");
listPoint.add(form3);
PointStateDemo form4 = new PointStateDemo();
form4.setPatrolStatusType(0);
form4.setPatrolStatus("0");
form4.setPointName("4");
listPoint.add(form4);
PointStateDemo form5 = new PointStateDemo();
form5.setPatrolStatusType(0);
form5.setPatrolStatus("0");
form5.setPointName("5");
listPoint.add(form5);
PointStateDemo form6 = new PointStateDemo();
form6.setPatrolStatusType(0);
form6.setPatrolStatus("0");
form6.setPointName("6");
listPoint.add(form6);
PointStateDemo form7 = new PointStateDemo();
form7.setPatrolStatusType(0);
form7.setPatrolStatus("0");
form7.setPointName("7");
listPoint.add(form7);
PointStateDemo form8 = new PointStateDemo();
form8.setPatrolStatusType(0);
form8.setPatrolStatus("0");
form8.setPointName("8");
listPoint.add(form8);
PointStateDemo form9 = new PointStateDemo();
form9.setPatrolStatusType(0);
form9.setPatrolStatus("0");
form9.setPointName("9");
listPoint.add(form9);
PointStateDemo form10 = new PointStateDemo();
form10.setPatrolStatusType(0);
form10.setPatrolStatus("0");
form10.setPointName("10");
listPoint.add(form10);
PointStateDemo form11 = new PointStateDemo();
form11.setPatrolStatusType(0);
form11.setPatrolStatus("0");
form11.setPointName("11");
listPoint.add(form11);
PointStateDemo form12 = new PointStateDemo();
form12.setPatrolStatusType(0);
form12.setPatrolStatus("0");
form12.setPointName("12");
listPoint.add(form12);
PointStateDemo form13 = new PointStateDemo();
form13.setPatrolStatusType(0);
form13.setPatrolStatus("0");
form13.setPointName("13");
listPoint.add(form13);
PointStateDemo form14 = new PointStateDemo();
form14.setPatrolStatusType(0);
form14.setPatrolStatus("0");
form14.setPointName("14");
listPoint.add(form14);
PointStateDemo form15 = new PointStateDemo();
form15.setPatrolStatusType(0);
form15.setPatrolStatus("0");
form15.setPointName("15");
listPoint.add(form15);
}catch(Exception ex) {
Log.e("JSON Error: ", ex.toString());
}
Page1Adapter2 page1Adapter2 = new Page1Adapter2(mContext,listPoint);
doneHolder.GridList.setAdapter(page1Adapter2);
break;
}
return convertView;
}
/**
* ViewHolder类
*/
static class ViewDoingHolder {
TextView taskName;
TextView taskTime;
TextView taskState;
GridView GridList;
Button noBtn;
Button okBtn;
}
static class ViewDoneHolder {
TextView taskName;
TextView taskTime;
TextView taskState;
GridView GridList;
Button noBtn;
Button okBtn;
}
}
Page1FormDemo.java
/**
* Created by lmx on 2018/2/26.
*/
public class Page1FormDemo {
private int propertyType;//属性类型 0、1、2:未开始、进行中、已完成
private String taskName;
private String taskDate;
private String taskTime;
private String taskState;
public int getPropertyType() {
return propertyType;
}
public void setPropertyType(int propertyType) {
this.propertyType = propertyType;
}
public String getTaskName() {
return taskName;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public String getTaskDate() {
return taskDate;
}
public void setTaskDate(String taskDate) {
this.taskDate = taskDate;
}
public String getTaskTime() {
return taskTime;
}
public void setTaskTime(String taskTime) {
this.taskTime = taskTime;
}
public String getTaskState() {
return taskState;
}
public void setTaskState(String taskState) {
this.taskState = taskState;
}
}
page1_item1_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--状态1-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="183dip"
android:layout_marginTop="14dip"
android:orientation="vertical"
android:background="@color/white"
>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray_white"/>
<!-- header -->
<LinearLayout
android:id="@+id/taskHeader"
android:layout_width="match_parent"
android:layout_height="37dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:orientation="horizontal"
>
<ImageView
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_marginTop="8dip"
android:layout_marginLeft="7dip"
android:background="@drawable/task1"
/>
<TextView
android:id="@+id/taskNameDoing"
android:layout_width="80dip"
android:layout_height="37dip"
android:layout_marginLeft="7dip"
android:gravity="fill_vertical"
android:text="任务名称"
android:textColor="@color/c2"
android:textSize="@dimen/table_body"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="37dip"
android:gravity="right" >
<TextView
android:id="@+id/taskStateDoing"
android:layout_width="wrap_content"
android:layout_height="37dip"
android:gravity="fill_vertical"
android:text="状态"
android:textColor="@color/c2"
android:textSize="@dimen/table_body"
/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="@color/gray_white"/>
<!-- content -->
<LinearLayout
android:id="@+id/task"
android:layout_width="match_parent"
android:layout_height="100dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="39dip"
android:orientation="horizontal">
<TextView
android:id="@+id/taskTimeDoing"
android:layout_width="wrap_content"
android:layout_height="39dip"
android:gravity="fill_vertical"
android:text="2020-12-12 13:00-15:00"
android:textColor="@color/c2"
android:textSize="@dimen/table_body"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="61dip"
android:orientation="horizontal">
<GridView
android:id="@+id/GridListDoing"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android"
android:numColumns="auto_fit"
android:columnWidth="@dimen/gridView_box2"
android:stretchMode="columnWidth"
android:verticalSpacing="@dimen/gridView_verticalSpacing"
android:horizontalSpacing="@dimen/gridView_horizontalSpacing">
<!--
GirdView属性:
android:numColumns="auto_fit" 列数设置为自动
android:columnWidth="90dp" 每列的宽度,也就是Item的宽度
android:stretchMode="columnWidth" 缩放与列宽大小同步
android:verticalSpacing="10dp" 垂直边距
android:horizontalSpacing="10dp" 水平边距
-->
</GridView>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="@color/gray_white"/>
<!-- bottom -->
<LinearLayout
android:id="@+id/taskBottom"
android:layout_width="match_parent"
android:layout_height="42dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:orientation="horizontal"
>
<Button
android:id="@+id/noBtn"
android:layout_width="80dip"
android:layout_height="28dip"
android:background="@drawable/btn_bg_normal2_demo"
android:text="按钮1"
android:gravity="center"
android:textColor="@color/c1"
android:textSize="@dimen/table_body"
android:layout_marginTop="8dip"
android:layout_marginLeft="175dip"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="28dip"
android:gravity="right"
android:layout_marginTop="8dip"
android:layout_marginLeft="8dip"
>
<Button
android:id="@+id/okBtn"
android:layout_width="80dip"
android:layout_height="fill_parent"
android:background="@drawable/btn_bg_normal_demo"
android:text="按钮2"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/table_body"
/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray_white"/>
</LinearLayout>
</LinearLayout>
page1_item2_demo.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--状态2-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="183dip"
android:layout_marginTop="14dip"
android:orientation="vertical"
android:background="@color/white"
>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray_white"/>
<!-- header -->
<LinearLayout
android:id="@+id/taskHeader2"
android:layout_width="match_parent"
android:layout_height="37dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:orientation="horizontal"
>
<ImageView
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_marginTop="8dip"
android:layout_marginLeft="7dip"
android:background="@drawable/task1_2"
/>
<TextView
android:id="@+id/taskNameDone"
android:layout_width="80dip"
android:layout_height="37dip"
android:layout_marginLeft="7dip"
android:gravity="fill_vertical"
android:text="任务名称"
android:textColor="@color/bg2"
android:textSize="@dimen/table_body"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="37dip"
android:gravity="right" >
<TextView
android:id="@+id/taskStateDone"
android:layout_width="wrap_content"
android:layout_height="37dip"
android:gravity="fill_vertical"
android:text="状态"
android:textColor="@color/bg2"
android:textSize="@dimen/table_body"
/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="@color/gray_white"/>
<!-- content -->
<LinearLayout
android:id="@+id/task2"
android:layout_width="match_parent"
android:layout_height="100dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="39dip"
android:orientation="horizontal">
<TextView
android:id="@+id/taskTimeDone"
android:layout_width="wrap_content"
android:layout_height="39dip"
android:gravity="fill_vertical"
android:text="2020-12-12 13:00-15:00"
android:textColor="@color/bg2"
android:textSize="@dimen/table_body"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="61dip"
android:orientation="horizontal">
<GridView
android:id="@+id/GridListDone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/white"
xmlns:android="http://schemas.android.com/apk/res/android"
android:numColumns="auto_fit"
android:columnWidth="@dimen/gridView_box2"
android:stretchMode="columnWidth"
android:verticalSpacing="@dimen/gridView_verticalSpacing"
android:horizontalSpacing="@dimen/gridView_horizontalSpacing">
</GridView>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="@color/gray_white"/>
<!-- bottom -->
<LinearLayout
android:id="@+id/taskBottom2"
android:layout_width="match_parent"
android:layout_height="42dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:orientation="horizontal"
>
<Button
android:id="@+id/noBtn"
android:layout_width="80dip"
android:layout_height="28dip"
android:background="@drawable/btn_bg_no1_demo"
android:text="按钮1"
android:gravity="center"
android:textColor="@color/bg2"
android:textSize="@dimen/table_body"
android:layout_marginTop="8dip"
android:layout_marginLeft="175dip"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="28dip"
android:gravity="right"
android:layout_marginTop="8dip"
android:layout_marginLeft="8dip"
>
<Button
android:id="@+id/okBtn"
android:layout_width="80dip"
android:layout_height="fill_parent"
android:background="@drawable/btn_bg_no2_demo"
android:text="按钮2"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/table_body"
/>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray_white"/>
</LinearLayout>
</LinearLayout>
Page1Adapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.andbase.R;
import com.andbase.patrol.gtDemo.model.PointStateDemo;
import java.util.List;
/**
* Created by lmx on 2018/1/31.
*/
public class Page1Adapter extends BaseAdapter {
private Context mContext;
private List<PointStateDemo> lists;
public Page1Adapter(Context context, List<PointStateDemo> lists){
this.mContext = context;
this.lists = lists;
}
@Override
public int getCount() {
return lists.size();
}
@Override
public int getItemViewType(int position) {
//根据position返回指定的布局类型,比如0、1,根据这个返回值加载不同布局
return lists.get(position).getPatrolStatusType();
}
@Override
public int getViewTypeCount() {
//这里是adapter里有几种布局
return 2;
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
PointStateDemo form = lists.get(position);
//未巡检0
BlueViewHolder blueHolder = null;
//正常1
DarkBlueViewHolder darkBlueHolder = null;
if (form == null) {
return null;
}
if (convertView == null){
switch (form.getPatrolStatusType()){
case 0://未巡检
convertView = LayoutInflater.from(mContext).inflate(R.layout.page_detail_item_blue_demo, parent, false);
//使用减少findView的次数
blueHolder = new Page1Adapter.BlueViewHolder();
blueHolder.pointName = (TextView) convertView.findViewById(R.id.pointName);
//设置标记
convertView.setTag(blueHolder);
break;
case 1://正常
convertView = LayoutInflater.from(mContext).inflate(R.layout.page_detail_item_dark_blue_demo, parent, false);
//使用减少findView的次数
darkBlueHolder = new Page1Adapter.DarkBlueViewHolder();
darkBlueHolder.pointName = (TextView) convertView.findViewById(R.id.pointName);
//设置标记
convertView.setTag(darkBlueHolder);
break;
}
}else {
switch (form.getPatrolStatusType()){
case 0:
blueHolder = (Page1Adapter.BlueViewHolder) convertView.getTag();
break;
case 1:
darkBlueHolder = (Page1Adapter.DarkBlueViewHolder) convertView.getTag();
break;
}
}
//设置数据
switch (form.getPatrolStatusType()){
case 0:
blueHolder.pointName.setText(form.getPointName());
break;
case 1:
darkBlueHolder.pointName.setText(form.getPointName());
break;
}
return convertView;
}
/**
* ViewHolder类
*/
static class BlueViewHolder {
TextView pointName;
}
static class DarkBlueViewHolder {
TextView pointName;
}
}
Page1Adapter2.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.andbase.R;
import com.andbase.patrol.gtDemo.model.PointStateDemo;
import java.util.List;
/**
* Created by lmx on 2018/1/31.
*/
public class Page1Adapter2 extends BaseAdapter {
private Context mContext;
private List<PointStateDemo> lists;
public Page1Adapter2(Context context, List<PointStateDemo> lists){
this.mContext = context;
this.lists = lists;
}
@Override
public int getCount() {
return lists.size();
}
@Override
public int getItemViewType(int position) {
//根据position返回指定的布局类型,比如0、1,根据这个返回值加载不同布局
return lists.get(position).getPatrolStatusType();
}
@Override
public int getViewTypeCount() {
//这里是adapter里有几种布局
return 1;
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
PointStateDemo form = lists.get(position);
//未巡检0
GrayViewHolder grayHolder = null;
if (form == null) {
return null;
}
if (convertView == null){
switch (form.getPatrolStatusType()){
case 0://未巡检
convertView = LayoutInflater.from(mContext).inflate(R.layout.page_detail_item_gray_demo, parent, false);
//使用减少findView的次数
grayHolder = new Page1Adapter2.GrayViewHolder();
grayHolder.pointName = (TextView) convertView.findViewById(R.id.pointName);
//设置标记
convertView.setTag(grayHolder);
break;
}
}else {
switch (form.getPatrolStatusType()){
case 0:
grayHolder = (Page1Adapter2.GrayViewHolder) convertView.getTag();
break;
}
}
//设置数据
switch (form.getPatrolStatusType()){
case 0:
grayHolder.pointName.setText(form.getPointName());
break;
}
return convertView;
}
/**
* ViewHolder类
*/
static class GrayViewHolder {
TextView pointName;
}
}
PointStateDemo.java
import com.ab.db.orm.annotation.Column;
import com.ab.db.orm.annotation.Id;
import com.ab.db.orm.annotation.Table;
/**
* Created by lmx on 2018/2/26.
*/
@Table(name = "M_POINT_STATE")
public class PointStateDemo {
// _ID @Id主键,int类型,数据库建表时此字段会设为自增长
@Id
@Column(name = "_id")
public int _id;
@Column(name = "id")
private String id;
@Column(name = "guid")
private String guid;
@Column(name = "task_id")
private String taskId;
@Column(name = "point_name")
public String pointName;
@Column(name = "patrol_status")
private String patrolStatus;
//根据状态不同,切换不同颜色
@Column(name = "patrol_status_type")
private int patrolStatusType;
public int getPatrolStatusType() {
return patrolStatusType;
}
public void setPatrolStatusType(int patrolStatusType) {
this.patrolStatusType = patrolStatusType;
}
public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public String getTaskId() {
return taskId;
}
public void setTaskId(String taskId) {
this.taskId = taskId;
}
public String getPointName() {
return pointName;
}
public void setPointName(String pointName) {
this.pointName = pointName;
}
public String getPatrolStatus() {
return patrolStatus;
}
public void setPatrolStatus(String patrolStatus) {
this.patrolStatus = patrolStatus;
}
}