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

时间:2016-08-05 03:44:26

标签: java android android-layout android-fragments android-support-library

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

我已经在SO和网络上阅读了更多帖子,我认为这与片段正确加载有关,但在底栏下面或旁边......这就是为什么它不是可见?为什么会这样?是因为底栏有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<String> swipecardsList;
    private ArrayList<Job> jobList = new ArrayList<Job>();
    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<Job> {
    private final Context context;
    private final ArrayList<Job> jobs;
    private final int layoutResourceId;
    private final SwipeFlingAdapterView flingContainer;
    private boolean isExpanded = false;

    public JobAdapter(Context context, int layoutResourceId, ArrayList<Job> 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中:

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <com.lorentzos.flingswipe.SwipeFlingAdapterView
        android:id="@+id/frame"
        android:background="@color/laborswipe_lightgray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:context=".MainActivity"
        android:layout_gravity="top"   />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</merge>

片段布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".JobDescFragment">

    <LinearLayout
        android:id="@+id/outerDescriptionLayout"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_gravity="center_horizontal|top"
        android:orientation="vertical"
        android:background="@drawable/swipecard_shadow"
        android:gravity="top"
        android:layout_marginLeft="5dp">

        <LinearLayout
            android:id="@+id/DescriptionLayout"
            android:layout_width="match_parent"
            android:layout_height="400dp"
            android:layout_gravity="center_horizontal|top"
            android:orientation="vertical"
            android:weightSum="1"
            android:gravity="top"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="#00FF00"
            android:clickable="true">

            <TextView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:text="Detailed Description:"
                android:textColor="#000000"
                android:id="@+id/tv_title" />

            <TextView
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:text="THIS IS THE FULL DESCRIPTION"
                android:textColor="#000000"
                android:id="@+id/tv_fullDescription" />
        </LinearLayout>
    </LinearLayout>

</FrameLayout>

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

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

enter image description here

当我不使用底栏时(打开工作片段,背景为绿色):

enter image description here

2 个答案:

答案 0 :(得分:1)

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

正如你所说的没有底栏,你片段显示完全虽然当底栏在那里,问题统计,根据你的登录片段表明你的片段即使当底栏可见时,正在加载完全正常的片段在那里但是不可见,看起来你的片段没有得到适当的空间来显示。

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

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

答案 1 :(得分:0)

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

  

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

     

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

因此,由于导航抽屉通过动画转换视图和视图之外的片段,因此当您向活动添加新片段时也会发生同样的事情。

解决方案

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

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

如果上述方法无效,请尝试

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

所以将main_activity布局更改为

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <com.lorentzos.flingswipe.SwipeFlingAdapterView
        android:id="@+id/frame"
        android:background="@color/laborswipe_lightgray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:context=".MainActivity"
        android:layout_gravity="top"   />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/holder_bottombar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"/>

</merge>

现在在代码中而不是将底栏连接到mainactivity,只需将它连接到持有者就像这样

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