-1

First of all I apologize for not posting any code on what I have done.But I am not getting the point from where and how to start customize listview.

Question :

I have to create a listview in which based on the selection adjacent listitems shall merge.

I googled but couldn't find any relevant information.

Please help me.

Please see the attached image.

See this image..it shows rounded corner background of listitems

Default list is the list with some custom adapter and second image shows what is expected.

Shruti
  • 1
  • 13
  • 55
  • 95
  • If you want merge two cell on selection then you need to set divider background null for particular row. – Piyush Sep 18 '14 at 10:13
  • @PG_Android : but background of item is different , it is some gradient with rounded corners.. – Shruti Sep 18 '14 at 10:14
  • How will item click work? means for two item in one – Chirag Jain Sep 18 '14 at 10:16
  • @Shruti Yes you are right. You have applied custom background. Have you searched all?? – Piyush Sep 18 '14 at 10:19
  • @ChiragJain : after merging it will be considered as single item only – Shruti Sep 18 '14 at 10:21
  • @PG_Android : please see this image http://imgur.com/4FeIoUV ... corners of lit item background are like this...and yes I tried to search about how to merge two listitems to have them as only one item..but couldn't get lucky.. – Shruti Sep 18 '14 at 10:22
  • @Shruti I think after merge two cell you have to add that item in another listview and make custom adapter for it. So it is tough to do but i think one is the way. and i found not same but useful links. Just check these http://stackoverflow.com/questions/10582608/join-two-cursor-listviews-in-one-with-separator-lines , http://stackoverflow.com/questions/19129975/android-auto-merge-two-listview-in-one-screen – Piyush Sep 18 '14 at 10:24
  • @Shruti is these links are useful to you? – Piyush Sep 18 '14 at 10:37
  • yes i saw those links @PG_Android..but couldn't get more out of it..i was thinking to use table layout inside scrollview to achieve this. – Shruti Sep 19 '14 at 09:12
  • why a downvote ???? atleast comment and tell me the reason – Shruti Sep 24 '14 at 07:51
  • Does my answer solved your problem? – Chirag Jain Nov 11 '14 at 07:11
  • no,it didn't helped me @ChiragJain – Shruti Nov 11 '14 at 09:28

1 Answers1

0

It is bit tricky but i think the best possible solution for this problem is listView inside a listView.

Now let me explain,

There should be two list view

LV1 (ExtendedHeightListView) : It will have height equal to the height of all items (obviously view will not recycle in this listview). We will use it as a item for main list view.

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 (Default ListView) : Main ListView have items as extended height list view. (It is normal list view so your small list views will recycle).

Now considering you have data in grouped manner. Say

G1 - 2 items, G2 - 5 items, G3 - 1 item

BaseAdapter(BA1) for ExtendedHeightListView (LV1) :

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;

}

BaseAdapter(BA2) for Main ListView (LV2) :

pass G1, G2 and G3 to the adapter.

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;

}

And set your OnItemClickListener on Main ListView (LV2).

I think it will help...

Chirag Jain
  • 1,612
  • 13
  • 20