مرجع تخصصی برنامه نویسان

انجمن تخصصی برنامه نویسان فارسی زبان

کاربر سایت

roozbehzamani

عضویت از 1395/08/08

قراردادن یک bottom bar داخل یک فرگمت (fragment) تب (tab fragment)

  • دوشنبه 14 اسفند 1396
  • 14:32
تشکر میکنم

سلام . من یدونه تب به صورت tab fragment ایجاد کردم و در داخل یکی از فرگمنت ها میخوام یدونه bottom bar قرار بدم . کدها هم ب صورت زیر نوشتم ولی از برنامه خارج میشه 

کدهای activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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/AppTheme.Base">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <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>

کدهای بخش fragment_first.xml

<FrameLayout
    android:id="@+id/frameLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.shm_rz.register_login_materialdesign.FirstFragment">

    <!-- TODO: Update blank fragment layout -->

    <com.roughike.bottombar.BottomBar
        android:id="@+id/bottomBar"
        android:layout_width="match_parent"
        android:layout_height="64dp"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="500dp"
        android:background="#FFFFFF"
        app:bb_activeTabColor="@color/colorAccent"
        app:bb_behavior="shifting"
        app:bb_inActiveTabAlpha="0.3"
        app:bb_inActiveTabColor="@color/colorAccent"
        app:bb_tabXmlResource="@xml/bottombar_tabs_five"
        app:bb_titleTextAppearance="@style/CustomTitleTextAppearance"
        app:bb_titleTypeFace="fonts/GreatVibes-Regular.otf" />

</FrameLayout>

کدهای FirstFragment.java

package com.shm_rz.register_login_materialdesign;


import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.roughike.bottombar.BottomBar;
import com.roughike.bottombar.OnTabSelectListener;


/**
 * A simple {@link Fragment} subclass.
 */
public class FirstFragment extends Fragment {

    First_Fragment_First fragment1;
    First_Fragment_Secend fragment2;
    First_Fragment_Third fragment3;
    First_Fragment_Four fragment4;
    First_Fragment_Five fragment5;
    ViewGroup frameLayout;
    public FirstFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        frameLayout = (ViewGroup) getView().findViewById(R.id.frameLayout);
        fragment1 = new First_Fragment_First();
        fragment2 = new First_Fragment_Secend();
        fragment3 = new First_Fragment_Third();
        fragment4 = new First_Fragment_Four();
        fragment5 = new First_Fragment_Five();

        BottomBar bottomBar = (BottomBar) getView().findViewById(R.id.bottomBar);

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.frameLayout, fragment1);
        transaction.commit();


        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();

                switch (tabId) {
                    case R.id.tab_recents:
                        transaction.replace(R.id.frameLayout, fragment1);
                        transaction.commit();
                        break;

                    case R.id.tab_favorites:
                        transaction.replace(R.id.frameLayout, fragment2);
                        transaction.commit();
                        break;

                    case R.id.tab_food:
                        transaction.replace(R.id.frameLayout, fragment3);
                        transaction.commit();
                        break;

                    case R.id.tab_friends:
                        transaction.replace(R.id.frameLayout, fragment4);
                        transaction.commit();
                        break;

                    case R.id.tab_nearby:
                        transaction.replace(R.id.frameLayout, fragment5);
                        transaction.commit();
                        break;
                }
            }
        });

        return inflater.inflate(R.layout.fragment_first, container, false);

    }

}

همچنین bottom bar که من استقاده کردم شامل پنج تب هست مه برای ان ها پنج fragment ایجاد کردم 

این هم کد های MainActivity.java ولی توش چیزی در مورد این ننوشتم ولی بازم خواستین نگاه کنین 

package com.shm_rz.register_login_materialdesign;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import com.mikepenz.materialdrawer.AccountHeader;
import com.mikepenz.materialdrawer.AccountHeaderBuilder;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.DividerDrawerItem;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
import com.mikepenz.materialdrawer.model.ProfileDrawerItem;
import com.mikepenz.materialdrawer.model.SecondaryDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;
import com.mikepenz.materialdrawer.model.interfaces.IProfile;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Roozbeh Zamani on 09/02/2018.
 */

