1

我已经努力了几个星期来解决这个问题。我有两个可扩展的视图,一个在另一个内部。每当我单击第二级可扩展视图项时,它都不会膨胀其子项。当我单击时,我看到的只是一个小滚动条,它出现在可扩展视图项本身的右侧。使用具有固定 textview 的简单布局会导致相同的行为,这意味着问题不在 ShiftListItem 对象中。

此外,从未调用过 getChildView()。

这是出现一秒钟的小滚动条

年份列表适配器:

package com.dg.android.salarycalculator.adapter;

import java.util.ArrayList;
import java.util.Calendar;

import com.dg.android.salarycalculator.R;
import com.dg.android.salarycalculator.Salary;
import com.dg.android.salarycalculator.views.MonthsListView;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class YearListAdapter extends BaseExpandableListAdapter {

private Context _context;
private SparseArray<SparseArray<ArrayList<Salary>>> _shiftYears;

public YearListAdapter(
        SparseArray<SparseArray<ArrayList<Salary>>> shiftYears,
        Context context) {
    super();

    _context = context;
    _shiftYears = shiftYears;

}

public Object getChild(int arg0, int arg1) {
    return (null == _shiftYears.get(_shiftYears.keyAt(arg0))) ? null
            : _shiftYears.get(_shiftYears.keyAt(arg0)).get(
                    _shiftYears.get(_shiftYears.keyAt(arg0)).keyAt(arg1));
}

public long getChildId(int arg0, int arg1) {
    return arg1;
}

public View getChildView(int arg0, int arg1, boolean arg2,
        View convertView, ViewGroup arg4) {

    MonthsListView monthLevelExpandable = new MonthsListView(_context);

    MonthListAdapter adapter = new MonthListAdapter(_context,
            _shiftYears.get(_shiftYears.keyAt(arg0)));
    monthLevelExpandable.setAdapter(adapter);

    adapter.forceReload();
    monthLevelExpandable.setGroupIndicator(null);

    return monthLevelExpandable;

}

public int getChildrenCount(int arg0) {
    if (null == _shiftYears.get(_shiftYears.keyAt(arg0)))
        return 0;
    return _shiftYears.get(_shiftYears.keyAt(arg0)).size();
}

public Object getGroup(int arg0) {
    return (null == _shiftYears) ? null : _shiftYears.get(_shiftYears
            .keyAt(arg0));
}

public int getGroupCount() {
    return _shiftYears.size();
}

public long getGroupId(int arg0) {
    return arg0;
}

public View getGroupView(int arg0, boolean isExpanded, View convertView,
        ViewGroup arg3) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) _context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.year_list_view_item, null);

    }

    TextView title = (TextView) convertView.findViewById(R.id.yearTitle);
    title.setText("" + _shiftYears.keyAt(arg0));

    return convertView;

}

public boolean hasStableIds() {
    return true;
}

public boolean isChildSelectable(int arg0, int arg1) {
    return true;
}

public void forceReload() {
    notifyDataSetChanged();
}

}

月列表适配器:

package com.dg.android.salarycalculator.adapter;

import java.util.ArrayList;
import java.util.Calendar;

import com.dg.android.salarycalculator.R;
import com.dg.android.salarycalculator.Salary;
import com.dg.android.salarycalculator.views.ShiftListItem;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class MonthListAdapter extends BaseExpandableListAdapter {

private Context _context;
private SparseArray<ArrayList<Salary>> _shiftMonths;

public MonthListAdapter(Context context,
        SparseArray<ArrayList<Salary>> shiftMonths) {
    super();

    _context = context;
    _shiftMonths = shiftMonths;

}

public Object getChild(int groupPosition, int childPosition) {
    return (null == _shiftMonths) ? null : _shiftMonths.get(
            _shiftMonths.keyAt(groupPosition)).get(childPosition);

}

public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {


    ShiftListItem sli;
    if (null == convertView) {
        sli = (ShiftListItem) View.inflate(_context,
                R.layout.shift_list_item, null);
    } else {
        sli = (ShiftListItem) convertView;
    }
    sli.setSalary(_shiftMonths.get(_shiftMonths.keyAt(groupPosition)).get(
            childPosition));
    return sli;
}





public int getChildrenCount(int groupPosition) {
    if (null == _shiftMonths
            || null == _shiftMonths.get(_shiftMonths.keyAt(groupPosition)))
        return 0;           
    return _shiftMonths.get(_shiftMonths.keyAt(groupPosition)).size();
}

public Object getGroup(int groupPosition) {
    return (null == _shiftMonths) ? null : _shiftMonths.get(_shiftMonths
            .keyAt(groupPosition));
}

public int getGroupCount() {
    return _shiftMonths.size();
}

public long getGroupId(int groupPosition) {
    return groupPosition;
}

public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) _context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.month_list_view_item, null);
    }

    TextView title = (TextView) convertView.findViewById(R.id.monthTitle);
    title.setText("" + _shiftMonths.keyAt(groupPosition));

    return convertView;

}

