自定义列表视图:选择时的span listitems

时间:2014-09-18 09:44:05

标签: android listview android-listview android-custom-view listviewitem

首先,我为没有在我所做的事情上发布任何代码而道歉。但我没有明白从哪里开始定制listview。

问题:

我必须创建一个列表视图,其中基于选择的相邻列表项将合并。

我用Google搜索但无法找到相关信息。

请帮帮我。

请参阅随附的image

请参阅此image ..它显示了listitems的圆角背景

默认列表是带有一些自定义适配器的列表,第二个图像显示了预期的内容。

1 个答案:

答案 0 :(得分:0)

这有点棘手,但我认为这个问题的最佳解决方案是listView中的listView。

现在让我解释一下,

应该有两个列表视图

LV1(ExtendedHeightListView):它的高度等于所有项目的高度(显然视图不会在此列表视图中回收)。我们将它用作主列表视图的项目。

public class ExtendedHeightListView extends ListView {

    boolean expanded = false;

    public ExtendedHeightListView(Context context) {
        super(context);
    }

    public ExtendedHeightListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ExtendedHeightListView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
    }

    public boolean isExpanded() {
        return expanded;
    }

    public void setExpanded(boolean expanded) {
        this.expanded = expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if (isExpanded()) {
            // Calculate entire height by providing a very large height hint.
            // But do not use the highest 2 bits of this integer; those are
            // reserved for the MeasureSpec mode.
            int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

            android.view.ViewGroup.LayoutParams params = getLayoutParams();
            params.height = getMeasuredHeight();
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}

LV2(默认ListView):主ListView将项目作为扩展高度列表视图。 (这是正常的列表视图,因此您的小列表视图将循环使用)。

现在考虑分组方式的数据。说

G1 - 2项, G2 - 5项, G3 - 1项

ExtendedHeightListView(LV1)的BaseAdapter(BA1):

public void BA1(Context context, Grp g) {
    this.context = context;
     this.grp = g;
}

public int getCount() {
    return grp.getNumberOfItems();
}

public View getView(int position, ... ) {

    // write here your normal getView;

}

Main ListView(LV2)的BaseAdapter(BA2):

将G1,G2和G3传递给适配器。

public int getCount() {
    return 3;  //Number of groups
}

public View getView (int position, ... ) {
     if (convertView == null) {
       convertView = inflate LV1;
     } 

     ExtendedHeightListView lv1 = convertView.findViewById(R.id.lv1);
     Grp g = getGroup(position);

     BA1 adapter = new BA1 (context, g);

     //If background of each group is different
     lv1.setBackgroundResource(R.drawable.bg);

     lv1.setAdapter (adapter);

     return convertView;

}

在Main ListView(LV2)上设置OnItemClickListener。

我认为这会有所帮助......