package com.quickblox.sample.chat.ui.widget;

import android.content.Context;
import android.database.DataSetObserver;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Adapter;
import android.widget.RelativeLayout;

public class AttachmentPreviewAdapterView extends RelativeLayout {

    private static String TAG = AttachmentPreviewAdapterView.class.getSimpleName();

    private static final Handler mainThreadHandler = new Handler(Looper.getMainLooper());

    private Adapter adapter;
    private DataSetObserver dataSetObserver;

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

        dataSetObserver = new DataSetObserver() {
            @Override
            public void onChanged() {
                populateWithViewsFromAdapter();
            }

            @Override
            public void onInvalidated() {
                populateWithViewsFromAdapter();
            }
        };
    }

    public void setAdapter(Adapter newAdapter) {
        if (adapter != null) {
            adapter.unregisterDataSetObserver(dataSetObserver);
        }
        adapter = newAdapter;
        adapter.registerDataSetObserver(dataSetObserver);
        populateWithViewsFromAdapter();
    }

    private void populateWithViewsFromAdapter() {
        mainThreadHandler.post(new Runnable() {
            @Override
            public void run() {
                removeAllViews();
                for (int i = 0; i < adapter.getCount(); i++) {
                    View childView = adapter.getView(i, null, AttachmentPreviewAdapterView.this);
                    addView(childView, i);
                }
            }
        });
    }
}

@@ -225,16 +225,47 @@
 w.View;%0A
+import android.view.ViewGroup;%0A
 import a
@@ -309,24 +309,66 @@
 .widget.
-Relative
+HorizontalScrollView;%0Aimport android.widget.Linear
 Layout;%0A
@@ -422,109 +422,30 @@
 nds 
-RelativeLayout %7B%0A%0A    private static String TAG = AttachmentPreviewAdapterView.class.getSimpleName();
+HorizontalScrollView %7B
 %0A%0A  
@@ -529,24 +529,60 @@
 Looper());%0A%0A
+    private LinearLayout container;%0A
     private 
@@ -751,24 +751,340 @@
 t, attrs);%0A%0A
+        container = new LinearLayout(context);%0A        container.setOrientation(LinearLayout.HORIZONTAL);%0A        HorizontalScrollView.LayoutParams lp = new HorizontalScrollView.LayoutParams(%0A                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);%0A        addView(container, lp);%0A%0A
         data
@@ -1845,16 +1845,26 @@
         
+container.
 removeAl
@@ -2046,32 +2046,42 @@
                 
+container.
 addView(childVie

Add scrolling ability to attachments.