My Xamarin Android app uses fragments for each view rather than separate activities, and one of the fragments needs to contain a ViewPager for a photo slider.
From everything I've seen, the ViewPager has to bind to an adapter that uses fragments. This is problematic because this means I have a fragment within a fragment, which has to use the ChildFragmentManager, which isn't supported in versions prior to Android 4.2. My app needs to work on Android 4.0+, so I need guidance on how to work around this issue.
Some lines of thought I had...
Is there a way to bind some other kind of adapter to a ViewPager that doesn't use fragments? In my case all I need is to show an ImageView as each item, so a fragment seems overkill and obviously has the 4.2+ issue.
Is there some other control that I could use besides ViewPager that would give me the desired UX (list of photos sliding left/right) without relying on child fragments?
Is there a way to make ChildFragmentManager work with Android 4.0 and 4.1?
Current code to setup the ViewPager with only Android 4.2+ support:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dip"
android:id="#+id/property_details_image_gallery" />
private ViewPager gallery;
gallery = view.FindViewById<ViewPager> (Resource.Id.property_details_image_gallery);
gallery.Adapter = new ImagePagerAdapter (ChildFragmentManager, 0, facts.PhotoUris);
gallery.SetOnPageChangeListener (this);
public class ImagePagerAdapter : FragmentPagerAdapter
{
private int resourceId;
private List<string> photos;
public ImagePagerAdapter (FragmentManager fragmentManager, int resourceId, List<string> photos) : base(fragmentManager)
{
this.resourceId = resourceId;
this.photos = photos;
if (photos.Count == 0)
photos.Add (string.Empty); // If there are no photos, we add a single photo that will result in a 'noimage' photo
}
public override Fragment GetItem (int position)
{
// Instantiate a new fragment
ImageGalleryFragment fragment = new ImageGalleryFragment (photos[position], position);
return fragment;
}
public override long GetItemId (int position)
{
return position;
}
public override int Count {
get {
return photos.Count;
}
}
}
I just started using Xamarin 3 days ago, so please be gentle. :)
I was able to get this working by having ImagePagerAdapter inherit directly from PagerAdapter.
public class ImagePagerAdapter : PagerAdapter {
private List<string> data;
private Context context;
public ImagePagerAdapter(Context context, List<string> data) {
this.context = context;
this.data = data;
}
public override Java.Lang.Object InstantiateItem(View collection, int position) {
// Create new image view
var uri = data [position];
var imageView = new ImageView(context);
var defaultImage = context.Resources.GetDrawable (Resource.Drawable.noimage);
imageView.SetUrlDrawable (uri, defaultImage);
// Add image view to pager
var viewPager = collection as ViewPager;
viewPager.AddView(imageView);
return imageView;
}
public override void DestroyItem(View collection, int position, Java.Lang.Object view) {
var viewPager = collection as ViewPager;
viewPager.RemoveView(view as View);
}
public override bool IsViewFromObject(View view, Java.Lang.Object obj) {
return view == obj;
}
public override IParcelable SaveState() {
return null;
}
public override void StartUpdate(View arg0) {
}
public override void FinishUpdate(View arg0) {
}
public override int Count {
get {
return data != null ? data.Count : 0;
}
}
}
Related
i'm facing a small issue figuring out on the following -
I have a RecyclerView in my MainActivity, the RecyclerView has CardView within it.
The data displays on the MinActivity page, cuz the RecyclerView is in it.
Now, I want a new activity to pop-up once a CardView is clicked.
The new activity will use the same data as the CardView had.
I'm using Firebase storage if it matters.
Example -
My CardView 1 has a name, age and country - Jake, 19, UK.
The other activity will get the collection data from Firebase which is the exact same as CardView 1 and implement it in to the new activity.
I hope I explained it well..
(I'm using getters and setters)
My code so far (Only the needed parts)-
MainActivity
list_post = new ArrayList<>();
list_header = new ArrayList<>();
postsAdapter = new PostsAdapter(list_post);
headerAdapter = new HeaderAdapter(list_header);
headerRecycler = (RecyclerView) findViewById(R.id.headerRecycler);
headerRecycler.setAdapter(headerAdapter);
headerRecycler.setLayoutManager(new LinearLayoutManager(this));
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection("FeaturedPosts").limit(1).addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(QuerySnapshot queryDocumentSnapshotss, FirebaseFirestoreException ee) {
for (DocumentChange doc2: queryDocumentSnapshotss.getDocumentChanges()) {
if (doc2.getType() == DocumentChange.Type.ADDED) {
ListForHeader listForHeader = doc2.getDocument().toObject(ListForHeader.class);
list_header.add(listForHeader);
headerAdapter.notifyDataSetChanged();
}
}
}
});
RecyclerAdapter
public class HeaderAdapter extends RecyclerView.Adapter<HeaderAdapter.ViewHolder2> {
public List<ListForHeader> list_header;
public Context contextt;
public HeaderAdapter (List<ListForHeader> list_header) {
this.list_header = list_header;
}
#NonNull
#Override
public HeaderAdapter.ViewHolder2 onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.featured_activity, parent, false);
contextt = parent.getContext();
return new HeaderAdapter.ViewHolder2(view);
}
#Override
public void onBindViewHolder(#NonNull HeaderAdapter.ViewHolder2 holder, int position) {
String header_data2 = list_header.get(position).getHeader2();
holder.setHeaderText2(header_data2);
String date_data2 = list_header.get(position).getDate2();
holder.setDateText2(date_data2);
String image_data2 = list_header.get(position).getImage_url2();
holder.setIntroIMG2(image_data2);
}
#Override
public int getItemCount() {
return list_header.size();
}
public class ViewHolder2 extends RecyclerView.ViewHolder {
private View mView;
private ImageView introIMG2;
private TextView headerText2;
private TextView dateText2;
public ViewHolder2(View itemView) {
super(itemView);
mView = itemView;
}
public void setHeaderText2(String headText2) {
headerText2 = mView.findViewById(R.id.introHeader2);
headerText2.setText(headText2);
}
public void setDateText2(String tarihText2) {
dateText2 = mView.findViewById(R.id.introDate2);
dateText2.setText(tarihText2);
}
public void setIntroIMG2 (String downloadUri) {
introIMG2 = (ImageView) mView.findViewById(R.id.introImage2);
Glide.with(contextt).load(downloadUri).into(introIMG2);
}
}
}
As far as I see its simple issue. You can add cardview Id to viewHolder and set onlclicklister to it in bindview and in on click Start activity and pass the current data in Intent. Let me know if its something else stopping you from sending data or getting click on cardview.
Extra: you may have to set cardview clickable true.
here is the example.
class DemoViewHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
#BindView(R.id.card)
CardView cardView;
public DemoViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
cardView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.card:
doSomethingOn(mList.get(getAdapterPosition()))
break;
}
}
public class BookingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context mContext;
ArrayList<Booking> mList;
public BookingAdapter(Context context, ArrayList<Booking> list) {
mContext = context;
mList = list;
this.cancelBookingListener = cancelBookingListener;
}
I am trying to implement tabs through MvvmCross in Xamarin. I came across MvxTabActivity in Android and MvxTabBarViewController in IOS. Both are working well. The problem is MvxTabActivity is obselete. Are there any alternatives for MvxTabActivity?
I found another way to implement this, which uses TabLayout and a ViewPager.
The solution asks to use fragments within a fragment. I have pasted the code for this approach. The problem here is on swiping the tabs, all the data in previous tabs is lost.
I tried using RetainInstance = true, that gave following exception : "Can't retain fragements that are nested in other fragments."
Product Detail Activity :
[Activity(Label = "ProductDetailView")]
public class ProductDetailView : MvxAppCompatActivity<ProductDetailViewModel>
{
private FrameLayout _mainFrame;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.product_detail_view);
if (FindViewById<FrameLayout>(Resource.Id.frame_Detail) != null)
{
var frag = new NutritionCategoryView();
frag.ViewModel = ViewModel.NutritionCategoryModel;
var trans = SupportFragmentManager.BeginTransaction();
trans.Replace(Resource.Id.frame_Detail, frag);
trans.AddToBackStack(null);
trans.Commit();
}
}
}
Nutrition Category View Fragment :
public class NutritionCategoryView : MvxFragment
{
public NutritionCategoryViewModel vm
{
get { return (NutritionCategoryViewModel) ViewModel; }
}
private TabLayout _tablayout;
private ViewPager _viewPager;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
this.EnsureBindingContextIsSet(inflater);
var view = this.BindingInflate(Resource.Layout.nutrition_category_view, container, false);
SetViewPager(view);
return view;
}
private void SetViewPager(View view)
{
_viewPager = view.FindViewById<Android.Support.V4.View.ViewPager>(Resource.Id.viewpager);
if (_viewPager != null)
{
var fragments = new List<CategoryTabsAdapter.FragmentInfo>
{
new CategoryTabsAdapter.FragmentInfo
{
FragmentType = typeof(CategoryView),
Title = "Proximates",
ViewModel = vm.Category1
},
new CategoryTabsAdapter.FragmentInfo
{
FragmentType = typeof(CategoryView),
Title = "Minerals",
ViewModel = vm.Category2
},
new CategoryTabsAdapter.FragmentInfo
{
FragmentType = typeof(CategoryView),
Title = "Fats",
ViewModel = vm.Category3
},
new CategoryTabsAdapter.FragmentInfo
{
FragmentType = typeof(CategoryView),
Title = "Vitamins",
ViewModel = vm.Category4
}
};
_viewPager.Adapter = new CategoryTabsAdapter(Activity, ChildFragmentManager, fragments);
}
_tablayout = view.FindViewById<TabLayout>(Resource.Id.sliding_tabs);
_tablayout.SetBackgroundColor(Android.Graphics.Color.Black);
_tablayout.SetupWithViewPager(_viewPager);
}
}
Category Tabs Adapter :
public class CategoryTabsAdapter : FragmentStatePagerAdapter
{
private readonly Context _context;
public IEnumerable<FragmentInfo> Fragments { get; private set; }
public CategoryTabsAdapter(Context context, FragmentManager fragmentManager, IEnumerable<FragmentInfo> fragments) : base(fragmentManager)
{
_context = context;
Fragments = fragments;
}
public override int Count
{
get { return Fragments.Count(); }
}
public override Fragment GetItem(int position)
{
var fragmentInfo = Fragments.ElementAt(position);
var fragment = Fragment.Instantiate(_context, Java.Lang.Class.FromType(fragmentInfo.FragmentType).Name);
((MvxFragment)fragment).ViewModel = fragmentInfo.ViewModel;
return fragment;
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return new Java.Lang.String(Fragments.ElementAt(position).Title);
}
public class FragmentInfo
{
public string Title { get; set; }
public Type FragmentType { get; set; }
public IMvxViewModel ViewModel { get; set; }
}
}
Category View Fragment
public class CategoryView : MvxFragment<CategoryViewModel>
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
this.EnsureBindingContextIsSet(inflater);
var view = this.BindingInflate(Resource.Layout.category_view, container, false);
//Exception caused here :
//RetainInstance = true;
return view;
}
}
I am new to Xamarin and MvvmCross, so could come up with this much research only. Any solution for either approaches would be of great help.
P.S. This is my first question on Stackoverflow.
I think you need to use the MvxCachingFragmentStatePagerAdapter which is in the MvvmCross.Droid.Support.V4 Nuget package. Then hook it up to your TabLayout with SetupWithViewPager().
var viewPager = view.FindViewById<ViewPager>(Resource.Id.viewpager);
if (viewPager != null)
{
var fragments = new List<MvxCachingFragmentStatePagerAdapter.FragmentInfo>
{
new MvxCachingFragmentStatePagerAdapter.FragmentInfo(
"TitleA",
typeof (YourFragmentA),
typeof (YourViewModelA)),
new MvxCachingFragmentStatePagerAdapter.FragmentInfo(
"TitleB",
typeof (YourFragmentB),
typeof (YourViewModelB)),
new MvxCachingFragmentStatePagerAdapter.FragmentInfo(
"TitleC",
typeof (YourFragmentC),
typeof (YourViewModelC))
};
viewPager.Adapter = new MvxCachingFragmentStatePagerAdapter(Activity, ChildFragmentManager, fragments);
viewPager.OffscreenPageLimit = fragments.Count;
var tabLayout = view.FindViewById<TabLayout>(Resource.Id.tabs);
tabLayout.SetupWithViewPager(viewPager);
}
Your tabs are being recreated each time because you are using FragmentStatePagerAdapter instead of FragmentPagerAdapter. From the docs, FragmentPagerAdapter:
This version of the pager is best for use when there are a handful of
typically more static fragments to be paged through, such as a set of
tabs. The fragment of each page the user visits will be kept in
memory, though its view hierarchy may be destroyed when not visible.
This can result in using a significant amount of memory since fragment
instances can hold on to an arbitrary amount of state. For larger sets
of pages, consider FragmentStatePagerAdapter.
FragmentStatePagerAdapter:
This version of the pager is more useful when there are a large number
of pages, working more like a list view. When pages are not visible to
the user, their entire fragment may be destroyed, only keeping the
saved state of that fragment. This allows the pager to hold on to much
less memory associated with each visited page as compared to
FragmentPagerAdapter at the cost of potentially more overhead when
switching between pages.
So use FragmentPagerAdapter, but I think it will have to be at the Activity level, and not a fragment within a fragment.
I want to change text size in ListView in different fragment class. I declared the variable in other class as
public static TextView title;
then I change the value of that textsize by using class fragment.
title.setTextSize(30);
when I click
back to other activity it doesn't work.
Here is my code:
public class Setting extends AppCompatActivity {
private RadioGroup Text_Size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
Text_Size = (RadioGroup) findViewById(R.id.radiogroup);
Text_Size.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.small:
HotNewsFrag.title.setTextSize(15);
break;
case R.id.medium:
HotNewsFrag.title.setTextSize(20);
break;
case R.id.big:
HotNewsFrag.title.setTextSize(30);
break;
default:
break;
}
}
});
LinearLayout back = (LinearLayout) findViewById(R.id.back1);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent setting = new Intent(Setting.this, MainActivity.class);
startActivity(setting);
}
});
}
}
Here is my fragment:
public class HotNewsFrag extends Fragment {
private View view;
private ListView listOfNews;
ListViewAdapter adapter;
ArrayList<HotNews> arraylist1;
public static String newsid;
public static TextView title;
}
The way you are handling this is completely wrong.
BIG PROBLEM:
defining the text View as Static would cause memory leak and prevent your view and activity from being garbage collected.
here are two approaches :
FIRST : SHARED PREFERENCES AND ONRESUME
you can store the text size in a sharedPreference and in your activity or fragments, you override onResume and set text size to our textsview again by the value in sharedPreference which you stored before
SECOND : BROADCAST RECEIVERS
You can use broadcast receivers and register them wherever you like. whenever the user change the text size, you send local broadcast and change text size wherever you register that.
Text_Size.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int txtSize=0;
switch (checkedId)
{
case R.id.small:
txtSize=15;
break;
case R.id.medium:
txtSize=20;
break;
case R.id.big:
txtSize=30;
break;
default:
break;
}
Setting.this.getSharedPreferences("MY_SHARED",0).edit().putInt("FONT_SIZE",txtSize).commit();
}
});
In your fragment
#Override
public void onResume() {
super.onResume();
title.setTextSize(getActivity().getSharedPreferences("MY_SHARED",0).getInt("FONT_SIZE",20));
ADAPTER_OF_YOUR_LISTVIEW.notifyAll();
}
If you just want the Text to become Emphasized, Try using SetTypeFace.BOLD
I have three fragments in view pager adapter. I can swipe left to right or right to left and get those fragments displayed. But, I want to prevent user from swiping back once he gets to the third fragment. Like, he should not be able to view previous two fragments once he opens the third fragment
public class PagerAdapter extends FragmentPagerAdapter {
FragmentManager mFragmentManager;
private ArrayList<View> views = new ArrayList<View>();
public PagerAdapter(FragmentManager fm) {
super(fm);
mFragmentManager = fm;
}
#Override
public Fragment getItem(int i) {
switch (i)
{
case 0 :
return new FragmentOne();
case 1 :
return new FragmentTwo();
case 2 :
return new FragmentThree();
default :
break;
}
return null;
}
#Override
public int getCount() {
return 3;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View v = views.get (position);
container.addView (v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(views.get(0));
}
}
Yes, you should be able. Checkout android developers docs.
FragmentPagerAdapter
public void destroyItem (ViewGroup container, int position, Object object)
Remove a page for the given position. The adapter is responsible for removing the view from its container, although it only must ensure this is done by the time it returns from finishUpdate(ViewGroup).
Currently, the project in question is set up using a ViewPager which has an adapter capable of loading 4 different fragments. The last 3 Fragments are dependent upon the first fragment giving them a value (the SQL foreign key comes from the a value from the first item in the ViewPager).
My question is: how do I retrieve a value from the first Fragment's EditText, perform an SQL Operation and pass a value once the user attempts to "swipe" to the next tab?
Note, that I am trying to do this on swipe, and not via button. So far I have attempted to override OnPageScrolled, which yielded a null pointer when referring to the first fragment. I am assuming that the first fragment has been destroyed when I attempt to call it's hasPopulatedClassName() function
For Reference, the offending activity. Note the comment under OnPageScrolled
public class ActivityClassEdit extends FragmentActivity implements ViewPager.
private ClassEditPagerAdapter aPager;
//Views
private ViewPager vPager;
//Boolean telling us if we are good to move on to Criteria, People, or Links
private boolean mClassInserted;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_class_edit);
if(getIntent().hasExtra(CM.BKEY_PASSED_SEMESTERKEY)){
//Set the views
vPager=(ViewPager) findViewById(R.id.ace_pager);
//Instantiate the pager and set it
aPager=new ClassEditPagerAdapter(this, getIntent().getExtras().getLong(CM.BKEY_PASSED_SEMESTERKEY, -1));
vPager.setAdapter(aPager);
vPager.setOnPageChangeListener(this);
} else {
//TODO handle not having a semester key
}
}
//=============PageScrollListener==============
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if(!mClassInserted) {
if (position == 1) {
//FragmentClassEdit fCE = (FragmentClassEdit) aPager.getRegisteredFragment(0);
FragmentClassEdit fCE = (FragmentClassEdit) aPager.getRegisteredFragment(0);
if (fCE.hasPopulatedClassName() == false) {
vPager.setCurrentItem(0);
Toast.makeText(this, getResources().getString(R.string.toast_typeInClassName), Toast.LENGTH_LONG).show();
} else {
//Save to shared Preferences, in case we have to delete on user cancellation
//null pointer here!!!
long mInsertedClassID= Long.getLong(fCE.insertClass());
getSharedPreferences(CM.SP, Activity.MODE_PRIVATE).edit().putLong(CM.SP_LASTINSERTEDCLASS, mInsertedClassID).apply();
aPager.setClassID(mInsertedClassID);
vPager.setCurrentItem(position);
}
}
} else {
vPager.setCurrentItem(position);
}
}
And here is the ViewPagerAdapter:
public class ClassEditPagerAdapter extends FragmentStatePagerAdapter{
private FragmentClassEdit fCE;
private Fragment fCrit;
private Fragment fLinks;
private Fragment fPeople;
private FragmentManager fragmentManager;
private String[] sTabText;
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
private Context ctx;
private long semID;
private long cID;
public ClassEditPagerAdapter(FragmentActivity context, long semesterID){
super(context.getSupportFragmentManager());
fragmentManager=context.getSupportFragmentManager();
ctx= context;
sTabText = ctx.getResources().getStringArray(R.array.cepa_titles);
cID=-1;
semID=semesterID;
}
public void setClassID(long classID){
cID=classID;
}
#Override
public Fragment getItem(int fragID) {
switch (fragID) {
//FragClassEdit
case 0:
fCE = FragmentClassEdit.newInstance(semID);
return fCE;
//FragTypeEdit
case 1:
fCrit = FragmentCriteriaEdit.newInstance(cID);
return fCrit;
//FragPeople
case 2:
fPeople = FragmentPeopleEdit.newInstance(cID);
return fPeople;
//FragLinks
case 3:
fLinks = FragmentLinkEdit.newInstance(cID);
return fLinks;
}
return null;
}
#Override
public Object instantiateItem(ViewGroup container, int position){
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
#Override
public int getCount() {
return 4;
}
#Override
public String getPageTitle(int position){
return sTabText[position];
}
}