2

I used this guide to create a Custom ListView that parses its data from JSON.

https://www.bignerdranch.com/blog/customizing-android-listview-rows-subclassing/

I want to add a Floating Action Button to the ItemListFragment but don't know how.

Tried to put it under here:

public ItemView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    LayoutInflater.from(context).inflate(R.layout.item_view_children, this, true);
    setupChildren();
}

But it gets displayed to every Item which i do not want.

Here is the code with the full example:

https://github.com/bignerdranch/android-listview-custom-view/tree/master/ListItemViewDemo/src/com/bignerdranch/android/listitemviewdemo

Any ideas how to do this?

Tried to modify inflate but dosen't work:

public static ItemView inflate(ViewGroup parent) {
    ItemView itemView = (ItemView)LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_view, parent, false);
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    FloatingActionButton fab = (FloatingActionButton) inflater.inflate(R.layout.fab_view, parent, false);
    return itemView;
}

EDIT:

If i place it under constructor, it works ok. But Because constructor is called multiple times for each view, i have to find a way to call it only once.

public ItemView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    LayoutInflater.from(context).inflate(R.layout.item_view_children, this, true);
    LayoutInflater.from(context).inflate(R.layout.fab_view, this, true);
    setupChildren();
}
Spira
  • 63
  • 1
  • 10
  • add programmatically FAB to your list activity Java class – piotrek1543 Dec 27 '15 at 15:38
  • https://github.com/shamanland/floating-action-button with good docs that should be able to follow for fab... – Robert Rowntree Dec 27 '15 at 17:28
  • Thanks for your info. I had already checked how Floating Action Button is working. I cannot figure out how to place it on my ListiView which uses a custom Adapter. – Spira Dec 27 '15 at 22:35
  • wrap the fab.xml inside a 'RelativeLayout' as a sibling of the ListView. IMO the declaration of the fab is independent of whatever list u have. Its a sibling organized within the parents borders so as to 'float' above the list. – Robert Rowntree Dec 28 '15 at 00:33
  • Could you please check the example? I get what to mean but i cannot do it in the example code. – Spira Dec 29 '15 at 20:06

3 Answers3

1

Solution finally was found. Was much simpler that i thought. ItemListFragment which extends ListFragment onCreateView method invokes the overridden method.

So replacing:

View v = super.onCreateView(inflater, container, savedInstanceState);

with:

View v = inflater.inflate(R.layout.list_view, null); 

worked!

Also had to set list_vew.xml:

<FrameLayout>
    <ListView android:id="@id/android:list" />
    <Button />
</FrameLayout>
Spira
  • 63
  • 1
  • 10
  • Are you recycling inflated views? Or just creating them and leaving them by there? – Joaquin Iurchuk Jan 01 '16 at 23:44
  • As being new to Android, don't know so much to answer you. I think this might answer better: http://stackoverflow.com/questions/15912999/recycling-views-in-custom-array-adapter-how-exactly-is-it-handled – Spira Jan 03 '16 at 11:56
0

Add this code to your XML layout file and it will be displayed in the bottom right hand corner.

 <android.support.design.widget.FloatingActionButton android:id="@+id/fab"
 android:layout_width="wrap_content" android:layout_height="wrap_content"
 android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
 android:src="@android:drawable/ic_menu_camera" />

Then in your fragment class you can call it with this code

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           //put your code here
        }
    });
Nicolas Zawada
  • 497
  • 1
  • 3
  • 12
  • This will not work. If you look to ItemView its extends RelativeLayout and inflates the R.layout.item_view. After that it inflates each view as LayoutInflater.from(context).inflate(R.layout.item_view_children, this, true); – Spira Dec 27 '15 at 17:30
  • Because that's in his ItemView class, @Spira . He is supposed to put it in his ItemListFragment class. – Nicolas Zawada Dec 27 '15 at 17:37
  • Can you give an example of this? ItemListFragment class dosen't have a xml layout file. – Spira Dec 27 '15 at 22:31
  • Oh my bad, then simply put the xml for the FloatingActionButton in your acitivity layout file and call it in your MainActivity java class. That gives you the endresult you want, right? – Nicolas Zawada Dec 28 '15 at 11:52
  • No it dosen't. if you check the example code ListActivity is just using a fragment. My problem is that ItemView is used by ItemAdapter. So maybe somehow how to place to ItemListFragment the xml. But i am not so sure how to do it. ItemListFragment is calling setListAdapter which is using the ItemAdapter and then the ItemView. – Spira Dec 29 '15 at 20:02
0

Xamarin Android:

RX is useful here. You can subscribe to the TabSelected event for the TabLayout in your activity. In the example below, I have three tabs. For the first tab, I do not want to show the floating action button. However, for the other two, I display the floating action button.

var tabs = FindViewById<TabLayout>(Resource.Id.tabLayout);
tabs.SetupWithViewPager(viewPager);

Observable.FromEventPattern<EventHandler<TabLayout.TabSelectedEventArgs>, TabLayout.TabSelectedEventArgs>(
            x => tabs.TabSelected += x, x => tabs.TabSelected -= x)
        .Subscribe(s => 
        {
            var tabLayout = s.Sender as TabLayout;
            if (tabLayout.SelectedTabPosition == 0)
              FloatingActionButton.Hide();
            else
              FloatingActionButton.Show();

            viewPager.SetCurrentItem(tabLayout.SelectedTabPosition, true);
        });
AntAsc
  • 1
  • 1