如何将片段放在来自库的“底部导航栏”中

时间:2019-05-21 05:43:37

标签: android android-fragments bottomnavigationview

我想将fragments放在这个来自库的bottom navigation bar中,而不是如何放置它们,因为它是外部的,我希望将该设计保留在我的应用程序中。

我要寻找的是该方法类似于将fragments与Android Studio的bottom navigation bar一起放置。

塞古尔·萨尔瓦多({3}}

我想使用这种类型的设计。

https://github.com/gauravk95/bubble-navigation

这是代码。

MainActivity.java

 public class MainActivity extends AppCompatActivity {

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

    ArrayList<ScreenSlidePageFragment> fragList = new ArrayList<>();



    fragList.add(ScreenSlidePageFragment.newInstance(getString(R.string.home), R.color.red_inactive));
    fragList.add(ScreenSlidePageFragment.newInstance(getString(R.string.search), R.color.blue_inactive));
    fragList.add(ScreenSlidePageFragment.newInstance(getString(R.string.likes), R.color.blue_grey_inactive));
    fragList.add(ScreenSlidePageFragment.newInstance(getString(R.string.notification), R.color.green_inactive));
    fragList.add(ScreenSlidePageFragment.newInstance(getString(R.string.profile), R.color.purple_inactive));
    ScreenSlidePagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(fragList, getSupportFragmentManager());

    final BubbleNavigationLinearView bubbleNavigationLinearView = findViewById(R.id.bottom_navigation_view_linear);
    bubbleNavigationLinearView.setTypeface(Typeface.createFromAsset(getAssets(), "rubik.ttf"));




    bubbleNavigationLinearView.setBadgeValue(0, null);
    bubbleNavigationLinearView.setBadgeValue(1, null); //invisible badge
    bubbleNavigationLinearView.setBadgeValue(2, null);
    bubbleNavigationLinearView.setBadgeValue(3, null);
    bubbleNavigationLinearView.setBadgeValue(4, null); //empty badge

    bubbleNavigationLinearView.setNavigationChangeListener(new BubbleNavigationChangeListener() {
        @Override
        public void onNavigationChanged(View view, int position) {



        }
    });



    final ViewPager viewPager = findViewById(R.id.view_pager);
    viewPager.setAdapter(pagerAdapter);
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float v, int i1) {
        }

        @Override
        public void onPageSelected(int i) {
            bubbleNavigationLinearView.setCurrentActiveItem(i);
        }

        @Override
        public void onPageScrollStateChanged(int i) {

        }
    });

}

}

ScreenSlidePagerAdapter.kt

       class ScreenSlidePagerAdapter(
    private val fragmentList: ArrayList<ScreenSlidePageFragment>,
    fm: FragmentManager
          ) : FragmentStatePagerAdapter(fm) {

override fun getCount(): Int = fragmentList.size

override fun getItem(position: Int): Fragment {
    if (position >= 0 && position < fragmentList.size)
        return fragmentList[position]
    return ScreenSlidePageFragment()
}
}

ScreenSlidePageFragment.kt

private const val ARG_TITLE = "arg_title"
private const val ARG_BG_COLOR = "arg_bg_color"


class ScreenSlidePageFragment : Fragment() {
private var title: String? = "Default title."
private var bgColorResId: Int = R.color.blue_inactive

private lateinit var inflatedView: View

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {
        title = it.getString(ARG_TITLE)
        bgColorResId = it.getInt(ARG_BG_COLOR)
    }
}

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    inflatedView = inflater.inflate(R.layout.fragment_screen_slide_page, container, false)
    return inflatedView
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    inflatedView.setBackgroundColor(ContextCompat.getColor(context!!, bgColorResId))
   // screen_slide_title.text = title
}

companion object {
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param title Title parameter
     * @param bgColorId Background Color
     * @return A new instance of fragment ScreenSlidePageFragment.
     */
    @JvmStatic
    fun newInstance(title: String, bgColorId: Int) =
            ScreenSlidePageFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_TITLE, title)
                    putInt(ARG_BG_COLOR, bgColorId)
                }
            }
}
}

0 个答案:

没有答案