打开活动而不会消失底栏

时间:2016-05-13 21:59:22

标签: android

根据新的Bottom Bar解决方案,我无法找到实现这样的目标的想法:

Gif with new Activity

据我所知,点击图片后新的活动正在打开,但如何在不消失Bottom Bar的情况下打开它?

编辑

我说的是“音乐”标签用户点击Kodaline专辑的示例,而不是显示类似新活动的内容。

2 个答案:

答案 0 :(得分:1)

未启动新活动,否则底部导航栏将暂时消失。基本思想是让一个活动根据选择的项目将视图或片段扩展到容器中。我推荐使用片段视图,因为某些FragmentTransaction错误特别难以追踪,当您拥有定义自定义视图的完全控制权时,您将受到FragmentManager的支配。示例gif将底部导航的可见性设置为View.GONE,并在滚动期间激活底部导航。

<强> MainActivity.java

public class MainActivity extends Activity implements
YourBottomNavView.OnItemClickListener {

    ViewGroup viewContainer;
    YourBottomNavView bottomNav;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewContainer = findViewById(R.id.view_container);
        bottomNav = findViewById(R.id.bottom_nav);
        bottomNav.setItemClickListener(this);
    }

    @Override
    public void onItemClicked(int item) {
        viewContainer.removeAllViews();
        View nextView = getView(item);
        viewContainer.addView(nextView);
    }

    private View getView(int item) {
        //Insert logic
    }
}

<强> R.layout.activity_main

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/view_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/header"/>

    <YourBottomNavView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

或者,你可以使用片段注入你的viewContainer,但我发现使用普通的旧视图比片段中的随机错误更可靠。

以下是自定义视图的示例

public class YourAwesomeView extends LinearLayout {

    public YourAwesomeView(Context context) {
        super(context);
        init();
    }

    public YourAwesomeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.YOUR_AWESOME_VIEW, this);
    }
}

<强> YourBottomNavView.java

public class YourBottomNavView extends LinearLayout {
    View button1, button2, button3;
    View root;
    OnItemClickListener onItemClickListener;

    public YourBottomNavView(Context context) {
        super(context);
        init();
    }

    public YourBottomNavView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.view_bottom_nav, this);
        root = findViewById(R.id.root);
        button1 = findViewById(R.id.button1_container);
        button2 = findViewById(R.id.button2_container);
        button3 = findViewById(R.id.button3_container);

        //The button clicks need to communicate to something like the
        //activity to inflate your new view / fragment. I personally
        //define an OnItemClickedListener interface in the 
        //YourBottomNavView class that the MainActivity implements
        //and I have the activity decide which view to inflate into
        //its frame layout. This is also where you can do cool
        //animations like we saw from the GIFs.
        //This is also where you can swap out drawables to color the  
        //ImageViews differently.

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClicked(0);
                root.setBackgroundColor(0xFF0000);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClicked(1);
                root.setBackgroundColor(0x00FF00);
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
                onItemClickListener.onItemClicked(2);
                root.setBackgroundColor(0x0000FF);
            }
        });
    }

    public void setItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClicked(int item);
    }
}

<强> R.layout.view_bottom_nav

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/default_color"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/button1_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weight="1">

        <ImageView android:id="@+id/image1"
            android:layout_width="24dp"
            android:layout_height="24dp"
            src="@drawable/icon1"/>

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/button2_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weight="1">

        <ImageView android:id="@+id/image2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            src="@drawable/icon1"/>

        <TextView
            android:id="@+id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/button3_container"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weight="1">

        <ImageView android:id="@+id/button3"
            android:layout_width="24dp"
            android:layout_height="24dp"
            src="@drawable/icon3"/>

        <TextView
            android:id="@+id/text3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

答案 1 :(得分:0)

几个月后我的应用程序想到了这个问题。 我发现仅使用Android应用程序中使用的MVC模式很难实现所需的行为,因为在应用程序流程中将主要活动保留在屏幕上会使此类超载。

虽然谷歌Android教程传播了这个MVC的想法,但真正的谷歌应用程序和设计实例(就像你说的这个美丽的音乐播放器)在架构和代码组织方面遥遥领先。

这个问题的真正解决方案是使用除MVC之外的其他模式来解耦逻辑和视图。您可能想要了解Clean Architecture中使用的模式,以便在多个类中将重代码解耦,并且仍然在屏幕上保持相同的Activity(具有底部条的那个)。