2016-05-13 5 views
1

が消えなくて、私はこのような何か達成するためのアイデアを見つけることができません:私は画像新しい活動上のクリックした後に開いていることを理解しオープン活動新しい下のバー・ソリューションによると、下のバー

Gif with new Activity

を、しかし、ボトムバーを消さずにオープンする方法は?私は音楽のタブのユーザーがコーダラインのアルバムにして、新しい活動のようなものを明らかにするよりも、クリックされた例について話している

EDIT

+1

私は、FragmentsをActivityの代わりに使用し、親のアクティビティレイアウトに "Bottom Bar"を保持します。 [この例](http://blog.grafixartist.com/bottom-navigation-bar-android-tutorial/)では手がかりを得ています。 – Fllo

答えて

1

新しいアクティビティが開始されていないか、下位のナビゲーションバーが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

IMOはコードが混乱するため、最初に提案されたソリューションはひどいものです。私はむしろオプション#2を使い、navBarViewを保持している何らかの種類のホストアクティビティを使用し、内部のフラグメントを変更します。シンプルでクリーンで柔軟性があります。 – Leonardo

+0

しかし、どのようにステータスバーの色が変化しているのでしょうか?これは、アクティビティの再ロードや新しいものの作成が必要です。 – ThirdMartian

+0

これまではフラグメントを使用していましたが、それに慣れていれば、フラグメントをFrameLayoutに展開する似たようなソリューションになります。しかし、私が述べたように、私はデバッグが不可能なFragmentTransactionのバグに遭遇しました。ボトムナビゲーターのonClickListenersの場合は、自分で状態を管理する必要があります。ボタンをクリックすると、クリックしたものが「選択」され、それに応じて色が変更されている間に、他のボタンが「選択解除」に設定されていることを確認する必要があります。 –

0

の一例です私のアプリのこの問題を試してください。 Androidのアプリケーションで使用されているMVCパターンのみで目的の動作を達成することは非常に難しいことが分かりました。

GoogleのAndroidチュートリアルはこのMVCのアイデアを広めていますが、あなたが言ったこの美しい音楽プレーヤーのような、実際のGoogleのアプリケーションとデザインの例は、アーキテクチャとコード構成に関してはるかに先行しています。

この問題の本当の解決策は、MVC以外のパターンを使用してロジックとビューを結合解除することです。重いコードを複数のクラスに分けず、同じアクティビティ(下のバーのもの)を画面上に残すために、Clean Architectureで使用されているパターンについて読むことができます。

関連する問題