Skip to content

Instantly share code, notes, and snippets.

@Cheesebaron
Created September 6, 2014 15:43
Show Gist options
  • Save Cheesebaron/7f58c1498a1687e440de to your computer and use it in GitHub Desktop.
Save Cheesebaron/7f58c1498a1687e440de to your computer and use it in GitHub Desktop.
Removing Fragments from ViewPager
public class DummyFragment : Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var view = inflater.Inflate(Resource.Layout.dummy_frag, container, false);
var tv = view.FindViewById<TextView>(Resource.Id.textView1);
tv.Text = "" + Arguments.GetInt("number", -1);
return view;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager" />
</LinearLayout>
[Activity(Label = "App2", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : FragmentActivity
{
private ViewPager _viewPager;
private ViewPagerAdapter _adapter;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
_viewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
_adapter = new ViewPagerAdapter(this, _viewPager, SupportFragmentManager);
_viewPager.Adapter = _adapter;
}
public override bool OnCreateOptionsMenu(IMenu menu)
{
base.OnCreateOptionsMenu(menu);
MenuInflater.Inflate(Resource.Menu.menu, menu);
return true;
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Resource.Id.action_add_fragment:
_adapter.AddFragment(typeof(DummyFragment));
break;
case Resource.Id.action_remove_fragment:
_adapter.RemoveFragment(_adapter.Count - 1);
break;
}
return base.OnOptionsItemSelected(item);
}
}
<?xml version="1.0" encoding="utf-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_add_fragment"
android:title="@string/action_add_fragment"
android:showAsAction="ifRoom|withText"/>
<item android:id="@+id/action_remove_fragment"
android:title="@string/action_remove_fragment"
android:showAsAction="ifRoom|withText"/>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="action_add_fragment">Add</string>
<string name="action_remove_fragment">Remove</string>
</resources>
public class ViewPagerAdapter : FragmentStatePagerAdapter
{
private class ViewPagerItem
{
public Type Type { get; set; }
public Fragment CachedFragment { get; set; }
}
private readonly Context _context;
private readonly ViewPager _viewPager;
private readonly Dictionary<int, ViewPagerItem> _fragments = new Dictionary<int, ViewPagerItem>();
public ViewPagerAdapter(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer) { }
public ViewPagerAdapter(Context context, ViewPager pager, FragmentManager fm)
: base(fm)
{
_context = context;
_viewPager = pager;
}
public override Fragment GetItem(int position)
{
if (!_fragments.ContainsKey(position)) return null;
var bundle = new Bundle();
bundle.PutInt("number", position);
_fragments[position].CachedFragment = Fragment.Instantiate(_context,
FragmentJavaName(_fragments[position].Type), bundle);
return _fragments[position].CachedFragment;
}
public override int Count
{
get { return _fragments.Count; }
}
public void AddFragment(Type fragType, int position = -1)
{
if (position < 0 && _fragments.Count == 0)
position = 0;
else if (position < 0 && _fragments.Count > 0)
position = _fragments.Count;
_fragments.Add(position, new ViewPagerItem
{
Type = fragType
});
NotifyDataSetChanged();
}
public void RemoveFragment(int position)
{
DestroyItem(null, position, _fragments[position].CachedFragment);
_fragments.Remove(position);
NotifyDataSetChanged();
_viewPager.SetCurrentItem(position - 1, false);
}
protected virtual string FragmentJavaName(Type fragmentType)
{
var namespaceText = fragmentType.Namespace ?? "";
if (namespaceText.Length > 0)
namespaceText = namespaceText.ToLowerInvariant() + ".";
return namespaceText + fragmentType.Name;
}
}
@alexrainman
Copy link

HI, trying to use this but it's not working. When i try to add a fragment i get error message saying that MyNameSpace.FragmentClassName can not be found. I replace this line _fragments[position].CartFragment = Fragment.Instantiate(_context, FragmentJavaName(_fragments[position].Type), bundle); by _fragments[position].CartFragment = CartFragment.NewInstance(); and it works, but then, remove doesn't works. The removed fragment still on screen. Thanks.

@alexrainman
Copy link

Found out why removing is not working, this is missing:

public override int GetItemPosition (Java.Lang.Object objectValue)
{
return PagerAdapter.PositionNone;
}

Still not finding solution for class not found, but it is working with my own line of code.

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment