Reputation: 63
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:
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();
}
Upvotes: 2
Views: 2621
Reputation: 1
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);
});
Upvotes: 0
Reputation: 63
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>
Upvotes: 1
Reputation: 507
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
}
});
Upvotes: 0