使用BottomBar可以防止碎片打开?

我正在使用支持库添加类似于材料设计的底栏。 底栏效果很好,但似乎如果我显示了条形图,如果我尝试从我的自定义适配器打开任何片段,片段不会打开…或者它可能会在我的主要布局后面打开? 我不知道如何解决这个问题。 以下是我的代码。

我已经在SO和网络上阅读了更多post,我认为这与片段正确加载有关,但在底栏下方或旁边……这就是为什么它不可见? 为什么会这样? 是因为底栏有LinearLayout吗? 我将它定义为一个菜单,所以我不确定我是否可以控制它是一个LinearLayout ….

设置底栏,从我的活动的onCreate调用此方法:

public void setupBottomToolbar(Bundle savedInstanceState) { mBottomBar = BottomBar.attach(MainActivity.this, savedInstanceState); mBottomBar.setItems(R.menu.bottombar_menu); mBottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() { @Override public void onMenuTabSelected(@IdRes int menuItemId) { if (menuItemId == R.id.toolbar_jobs) { } else if (menuItemId == R.id.toolbar_messages) { } else if (menuItemId == R.id.toolbar_recentJobs) { } else if (menuItemId == R.id.toolbar_employerPools) { } } @Override public void onMenuTabReSelected(@IdRes int menuItemId) { if (menuItemId == R.id.toolbar_jobs) { // The user reselected item number one, scroll your content to top. } else if (menuItemId == R.id.toolbar_messages) { } else if (menuItemId == R.id.toolbar_employerPools) { } else if (menuItemId == R.id.toolbar_recentJobs) { } } }); // Setting colors for different tabs when there's more than three of them. // You can set colors for tabs in three different ways as shown below. mBottomBar.getBar().setBackgroundColor(getResources().getColor(R.color.laborswipe_darkgray)); mBottomBar.setActiveTabColor(getResources().getColor(R.color.laborswipe_lightgray)); // Make a Badge for the second tab, with red background color and a value of "13". BottomBarBadge unreadMessages = mBottomBar.makeBadgeForTabAt(1, getResources().getColor(R.color.laborswipe_orange), 5); // Control the badge's visibility unreadMessages.show(); //unreadMessages.hide(); // Change the displayed count for this badge. //unreadMessages.setCount(4); // Change the show / hide animation duration. unreadMessages.setAnimationDuration(200); // If you want the badge be shown always after unselecting the tab that contains it. unreadMessages.setAutoShowAfterUnSelection(true); // If you don't want this badge to be hidden after selecting the tab contains it. unreadMessages.setAutoShowAfterUnSelection(false); } 

在我的适配器中,我试图在单击按钮时打开片段,如下所示:

 holder.desc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(context, "Open Description", Toast.LENGTH_SHORT).show(); JobDescFragment firstFragment = new JobDescFragment(); ((MainActivity)context).getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, firstFragment).commit(); } }); 

如果我在活动的onCreate中注释掉对setupBottomToolbar()的调用,片段打开很好……但这意味着我没有底栏…

我错过了什么? 必须有一种方法来使用底栏并打开一个片段?

谢谢!

编辑:

