Forum Xamarin.Android

Add button on Sliding Tab Layout

monemdcmonemdc Member ✭✭✭

hi
I want to add a button inside the tab bag.
I made the following attempt, but the button appeared with all tabs and not a specific tab. In addition to stopping the application when adding a pressure event to the button.

FragmentLayout1.axml
`
<?xml version="1.0" encoding="utf-8"?>

  <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:scrollbars="vertical"
    android:layout_width="fill_parent"
    android:layout_height="341.0dp" />
<FrameLayout
    android:id="@+id/buttonPanel"
    android:layout_width="162.5dp"
    android:layout_height="50dp"
    android:layout_gravity="top|end"
    android:layout_marginEnd="0dp"
    android:layout_marginRight="0dp">
    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minWidth="25px"
        android:minHeight="25px"
        android:id="@+id/button1" />

</FrameLayout>


`

Tagged:

Best Answer

  • jezhjezh Xamurai
    Accepted Answer

    From your code,we could know there are three Fragment pages

         var fragments = new V4Fragment[]
                {
                    new HomeFragment(),
                    new PerformanceFragment(),
                    new CollectionFragment()
                };
    

    And we also find that the three Fragment pages Inflate the same layout HomeFragmentLayout

      var view = inflater.Inflate(Resource.Layout.HomeFragmentLayout, container, false);
    

    So if you add a Button in HomeFragmentLayout.xml,then it will been show in all three Fragment pages .

    According to your layout, you just need to load a different layout for each fragment page.

    PerformanceFragment .cs

    public class PerformanceFragment : Android.Support.V4.App.Fragment
    {
       // other code
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // var view = inflater.Inflate(Resource.Layout.HomeFragmentLayout, container, false);
            var view = inflater.Inflate(Resource.Layout.PerformanceFragmentLayout, container, false);
    
            var mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerView2);
    
         // other code
    
         }
    }
    

    CollectionFragment .cs

       public class CollectionFragment : Android.Support.V4.App.Fragment
        {
    
          // other code
            public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                var view = inflater.Inflate(Resource.Layout.CollectionFragmentLayout, container, false);
    
                var mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerView3);
    
                mRecyclerView.HasFixedSize = true;
    
                var mLayoutManager = new LinearLayoutManager(view.Context);
                mRecyclerView.SetLayoutManager(mLayoutManager);
    
    
             //   BUTTONA.Visibility = ViewStates.Invisible;
                int[] arr1 = { 4, 3, 2, 1 };
                var mAdapter = new MyRecyclerViewAdapter(arr1);
                mAdapter.ItemClick += OnItemClick;
    
                mRecyclerView.SetAdapter(mAdapter);
    
                return view;
            }
        }
    

    Note:

    Please pay attention to the RecyclerView id of the three diffferent layouts , each page should reference the Id of the corresponding control within the page layout.

    You can check the modified sample in the attachment.

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    We couldn't reproduce this question, could you please post a basic demo so that we can try to reproduce this question on our side?

  • monemdcmonemdc Member ✭✭✭

    HI
    This code is a complete example.
    What I want to do is place a button inside one Fragment tab only, and put Text on the other Fragment tab only.
    And the last Fragment tab is empty.
    In this way I will learn about the work mechanism and add the tools I want.

  • monemdcmonemdc Member ✭✭✭

    HI
    This code is a complete example.
    What I want to do is place a button inside one Fragment tab only, and put Text on the other Fragment tab only.
    And the last Fragment tab is empty.
    In this way I will learn about the work mechanism and add the tools I want.

    https://drive.google.com/file/d/14AeMCfYk8KrcDWmbZNFwKt2YAnfUsYDs/view?usp=sharing

  • jezhjezh Member, Xamarin Team Xamurai
    Accepted Answer

    From your code,we could know there are three Fragment pages

         var fragments = new V4Fragment[]
                {
                    new HomeFragment(),
                    new PerformanceFragment(),
                    new CollectionFragment()
                };
    

    And we also find that the three Fragment pages Inflate the same layout HomeFragmentLayout

      var view = inflater.Inflate(Resource.Layout.HomeFragmentLayout, container, false);
    

    So if you add a Button in HomeFragmentLayout.xml,then it will been show in all three Fragment pages .

    According to your layout, you just need to load a different layout for each fragment page.

    PerformanceFragment .cs

    public class PerformanceFragment : Android.Support.V4.App.Fragment
    {
       // other code
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // var view = inflater.Inflate(Resource.Layout.HomeFragmentLayout, container, false);
            var view = inflater.Inflate(Resource.Layout.PerformanceFragmentLayout, container, false);
    
            var mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerView2);
    
         // other code
    
         }
    }
    

    CollectionFragment .cs

       public class CollectionFragment : Android.Support.V4.App.Fragment
        {
    
          // other code
            public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                var view = inflater.Inflate(Resource.Layout.CollectionFragmentLayout, container, false);
    
                var mRecyclerView = view.FindViewById<RecyclerView>(Resource.Id.recyclerView3);
    
                mRecyclerView.HasFixedSize = true;
    
                var mLayoutManager = new LinearLayoutManager(view.Context);
                mRecyclerView.SetLayoutManager(mLayoutManager);
    
    
             //   BUTTONA.Visibility = ViewStates.Invisible;
                int[] arr1 = { 4, 3, 2, 1 };
                var mAdapter = new MyRecyclerViewAdapter(arr1);
                mAdapter.ItemClick += OnItemClick;
    
                mRecyclerView.SetAdapter(mAdapter);
    
                return view;
            }
        }
    

    Note:

    Please pay attention to the RecyclerView id of the three diffferent layouts , each page should reference the Id of the corresponding control within the page layout.

    You can check the modified sample in the attachment.

  • monemdcmonemdc Member ✭✭✭

    thanks

    jezh

  • jezhjezh Member, Xamarin Team Xamurai

    It's my pleasure to help you. Have a nice day! :)

  • monemdcmonemdc Member ✭✭✭

    @jezh said:
    It's my pleasure to help you. Have a nice day! :)

    It is a pleasure for me to answer your question, and I apologize for the simplicity of my questions. I am still a new student in xammarn

  • jezhjezh Member, Xamarin Team Xamurai

    We are always trying to learn new knowledge. Come on, man. :)
    If you have any problem about Xamarin, please feel free to contact us. <3

Sign In or Register to comment.