expandlistview 分组+浮动条

  

分组实现:主要是数据结构的组织和getGroupVIew函数中的判断处理。

public class RecordExpandableListViewAdapter extends BaseExpandableListAdapter {


private Context mContext;
private ArrayList<Object> mList;
private LayoutInflater mInflater;


class GroupViewHolder {
TextView tv_time;
ImageView img_groupIndicator;
}


class ChildViewHolder {
TextView tv_name;
TextView tv_status;
}


public RecordExpandableListViewAdapter(Context context,
ArrayList<Object> list) {
// TODO Auto-generated constructor stub
mContext = context;
mList = list;


mInflater = LayoutInflater.from(context);
}


@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
Object obj_group = mList.get(groupPosition);
Object obj_child = null;


if (obj_group instanceof Room) {
Room r = (Room) obj_group;
obj_child = r.tables.get(childPosition);
}
return obj_child;
}


@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}


@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ChildViewHolder childViewHolder = null;
if (convertView == null) {
childViewHolder = new ChildViewHolder();
convertView = mInflater.inflate(R.layout.child_record, null);


childViewHolder.tv_name = (TextView) convertView
.findViewById(R.id.tv_room_name);
childViewHolder.tv_status = (TextView) convertView
.findViewById(R.id.tv_status);


convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
Object obj = mList.get(groupPosition);
if (obj instanceof String) {
} else if (obj instanceof Room) {
Room r = (Room) obj;
String name = r.tables.get(childPosition).name;
String status = r.tables.get(childPosition).status;
childViewHolder.tv_name.setText(name);
childViewHolder.tv_status.setText(status);
}
return convertView;


}


@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
int count = 0;
Object obj = mList.get(groupPosition);


if (obj instanceof String) {
count = 0;
} else if (obj instanceof Room) {
Room r = (Room) obj;
count = r.tables.size();
}
return count;
}


@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
Object record = mList.get(groupPosition);
return record;
}


@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return mList.size();
}


@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}


@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
GroupViewHolder holder = null;
if (convertView == null) {
holder = new GroupViewHolder();
convertView = mInflater.inflate(R.layout.group_record, null);


holder.tv_time = (TextView) convertView
.findViewById(R.id.tv_record_time);
holder.img_groupIndicator = (ImageView) convertView
.findViewById(R.id.img_groupIndicator);
convertView.setTag(holder);
} else {
holder = (GroupViewHolder) convertView.getTag();
}
Object obj = mList.get(groupPosition);


String group = "";
if (obj instanceof String) {
group = (String) obj;
convertView.setBackgroundResource(R.color.deep_blue);
holder.img_groupIndicator.setVisibility(View.GONE);
} else if (obj instanceof Room) {
Room r = (Room) obj;
group = r.name;
convertView.setBackgroundResource(R.color.light_green);
holder.img_groupIndicator.setVisibility(View.VISIBLE);
if (isExpanded) {
holder.img_groupIndicator
.setImageResource(R.drawable.expander_open_holo_light);
} else {
holder.img_groupIndicator
.setImageResource(R.drawable.expander_close_holo_light);
}


}
holder.tv_time.setText(group);


return convertView;
}


@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}


@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}


}



浮动标签实现:

xml定义:

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/btn_submit_all" >


        <ExpandableListView
            android:id="@+id/elv_record"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false"
            android:fastScrollEnabled="true"
            android:groupIndicator="@null" >
        </ExpandableListView>


        <TextView
            android:id="@+id/topGroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#cc33B5E5"
            android:paddingLeft="5dp"
            android:textSize="18sp" >
        </TextView>
    </FrameLayout>

    

activity 中设置 expandlistview 的onScroll监听elv_record.setOnScrollListener(this);

实现onScroll方法:

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
final ExpandableListView listView = (ExpandableListView) view;
/**
* calculate point (0,0)
*/
int npos = view.pointToPosition(0, 0);// 其实就是firstVisibleItem
if (npos == AdapterView.INVALID_POSITION)// 如果第一个位置值无效
return;


long pos = listView.getExpandableListPosition(npos);
// int childPos = ExpandableListView.getPackedPositionChild(pos);// 获取第一行child的id
int groupPos = ExpandableListView.getPackedPositionGroup(pos);// 获取第一行group的id


// MyLog.v(TAG, "groupPos=" + groupPos);
// MyLog.v(TAG, "childPos=" + childPos);


Object obj = adapter.getGroup(groupPos);
if (obj instanceof Room) {
indicatorGroup.setVisibility(View.VISIBLE);
} else {
indicatorGroup.setVisibility(View.GONE);// 隐藏指示器
}


// update the data of indicator group view
if (groupPos != indicatorGroupId) {// 如果指示器显示的不是当前group
indicatorGroupId = groupPos;
indicatorGroup.setText(getTime(groupPos));
}
if (indicatorGroupId == -1) // 如果此时grop的id无效,则返回
return;


}


  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