public class MainActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    ViewGroup frameLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.astivity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        createNav();
        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());
        adapter.addFragment(new FirstFragment(), "فست فود");
        adapter.addFragment(new SecendFragment(), "رستوران");
        adapter.addFragment(new ThirdFragment(), "کافی شاپ");
        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);
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
Intent check_page=new Intent(MainActivity.this,check.class);
        //noinspection SimplifiableIfStatement
        switch (id) {
            case R.id.action_home:
                startActivity(check_page);
            return true;
            case R.id.action_menu_1:
                Toast.makeText(this, "Menu 1", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_menu_2:
                Toast.makeText(this, "Menu 2", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.action_menu_3:
                Toast.makeText(this, "Menu 3", Toast.LENGTH_SHORT).show();
                return true;

        }

        return super.onOptionsItemSelected(item);
    }




    private void createNav() {
        final PrimaryDrawerItem itemHome = new PrimaryDrawerItem().withIdentifier(1).
                withName(R.string.drawer_item_home).
                withSelectable(false)
                .withIcon(GoogleMaterial.Icon.gmd_home);

        final SecondaryDrawerItem itemSettings = new SecondaryDrawerItem().withIdentifier(2)
                .withName(R.string.drawer_item_settings)
                .withSelectable(false)
                .withIcon(GoogleMaterial.Icon.gmd_settings);


        final SecondaryDrawerItem itemSearch = new SecondaryDrawerItem().withIdentifier(3)
                .withName(R.string.drawer_item_search)
                .withSelectable(false)
                .withIcon(GoogleMaterial.Icon.gmd_search);


        AccountHeader headerResult = new AccountHeaderBuilder()
                .withActivity(this)
                .withHeaderBackground(R.drawable.header)
                .addProfiles(
                        new ProfileDrawerItem().withName("Shabnam Moazam").withEmail("Moazamshabnam@gmail.com").withIcon(getResources().getDrawable(R.drawable.profile))
                )
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
                        return false;
                    }
                })
                .build();
        new DrawerBuilder()
                .withActivity(this)
                .withToolbar(mToolbar)
                .withActionBarDrawerToggle(true)
                .withActionBarDrawerToggleAnimated(true)
                .withAccountHeader(headerResult)
                .addDrawerItems(
                        itemHome,
                        new DividerDrawerItem(),
                        itemSearch,
                        itemSettings
                )
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        if (drawerItem.equals(itemHome)) {
                            Toast.makeText(MainActivity.this, "Home!", Toast.LENGTH_SHORT).show();
                        }
                        if (drawerItem.getIdentifier() == itemSettings.getIdentifier()) {
                            Toast.makeText(MainActivity.this, "Settings", Toast.LENGTH_SHORT).show();
                        }
                        if (drawerItem.getIdentifier() == itemSearch.getIdentifier()) {
                            Toast.makeText(MainActivity.this, "Search", Toast.LENGTH_SHORT).show();
                        }
                        return true;
                    }
                })
                .withSelectedItem(-1)
                .build();


    }
}

ممنون میشم کمک کنین
در ضمن این چن تا xml و java قایل هاش رو هم میزارم خواستین نگا کنین.

پاسخ های این پرسش

تعداد پاسخ ها : 1 پاسخ
کاربر سایت

نرجس اسماعیلی

عضویت از 1393/01/20

  • دوشنبه 14 اسفند 1396
  • 15:49

سلام

خطا دارید که میندازه بیرون این لینک و ببینید

کاربرانی که از این پست تشکر کرده اند

هیچ کاربری تا کنون از این پست تشکر نکرده است

اگر نیاز به یک مشاور در زمینه طراحی سایت ، برنامه نویسی و بازاریابی الکترونیکی دارید

با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)