这是我活动的最重要部分。

 public class MainActivity extends AppCompatActivity { private ArrayList swipecardsList; private ArrayList jobList = new ArrayList(); private JobAdapter arrayAdapter; //arrayadapter private BottomBar mBottomBar; SharedPreferences settings; @InjectView(R.id.frame) SwipeFlingAdapterView flingContainer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Remove title bar //this.requestWindowFeature(Window.FEATURE_NO_TITLE); //color the notification bar with our company colors Window window = this.getWindow(); window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); window.setStatusBarColor(this.getResources().getColor(R.color.laborswipe_notificationbar)); //remove title from action bar and add the logo to the top left of the action bar setupTopToolbar(); setContentView(R.layout.activity_main); ButterKnife.inject(this); //set up the bottom toolbar using the roughike library to mimic android material design setupBottomToolbar(savedInstanceState); 

我的适配器:

 public class JobAdapter extends ArrayAdapter { private final Context context; private final ArrayList jobs; private final int layoutResourceId; private final SwipeFlingAdapterView flingContainer; private boolean isExpanded = false; public JobAdapter(Context context, int layoutResourceId, ArrayList jobs, SwipeFlingAdapterView flingContainer) { super(context, layoutResourceId, jobs); this.context = context; this.jobs = jobs; this.layoutResourceId = layoutResourceId; this.flingContainer = flingContainer; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view = convertView; final ViewHolder holder; String pay, hrs; final Bundle fragmentParams = new Bundle(); LayoutInflater inflater = LayoutInflater.from(context); if (view == null) { view = inflater.inflate(layoutResourceId, parent, false); holder = new ViewHolder(); holder.title = (TextView)view.findViewById(R.id.tv_jobTitle); holder.desc = (TextView) view.findViewById(R.id.tv_JobDesc); view.setTag(holder); } else { holder = (ViewHolder)view.getTag(); } Job j = jobs.get(position); holder.title.setText(j.getJobTitle()); holder.desc.setText(j.getDescription()); //when user clicks apply, swipe the card right holder.apply.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Open up a fragment to display the entire job description Toast.makeText(context, "Applied", Toast.LENGTH_SHORT).show(); flingContainer.getTopCardListener().selectRight(); } }); //when user clicks dismiss, swipe the card left holder.dismiss.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Open up a fragment to display the entire job description Toast.makeText(context, "Dismissed", Toast.LENGTH_SHORT).show(); flingContainer.getTopCardListener().selectLeft(); } }); //on click event listener for the job description field - open larger window to read description holder.desc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Toast.makeText(context, "Open Description", Toast.LENGTH_SHORT).show(); JobDescFragment firstFragment = new JobDescFragment(); Fragment frag = new Fragment(); frag = firstFragment.newJobDescFrag(j.getDescription()); ((MainActivity) context).getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, frag) .addToBackStack("JobDesc").commit(); } }); return view; } public class ViewHolder { TextView title; TextView payrate; TextView dateRange; TextView workinghrs; TextView location; TextView companyname; TextView desc; TextView experience; TextView equipment; Button apply, dismiss, expand; } } 

activity_main.xml中:

     

片段布局:

         

logcat的:

 08-07 11:20:47.799 13896-13896/com.lorentzos.swipecards.example I/System.out: DEBUG: job desc fragment loaded! 08-07 11:20:47.855 13896-13941/com.lorentzos.swipecards.example W/EGL_emulation: eglSurfaceAttrib not implemented 08-07 11:20:47.855 13896-13941/com.lorentzos.swipecards.example W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaa7f880, error=EGL_SUCCESS 08-07 11:20:48.002 13896-13941/com.lorentzos.swipecards.example V/RenderScript: 0xa1408000 Launching thread(s), CPUs 2 08-07 11:20:49.798 13896-13941/com.lorentzos.swipecards.example E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae433ca0 

当我使用底栏(不工作 – 没有打开碎片但显示吐司):

在此处输入图像描述

当我不使用底栏(打开workin-fragment,背景为绿色):

在此处输入图像描述

尝试链接问题的图片而没有问题(没有底栏),因为你使用merge ,布局层次结构将根据你的activity's viewgroup (线性,相对) constraints (我们不知道他们是什么样的)来裁员。

正如你所说,当没有底栏时 ,你的片段显示完美,虽然当它在那里的底栏,问题属性,根据你的登录片段表明你的片段正在加载完美,即使底栏是可见的,平均片段在那里,但是不可见,好像你的片段没有得到适当的空间来显示。

其他解决方案可以添加底部栏到您的片段而不是活动,以避免任何重叠,如

 mBottomBar.attach(findViewById(R.id.fragmentContainer), savedInstanceState); 

好吧,我认为解决方案应该很简单,从我在代码中看到的,你将BottomBar附加到你的活动中,我认为这是问题所在。 如果您在roughike / BottomBar github页面中阅读readme.md,您会发现这一点

为什么它与我的导航抽屉重叠?

您需要做的就是将BottomBar附加到您的Activity,而不是将其附加到包含您内容的视图。 例如,如果您的片段位于具有id fragmentContainer的ViewGroup中,您可以执行以下操作: mBottomBar.attach(findViewById(R.id.fragmentContainer), savedInstanceState);

因此,由于导航抽屉可以通过animation转换视图中的片段,因此在向活动添加新片段时也会发生同样的事情。

解决方案

从我在代码中看到的内容,您的片段容器ID是:活动布局中的fragment_container 。 因此,根据文档,您只需要将bottomBar附加到fragment_container而不是MainActivity.this

 mBottomBar.attach(findViewById(R.id.fragment_container), savedInstanceState); 

如果以上不起作用试试这个

你需要做的是添加一个额外的FrameLayout来保持你的底栏,它有一个透明的背景,但是在你的片段之上。

因此,将main_activity布局更改为

      

现在在代码中而不是将底栏连接到mainactivity,只需将它连接到支架即可

 mBottomBar.attach(findViewById(R.id.holder_bottombar), savedInstanceState);