How to set the first view in the ViewPager? - view

I use ViewPager to switch several views.
public class MainA extends ActivityGroup {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
View view1 = inflater.inflate(R.layout.viewa,null );
View view2 = inflater.inflate(R.layout.viewp, null);
ImageView imageView = new ImageView(getApplicationContext());
imageView.setImageResource(R.drawable.ic_action_search);
views.add(imageView);
views.add(view1);
views.add(view2);}}
ViewPager will set imageView as first view. When I turn left,there is no view. When I turn right , view1 will come out.
Now I have one question. I want to set view1 as first view. When I turn left, imageView will come out. When I turn right, view2 will come out.
How to do that ???

If you have multiple view then better to use PagerAdapter to add all these view. And then set first view selection like this
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);`

Related

Xamarin (Visual Studio), View mapped MKMapView, not a control on view, will not show navigation bars added to view

I created a View and mapped it to a MKMapView via code, but I cannot seem to have the Navigation Bar show up (it's the 2nd view in the stack, so it should have some ability to show the bar).
The map creates well with all functionality I want, BUT, it takes up the entire view space on the View
public override void LoadView()
{
CoreGraphics.CGRect r = new Rectangle(0, 40, (int)UIScreen.MainScreen.Bounds.Right, (int)UIScreen.MainScreen.Bounds.Bottom);
map = new MKMapView(r);
View = map;
}
any ideas?
To show Navigation Bar , you need make sure that the RootViewController of the Application is an UINavigationController (not an UIViewController) .
in Appdeledate.cs
public bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
// Override point for customization after application launch.
// If not required for your application you can safely delete this method
Window = new UIWindow(UIScreen.MainScreen.Bounds);
Window.RootViewController = new UINavigationController(new YourViewController());
Window.MakeKeyAndVisible();
return true;
}
in SceneDelegate (if your app contains it)
public void WillConnect (UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions)
{
Window = new UIWindow(scene as UIWindowScene);
Window.RootViewController = new UINavigationController(new YourViewController()) ;
Window.MakeKeyAndVisible();
}
And when you navigate from the first page to the map page
NavigationController.PushViewController(new YourMapController(),true);

Xamarin memory leakage

I'm a newbie to Xamarin. I have created an application that uses DrawerLayout(Android). But my problem is that every time i select a item in the menu(DrawerLayout menu), the memory increases, and this causes the app to become slow and crush. I've tried to use Xamarin profiler to analyze memory leaks - it suspects something called String.FastAllocationString, but it doesn't really show the line(code) that causes String.FastAllocationString issue. Please help ? Here is my code :
MainActivity
DrawerLayout drawerLayout;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
// Init toolbar
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.app_bar);
SetSupportActionBar(toolbar);
SupportActionBar.SetTitle(Resource.String.app_name);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetDisplayShowHomeEnabled(true);
// Attach item selected handler to navigation view
var navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;
// Create ActionBarDrawerToggle button and add it to the toolbar
var drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, Resource.String.open_drawer, Resource.String.close_drawer);
drawerLayout.SetDrawerListener(drawerToggle);
drawerToggle.SyncState();
}
void NavigationView_NavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
var ft = FragmentManager.BeginTransaction();
ft.AddToBackStack(null);
switch (e.MenuItem.ItemId)
{
case (Resource.Id.nav_incidents):
SupportActionBar.SetTitle(Resource.String.toolbar_Test);
ft.Add(Resource.Id.HomeFrameLayout, new Test());
break;
}
ft.Commit();
ft.Dispose();
// Close drawer
drawerLayout.CloseDrawers();
}
Fragment
[Activity(Label = "Test")]
public class Test : Fragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate(Resource.Layout.Test, container, false);
return view;
}
}
Xamarin Profiler
you have to check fragment is available before you add new one
switch (e.MenuItem.ItemId)
{
case (Resource.Id.nav_incidents):
SupportActionBar.SetTitle(Resource.String.toolbar_Test);
Fragment myFragment =
(Fragment)FragmentManager.FindFragmentByTag("FRAGMENT1");
if (myFragment.IsVisible){
ft.Replace(Resource.Id.HomeFrameLayout, new Test(),"FRAGMENT1");
}else{
ft.Add(Resource.Id.HomeFrameLayout, new Test(),"FRAGMENT1");
}
break;
}
Hope this help

Valid way to hide QLPreviewController RightBarButtonItem/RightBarButtonItems Xamarin iOS

My app supports iOS8+ devices. I want to hide right Action button from navigation bar. By research I found following few workarounds:
1. Create Sub class of QLPreviewController and in ViewDidAppear SetRightBarButtonItems to zero.
public class PdfViewController : QLPreviewController
{
public override void ViewDidAppear (bool animated)
{
base.ViewDidAppear (animated);
NavigationItem.SetRightBarButtonItems (new UIKit.UIBarButtonItem[0], false);
}
}
In this case problem is RightBarButtonItem appears and then disappears. In mean while user is able to click on that RightBarButtonItem button. I don't want this behavior.
2. Create UIViewController and add QLPreviewController as child ViewController.
void BtnShowPdf_Clicked (object sender, EventArgs e) {
var dummyVC = new UIViewController ();
var pdfVC = new PdfViewController ();
dummyVC.AddChildViewController (pdfVC);
dummyVC.View.AddSubview (pdfVC.View);
dummyVC.NavigationItem.SetRightBarButtonItems (new UIBarButtonItem[0], false);
dummyVC.EdgesForExtendedLayout = UIRectEdge.None;
dummyVC.AutomaticallyAdjustsScrollViewInsets = false;
dummyVC.View.BackgroundColor = UIColor.Clear;
pdfVC.EdgesForExtendedLayout = UIRectEdge.None;
pdfVC.AutomaticallyAdjustsScrollViewInsets = false;
pdfVC.View.BackgroundColor = UIColor.Clear;
}
In this case If I set QLPreviewController it works as expected. But NavigationBar becomes more darker than default ViewController background color.
Dark Bar:
http://screencast.com/t/bqVMv5qqGz
Needed clear background bar like:
http://screencast.com/t/MUwE2VnxJ7
My questions are:
A) I would like to know which is the correct way to hide right
navigation bar button as per Apple guidelines ? If you have correct
solution then those are also appreciated.
B) Also Can you please suggest solution(s) for #1 Or #2 ?
Pretty sure you can do this:
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
self.navigationItem.rightBarButtonItems = nil
}

RecyclerView Click event

I have created a RecyclerView adapter and I'm trying to start an activity when a row is clicked:
public override OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
MyViewHolder viewHolder = (MyViewHolder)holder;
viewHolder.MyView.Click += (sender, e) =>
{
var context = viewHolder.MyView.Context;
var intent = new Intent(context, typeof(DetailActivity));
context.StartActivity(intent);
}
}
When I click the first row it will take me to the activity like I want. If I scroll down so that the first row is rebound and then scroll back to the top again and then click the first row then my Click event fires twice. Once for the first row that was bound and then again for a row that was bound when I scrolled.
Is there an event you need to handle to unregister the click events?
I believe the standard pattern is to setup your clickhandlers in the constructor of the ViewHolder. Then in OnBindViewHolder, you update the Views/Data inside the ViewHolder.
Something like this (not compiled code):
Adapter:
public override OnBindViewHolder()
{
MyViewHolder viewHolder = (MyViewHolder)holder;
viewHolder.SetData(whatever data you care about);
}
MyViewHolder:
public MyViewHolder(View view) : base(view)
{
MainView = view;
MainView.Click += (sender, e) =>
{
var context = MainView.Context;
var intent = new Intent(context, typeof(DetailActivity));
context.StartActivity(intent);
}
}
Doing it this way keeps the Adapter cleaner by putting business logic in the ViewHolder, and also prevents your click handlers from being constantly setup and torn down as you scroll.

Checked checkboxes is unchecked when i scroll listview in xamarin android

Below is my GetView() method in adapter class, when I scroll the list view by selecting one checkbox. After scrolling back to initial position checked checkbox is getting unchecked.
public override View GetView (int position, View convertView, ViewGroup parent)
{
View view = convertView;
var item = mMyList [position];
//View holder
MyViewHolder holder = null;
if (view == null) {
holder = new MyViewHolder ();
view = mcontext.LayoutInflater.Inflate (Resource.Layout.listview_layout, null);
holder.mChecked = view.FindViewById<CheckBox> (Resource.Id.chkBox);
holder.Name = view.FindViewById<TextView> (Resource.Id.Name);
holder.StartDate = view.FindViewById<TextView> (Resource.Id.startDate);
holder.EndDate = view.FindViewById<TextView> (Resource.Id.endDate);
holder.Desc = view.FindViewById<TextView> (Resource.Id.Desc);
view.SetTag (holder);
} else {
holder = (MyViewHolder)view.Tag;
}
holder.Name.SetText (item.Name, TextView.BufferType.Normal);
holder.StartDate.SetText (item.StartDate, TextView.BufferType.Normal);
holder.EndDate.SetText (item.EndDate, TextView.BufferType.Normal);
holder.Desc.SetText (item.Description, TextView.BufferType.Normal);
return view;
}
It's simple!
You just need to store this "states" of checkboxes,because everytime when you scroll your listview, GetView method will be called(to draw hided items, Android reuses rows).
In your DataContext, for e.g. List<MyClass> , where MyClass represents:
public class MyClass
{
public string Name {get;set;}
public string SecondName {get;set;}
public bool IsChecked {get;set;}
}
try to add bool property(in this case IsChecked) for state of Checkbox.
And after this,in your GetView method write something likes this:
holder.mChecked.Checked = MyList [position].#YourBoolProperty#;
Btw i just write that on the fly.
Also,if something isn't clear for you, try to check this or this.
Hope that helps!

Resources