### 回答1: 可以定义一个结构体来存储每个人的信息,结构体内包含姓名、基本工资、浮动工资、支出等字段。然后使用循环遍历每个人,计算出工资实发数(基本工资+浮动工资-支出),最后输出每个人的姓名和工资实发数。 具体代码如下: ``` struct Person { char name[20]; int basic_salary; int float_salary; int expenditure; } persons[3]; int main() { for (int i = 0; i < 3; i++) { scanf("%s %d %d %d", persons[i].name, &persons[i].basic_salary, &persons[i].float_salary, &persons[i].expenditure); } for (int i = 0; i < 3; i++) { int salary = persons[i].basic_salary + persons[i].float_salary - persons[i].expenditure; printf("%s的工资实发数为%d\n", persons[i].name, salary); } return 0; } ``` 注意,上面的代码是给出了结构体的定义和循环读入和输出的示例,如果要使用请根据需要更改,如输入数据等 ### 回答2: 编写程序用结构体存放工资数据,可以方便管理和操作。首先,我们可以定义一个名为“Salary”的结构体,结构体中包括工资发放人的姓名、基本工资、浮动工资和支出四个数据项,如下所示: ```c++ struct Salary { string name; // 姓名 double base; // 基本工资 double bonus; // 浮动工资 double expense; // 支出 }; ``` 我们可以从以下数据表中读取数据,并用结构体赋值: | 姓名 | 基本工资 | 浮动工资 | 支出 | | :--: | :------: | :------: | :--: | | 张三 | 5000.00 | 800.00 | 250.00 | | 李四 | 6000.00 | 900.00 | 350.00 | | 王五 | 6500.00 | 1000.00 | 400.00 | | 赵六 | 5500.00 | 1200.00 | 300.00 | | 孙七 | 7000.00 | 1500.00 | 600.00 | ```c++ Salary s[5]; // 定义一个长度为5的Salary数组 // 对每个Salary对象进行赋值 s[0].name = "张三"; s[0].base = 5000.00; s[0].bonus = 800.00; s[0].expense = 250.00; s[1].name = "李四"; s[1].base = 6000.00; s[1].bonus = 900.00; s[1].expense = 350.00; s[2].name = "王五"; s[2].base = 6500.00; s[2].bonus = 1000.00; s[2].expense = 400.00; s[3].name = "赵六"; s[3].base = 5500.00; s[3].bonus = 1200.00; s[3].expense = 300.00; s[4].name = "孙七"; s[4].base = 7000.00; s[4].bonus = 1500.00; s[4].expense = 600.00; ``` 接下来,我们可以根据数据计算每个人的实发工资,并将结果输出: ```c++ for (int i = 0; i < 5; i++) { double net_salary = s[i].base + s[i].bonus - s[i].expense; cout << s[i].name << "\t实发工资:" << net_salary << "元" << endl; } ``` 完整的代码示例如下所示: ```c++ #include <iostream> #include <string> using namespace std; struct Salary { string name; double base; double bonus; double expense; }; int main() { Salary s[5]; s[0].name = "张三"; s[0].base = 5000.00; s[0].bonus = 800.00; s[0].expense = 250.00; s[1].name = "李四"; s[1].base = 6000.00; s[1].bonus = 900.00; s[1].expense = 350.00; s[2].name = "王五"; s[2].base = 6500.00; s[2].bonus = 1000.00; s[2].expense = 400.00; s[3].name = "赵六"; s[3].base = 5500.00; s[3].bonus = 1200.00; s[3].expense = 300.00; s[4].name = "孙七"; s[4].base = 7000.00; s[4].bonus = 1500.00; s[4].expense = 600.00; for (int i = 0; i < 5; i++) { double net_salary = s[i].base + s[i].bonus - s[i].expense; cout << s[i].name << "\t实发工资:" << net_salary << "元" << endl; } return 0; } ``` 通过使用结构体和数组,我们可以很方便地管理和操作大量的工资数据,并且代码的可读性和可维护性也得到了很好的保障。 ### 回答3: 在编写程序时,首先需要定义一个结构体,结构体中包括姓名、基本工资、浮动工资、支出和实发工资等五个变量。然后,可以通过for循环从数组中读取每个人的信息,并将其存储在对应的结构体变量中。接着,可以通过计算每个人的实发工资,并将其输出。 具体的代码实现如下: ``` #include <stdio.h> #include <string.h> #define MAX_NUM 10 struct employee { char name[20]; float base_salary; float float_salary; float expend; float actual_salary; }; int main() { struct employee list[MAX_NUM]; int i; for (i = 0; i < MAX_NUM; i++) { printf("Please input the information of employee %d:\n", i+1); printf("Name: "); scanf("%s", list[i].name); printf("Base salary: "); scanf("%f", &list[i].base_salary); printf("Float salary: "); scanf("%f", &list[i].float_salary); printf("Expend: "); scanf("%f", &list[i].expend); list[i].actual_salary = list[i].base_salary + list[i].float_salary - list[i].expend; } printf("\n\n"); printf("Name \t Actual Salary\n"); for (i = 0; i < MAX_NUM; i++) { printf("%-10s \t %-10.2f\n", list[i].name, list[i].actual_salary); } return 0; } ``` 在以上代码中,定义了一个结构体类型employee,并定义了一个长度为MAX_NUM的数组list,用于存储所有员工的信息。在循环中,通过scanf按顺序读入每个人的姓名、基本工资、浮动工资和支出,并计算出每个人的实际工资。最后,通过for循环输出所有员工的姓名和实际工资。 此外,为了方便输出,使用了printf中的格式控制符。其中,%-10s表示输出一个长度为10的左对齐字符串,%-10.2f表示输出一个长度为10的左对齐小数点后两位的浮点数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C_see

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值