AdapterView (TwoWayView) margin of item

928 views Asked by At

I'm using the TwoWayView library : https://github.com/lucasr/twoway-view to try and implement a horizontal ListView of sorts to overhaul what I had before. (HorizontalScrollView with a horizontally oriented LinearLayout that I add views to)

I want to have a margin before the first item, so there's a blank space at the left when the View is first created. But when scrolled, the blank space would be scrolled and disappear as well. When using a normal ListView it is essentially a HeaderView that I want.

When I implemented the scroll using HorizontalScrollView, I simply programmatically checked the first item and added the margin, which worked since its parent was LinearLayout and accepts margins. But I cannot do this in the getView() of the adapter used for this AdapterView since its LayoutParams do not inherit ViewGroup.MarginLayoutParams

I've also tried setting clipToPadding="false", but the views gets recycled too early, which is unacceptable since the padding I need is noticeably big.

Is there a way to achieve this behavior without moving all the HeaderView code from ListView into the TwoWayView library?

1

There are 1 answers

0
Ektos974 On

You can try something like this:

on getView(int pos, View convertView, ViewGroup parent){}

{
            if (convertView == null)
            { ... }
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT);

            /// To set your margin, you just need to test the position :)
            params.setMargins(left, top, right, bottom);

            viewHolder.LinearLayout.setLayoutParams(params);

        }

Hope this would help you.