Android实现面包屑效果,支持Fragment联动
使用方式
<com.tinytongtong.breadcrumbs.BreadCrumbsView
android:id="@+id/breadCrumbs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
breadCrumbsView.setOnTabListener(new BreadCrumbsView.OnTabListener() {
@Override
public void onAdded(BreadCrumbsView.Tab tab) {
Log.e("BreadCrumbsView", "BreadCrumbsView.OnTabListener#onAdded tab:" + tab.getIndex());
addFragment(tab);
}
@Override
public void onActivated(BreadCrumbsView.Tab tab) {
Log.e("BreadCrumbsView", "BreadCrumbsView.OnTabListener#onActivated tab:" + tab.getIndex());
}
@Override
public void onRemoved(BreadCrumbsView.Tab tab) {
Log.e("BreadCrumbsView", "BreadCrumbsView.OnTabListener#onRemoved tab:" + tab.getIndex());
removeLastFragment();
}
});
private void addFragment(BreadCrumbsView.Tab tab) {
//在这里添加你自己的Fragment,tab#getValue返回的是创建Tab时传入的数据,可以根据这些数据创建你自己想要的Fragment
Fragment fragment = BlankFragment.newInstance(String.format("我是第%d个Fragment", tab.getIndex()), "" + tab.getIndex());
getSupportFragmentManager()
.beginTransaction()
.add(R.id.container, fragment, String.valueOf(tab.getIndex()))
.show(fragment)
.addToBackStack(null)
.commit();
fragments.add(fragment);
}
/**
* 移除最后一个Fragment,显示倒数第二个Fragment
*/
private void removeLastFragment() {
if (fragments != null && fragments.size() > 1) {
getSupportFragmentManager().popBackStackImmediate();
fragments.removeLast();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.show(fragments.getLast())
.commit();
fragmentManager.executePendingTransactions();
}
}
源码地址:
评论