HOW TO IMPLEMENT THIS FIXED TAB BETWEEN ACTIVITIES?

Asked

Viewed 53 times

-3

I’m developing an Android project, like a social network, I’m with the idea, it’s good, but I’m wanting to implement a function: A fixed menu, type, on the facebook app, has a fixed menu, News Feed, notifications, and friend requests. But I want to put mine: A feed, a gallery field, among others, but I do not know how to create this fixed menu tab, it’s just her, I know how to make the good animations.

1 answer

0


You will need to use tabs, look at the example below:

No build.Radle:

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:appcompat-v7:23.4.0'
  compile 'com.android.support:design:23.4.0'
}

In your Mainactivity:

public class MainActivity extends AppCompatActivity {

private TabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new 
 ViewPagerAdapter(getSupportFragmentManager());
    //Esses 3 fragments ainda iremos criar
    adapter.addFragment(new OneFragment(), "ONE");
    adapter.addFragment(new TwoFragment(), "TWO");
    adapter.addFragment(new ThreeFragment(), "THREE");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

}

Your xml:

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
</android.support.design.widget.CoordinatorLayout>

So you create your Fragments, in this example I will use 3 tabs, I will put the code of only one:

public class OneFragment extends Fragment {

public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_one, container, false);

    ((TextView) view.findViewById(R.id.fragmentText)).setText("One");

    return view;
}

}

Xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="danielrocha.androidtabs.OneFragment">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fragmentText"
    android:textSize="40dp"
    android:textStyle="bold"
    android:layout_centerInParent="true"/>

</RelativeLayout>

Repeat the code for the other two Fragments, and change the class name, of course.

That will be the result:

https://rochadaniel.github.io/static/img/posts/tabs/tabs1.png

To put icons you put this method tab in Mainactivity:

private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(R.drawable.seuicone);
    tabLayout.getTabAt(1).setIcon(R.drawable.seuicone);
    tabLayout.getTabAt(2).setIcon(R.drawable.seuicone);
}

In order for tabs to have only icons, inside Viewpageradapter you modify the getPageTitle() method, I return null:

@Override
public CharSequence getPageTitle(int position) {

// returno nulo
return null;
}

I think that’s it. Source: https://rochadaniel.github.io/android/2016/09/22/Android-Trabalhando-com-Tabs.html

  • Thanks friend, now I will study over the code, grate to understand rsrsrs, thank you very much

  • @Jirensama if you get there and want to mark my answer as accepted I thank kkkk

  • How to do it, I’m new to this site rsrsrs

  • @Jirensama Below the negatively evaluate button, it is type a V

  • I found rsrsrssrsr

  • @Jirensama thank you, have a great night

  • can I ask just one more question ? How did you manage to build these codes, you master, from a lot of work ? you became an expert as ? me a rsrsrs tip

  • @Jirensama man, this one I took an example of the link half q ready and added, I am not an expert but I master well, to learn only in practice, you have to go doing your projects, finding problems and solving, then you mature and learning from your mistakes. You can also take a course, I recommend looking in udemy.com, there you find very cheap courses

  • vlw, I’ll do it

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.