public boolean hasStableIds() {
    return true;
}

public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

public void forceReload() {
    notifyDataSetChanged();
}

}

移位列表项:

    package com.dg.android.salarycalculator.views;


    import java.util.Calendar;
    import com.dg.android.salarycalculator.R;
    import com.dg.android.salarycalculator.Salary;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ShiftListItem extends LinearLayout {

    private Salary salary;
    private TextView itemTimeAndDate;
    private TextView itemCashView;

    public ShiftListItem(Context context, AttributeSet attrs) {
        super(context, attrs);

        salary = new Salary();
    }
@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    itemTimeAndDate = (TextView) findViewById(R.id.itemTimeAndDate);
    itemCashView = (TextView) findViewById(R.id.itemCashView);
}

public void setSalary(Salary salary) {
    this.salary = salary;

    Calendar start = salary.getShiftTime().getStart();
    Calendar end = salary.getShiftTime().getEnd();

    int tHour;
    int tMinute;
    int tSec;

    tSec = salary.getShiftTime().getDifference(
            salary.getShiftTime().getStart(),
            salary.getShiftTime().getEnd());
    tHour = tSec / 3600;
    tSec -= tHour * 3600;
    tMinute = tSec / 60;
    tSec -= tMinute * 60;

    itemTimeAndDate.setText("Time: (Start) "
            + returnTwoDigitString(start.get(Calendar.HOUR_OF_DAY)) + ":"
            + returnTwoDigitString(start.get(Calendar.MINUTE)) + " (End)"
            + returnTwoDigitString(end.get(Calendar.HOUR_OF_DAY)) + ":"
            + returnTwoDigitString(end.get(Calendar.MINUTE)) + "\n"
            + (start.get(Calendar.DAY_OF_MONTH)) + "-"
            + (end.get(Calendar.DAY_OF_MONTH)) + "/"
            + (start.get(Calendar.MONTH) + 1) + "-"
            + (end.get(Calendar.MONTH) + 1) + "/"
            + start.get(Calendar.YEAR) + "-" + end.get(Calendar.YEAR));
    itemCashView.setText(salary.getCash() + " " + salary.getCurrency()
            + " Total Time Of: " + returnTwoDigitString(tHour) + ":"
            + returnTwoDigitString(tMinute) + ":"
            + returnTwoDigitString(tSec));
}

public Salary getSalary() {
    return salary;
}

private String returnTwoDigitString(int time) {
    if (time < 10) {
        return "0" + time;
    }

    return "" + time;
}
}

shift_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.dg.android.salarycalculator.views.ShiftListItem xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/itemTimeAndDate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="false" />

    <TextView
        android:id="@+id/itemCashView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textIsSelectable="false" />

</com.dg.android.salarycalculator.views.ShiftListItem>
4

1 回答 1

0

我添加了下一个代码:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        widthMeasureSpec = MeasureSpec
                .makeMeasureSpec(960, MeasureSpec.AT_MOST);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(600,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

并添加了下一个字段: int intGroupPosition, intChildPosition, intGroupid;

到我所有ExpandableListView类(当然使用你自己的输入),它现在可以工作了!

于 2013-08-26T18:48:50.363 回答