首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用BottomBar阻止碎片打开?

使用BottomBar阻止碎片打开?
EN

Stack Overflow用户
提问于 2016-08-05 11:44:26
回答 2查看 571关注 0票数 7

我正在使用Support Library添加一个与材料设计栏类似的底栏。底部的条工作得很好,但似乎如果我有条显示,如果我试图打开我的自定义适配器中的任何片段,片段没有open...or可能它打开我的主布局后面?我不知道该怎么解决这个问题。下面是我的代码。

我在网上看到了更多的帖子,我认为这与片段被正确加载有关,但在底部bar...and下面或旁边,这就是它不可见的原因吗?这一切为什么要发生?是因为最下面的栏有一个LinearLayout吗?我把它定义为一个菜单,所以我不确定我是否能控制它成为一个LinearLayout……

设置底部栏,此方法从我的activity的onCreate调用:

代码语言:javascript
复制
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);
    }

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

代码语言:javascript
复制
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()的调用,片段将打开fine...but,这意味着我没有底栏……

我遗漏了什么?必须有一种方法来使用底部栏并打开一个片段吗?

谢谢!

编辑:

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

代码语言:javascript
复制
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);

我的适配器:

代码语言:javascript
复制
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:

代码语言:javascript
复制
<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>

片段布局:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
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

当我使用底部栏时(不工作-没有打开任何片段,但显示了toast ):

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

EN

回答 2

Stack Overflow用户

发布于 2016-08-09 01:09:04

试着链接一张有问题并且没有问题的图片(没有底部栏),因为你使用的是merge,布局层次结构将根据你的activity's viewgroup(线性的,相对的)constraints(我们不知道它们是什么样子)来放置。

正如你所说的,当没有bottombar时,你的片段显示完美的,尽管当你的底部栏在那里时,问题统计数据,根据你的日志片段,表明你的片段正在完美加载,即使当bottombar可见时,意味着片段在那里,但不可见,看起来你的片段没有获得适当的空间来显示。

其他解决方案可以向片段添加底部条,而不是活动,以避免任何重叠,例如

代码语言:javascript
复制
mBottomBar.attach(findViewById(R.id.fragmentContainer), savedInstanceState);
票数 1
EN

Stack Overflow用户

发布于 2016-08-14 20:30:18

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

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

您所需要做的不是将BottomBar附加到您的活动,而是将它附加到包含您的内容的视图。例如,如果您的片段位于id为fragmentContainer的ViewGroup中,您可以这样做:mBottomBar.attach(findViewById(R.id.fragmentContainer), savedInstanceState);

因此,由于导航抽屉使用动画将片段移入和移出视图,因此当您将新片段添加到活动中时,同样的事情也会发生。

解决方案

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

代码语言:javascript
复制
mBottomBar.attach(findViewById(R.id.fragment_container), savedInstanceState);

如果上面的方法不起作用,试试这个

您需要做的是添加一个额外的FrameLayout来容纳您的底部栏,它有一个透明的背景,但在您的片段的顶部。

因此将您的main_activity布局更改为

代码语言:javascript
复制
<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,而是像这样将它附加到holder

代码语言:javascript
复制
mBottomBar.attach(findViewById(R.id.holder_bottombar), savedInstanceState);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38780747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档