web-dev-qa-db-ja.com

下部のナビゲーションボタンのクリックでアクティビティを変更する方法

既存のAndroidアプリで下部ナビゲーションバーを使用したいのですが、問題はすべての画面がアクティビティであるということです。下部ナビゲーションバーを非表示にせずにアクティビティをロードできますか。

例:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<Android.support.design.widget.CoordinatorLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:app="http://schemas.Android.com/apk/res-auto"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent">

    <Android.support.v4.widget.NestedScrollView
        Android:id="@+id/myScrollingContent"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent">

        <!-- Your loooooong scrolling content here. -->

    </Android.support.v4.widget.NestedScrollView>
    <com.roughike.bottombar.BottomBar
        Android:id="@+id/bottomBar"
        Android:layout_width="match_parent"
        Android:layout_height="60dp"
        Android:layout_gravity="bottom"
        app:bb_tabXmlResource="@xml/bottom_bar"
        app:bb_behavior="shy"/>

</Android.support.design.widget.CoordinatorLayout>

これは私の基本活動です。

MainActivity.Java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        BottomBar bottomBar;
        bottomBar = (BottomBar) findViewById(R.id.bottomBar);
        bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelected(@IdRes int tabId) {
                if (tabId == R.id.matching) {
                    Log.i("matching","matching inside "+tabId);
                    Intent in=new Intent(getBaseContext(),Main2Activity.class);
                    startActivity(in);
                }else if (tabId == R.id.watchlist) {
                    Log.i("matching","watchlist inside "+tabId);
                    Intent in=new Intent(getBaseContext(),Main3Activity.class);
                    startActivity(in);
                }
            }
        });
    }
}

Main2Activity

public class Main2Activity extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main2);
        NestedScrollView dynamicContent = (NestedScrollView) findViewById(R.id.myScrollingContent);
        View wizard = getLayoutInflater().inflate(R.layout.activity_main2, null);
        dynamicContent.addView(wizard);

Main3Activity

public class Main3Activity extends MainActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main3);
        NestedScrollView dynamicContent = (NestedScrollView) findViewById(R.id.myScrollingContent);
        View wizard = getLayoutInflater().inflate(R.layout.activity_main3, null);
        dynamicContent.addView(wizard);


    }
}

マニフェスト

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
    package="com.bottom.bottomnavigation">

    <application
        Android:allowBackup="true"
        Android:icon="@mipmap/ic_launcher"
        Android:label="@string/app_name"
        Android:supportsRtl="true"
        Android:theme="@style/AppTheme">
        <activity Android:name=".Main2Activity">
            <intent-filter>
                <action Android:name="Android.intent.action.MAIN" />

                <category Android:name="Android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity Android:name=".MainActivity" />
        <activity Android:name=".Main3Activity"></activity>
    </application>

</manifest>
17
Sushil Dubey

次の方法でこの問題を解決しました。

1.下部ナビゲーションバーで1つBaseActivityを作成します。

 package com.example.Apple.bottomnavbarwithactivity;

 import Android.content.Intent;
 import Android.support.v7.app.AppCompatActivity;
 import Android.os.Bundle;
 import Android.util.Log;
 import Android.widget.RadioButton;
 import Android.widget.RadioGroup;

public class BaseActivity extends AppCompatActivity {


RadioGroup radioGroup1;
RadioButton deals;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base);


    radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
    deals = (RadioButton)findViewById(R.id.deals);
    radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        {
            Intent in;
            Log.i("matching", "matching inside1 bro" + checkedId);
            switch (checkedId)
            {
                case R.id.matching:
                    Log.i("matching", "matching inside1 matching" +  checkedId);
                    in=new Intent(getBaseContext(),MatchingActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                case R.id.watchList:
                    Log.i("matching", "matching inside1 watchlistAdapter" + checkedId);

                    in = new Intent(getBaseContext(), WatchlistActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);

                    break;
                case R.id.rates:
                    Log.i("matching", "matching inside1 rate" + checkedId);

                    in = new Intent(getBaseContext(),RatesActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                case R.id.listing:
                    Log.i("matching", "matching inside1 listing" + checkedId);
                    in = new Intent(getBaseContext(), ListingActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                case R.id.deals:
                    Log.i("matching", "matching inside1 deals" + checkedId);
                    in = new Intent(getBaseContext(), DealsActivity.class);
                    startActivity(in);
                    overridePendingTransition(0, 0);
                    break;
                default:
                    break;
            }
        }
    });
   }
 }

base_activity.xmlという名前のBaseActivityレイアウト

    <LinearLayout
        Android:layout_width="match_parent"
        Android:layout_height="56dp"
        Android:elevation="10dp"
        Android:background="@color/white"
        Android:id="@+id/bottonNavBar"
        Android:paddingTop="5dp"
        Android:orientation="horizontal"
        Android:layout_alignParentBottom="true"
        Android:foregroundGravity="bottom">


        <RadioGroup
            Android:id="@+id/radioGroup1"
            Android:gravity="center"
            Android:layout_height="match_parent"
            Android:layout_width="match_parent"
            Android:orientation="horizontal"
            Android:baselineAligned="false">

            <RadioButton
                Android:layout_width="match_parent"
                Android:gravity="center"
                Android:layout_height="match_parent"
                Android:text="Matching"
                Android:layout_weight="1"
                Android:button="@null"
                Android:padding="2dp"
                Android:checked="false"
                Android:textSize="12sp"
                Android:drawableTop="@drawable/selector_matching"
                Android:textColor="@drawable/selector_nav_text"
                Android:id="@+id/matching"/>

            <RadioButton
                Android:layout_width="match_parent"
                Android:gravity="center"
                Android:layout_height="match_parent"
                Android:button="@null"
                Android:layout_weight="1"
                Android:padding="2dp"
                Android:checked="false"
                Android:textSize="12sp"
                Android:drawableTop="@drawable/selector_watchlist"
                Android:textColor="@drawable/selector_nav_text"
                Android:id="@+id/watchList"
                Android:text="Watchlist"/>

            <RadioButton
                Android:layout_width="match_parent"
                Android:gravity="center"
                Android:layout_height="match_parent"
                Android:id="@+id/rates"
                Android:button="@null"
                Android:paddingTop="5dp"
                Android:paddingBottom="2dp"
                Android:paddingLeft="2dp"
                Android:paddingRight="2dp"
                Android:layout_weight="1"
                Android:checked="false"
                Android:textSize="12sp"
                Android:drawableTop="@drawable/selector_rates"
                Android:textColor="@drawable/selector_nav_text"
                Android:text="Rates"/>
            <RadioButton
                Android:layout_width="match_parent"
                Android:gravity="center"
                Android:layout_height="match_parent"
                Android:button="@null"
                Android:padding="2dp"
                Android:checked="false"
                Android:layout_weight="1"
                Android:textSize="12sp"
                Android:drawableTop="@drawable/selector_deals"
                Android:textColor="@drawable/selector_nav_text"
                Android:id="@+id/deals"
                Android:text="Deals"/>
            <RadioButton
                Android:layout_width="match_parent"
                Android:gravity="center"
                Android:layout_height="match_parent"
                Android:button="@null"
                Android:padding="2dp"
                Android:checked="false"
                Android:layout_weight="1"
                Android:textSize="12sp"
                Android:drawableTop="@drawable/selector_listing"
                Android:textColor="@drawable/selector_nav_text"
                Android:id="@+id/listing"
                Android:text="Listing"/>


        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:id="@+id/dynamicContent"
        Android:orientation="vertical"
        Android:layout_marginBottom="56dp"
        Android:background="@color/white"
        Android:layout_gravity="bottom">
    </LinearLayout>

2.下部ナビゲーションクリックで開き、アクティビティレイアウトを拡張する必要があるすべてのアクティビティでBaseActivityを拡張します。たとえば、5つのサンプルアクティビティを作成しました。

i] MatchingActivity。

  package com.example.Apple.bottomnavbarwithactivity;

    import Android.graphics.Color;
    import Android.os.Bundle;
    import Android.support.design.widget.FloatingActionButton;
    import Android.support.design.widget.Snackbar;
    import Android.support.v7.app.AppCompatActivity;
    import Android.support.v7.widget.Toolbar;
    import Android.view.View;
    import Android.widget.LinearLayout;
    import Android.widget.RadioButton;
    import Android.widget.RadioGroup;
              //extends our custom BaseActivity
    public class MatchingActivity extends BaseActivity {
        LinearLayout dynamicContent,bottonNavBar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                        //setContentView(R.layout.activity_matching);


                       //dynamically include the  current activity      layout into  baseActivity layout.now all the view of baseactivity is   accessible in current activity.
            dynamicContent = (LinearLayout)  findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_matching, null);
            dynamicContent.addView(wizard);


              //get the reference of RadioGroup.

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.matching);

                 // Change the corresponding icon and text color on nav button click.

            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_matching_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }

    }

ii] WatchlistActivity

 package com.example.Apple.bottomnavbarwithactivity;

    import Android.graphics.Color;
    import Android.support.v7.app.AppCompatActivity;
    import Android.os.Bundle;
    import Android.view.View;
    import Android.widget.LinearLayout;
    import Android.widget.RadioButton;
    import Android.widget.RadioGroup;

    public class WatchlistActivity extends AppCompatActivity {
        LinearLayout dynamicContent,bottonNavBar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_watchlist1);

            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_watchlist1, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.watchList);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.favourite_heart_selected, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }
    }

iii] RatesActivity

  package com.example.Apple.bottomnavbarwithactivity;

    import Android.graphics.Color;
    import Android.support.v7.app.AppCompatActivity;
    import Android.os.Bundle;
    import Android.view.View;
    import Android.widget.LinearLayout;
    import Android.widget.RadioButton;
    import Android.widget.RadioGroup;

    public class RatesActivity extends BaseActivity {
        LinearLayout dynamicContent,bottonNavBar;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           // setContentView(R.layout.activity_rates);

            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_rates, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.rates);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_rate_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }
    }

iv] ListingActivity

 package com.example.Apple.bottomnavbarwithactivity;

    import Android.graphics.Color;
    import Android.support.v7.app.AppCompatActivity;
    import Android.os.Bundle;
    import Android.view.View;
    import Android.widget.LinearLayout;
    import Android.widget.RadioButton;
    import Android.widget.RadioGroup;

    public class ListingActivity extends BaseActivity {
        LinearLayout dynamicContent,bottonNavBar;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
             //setContentView(R.layout.activity_listing);



            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_listing, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.listing);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_listing_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));
        }
    }

v] DealsActivity

 package com.example.Apple.bottomnavbarwithactivity;

    import Android.graphics.Color;
    import Android.support.v7.app.AppCompatActivity;
    import Android.os.Bundle;
    import Android.view.View;
    import Android.widget.LinearLayout;
    import Android.widget.RadioButton;
    import Android.widget.RadioGroup;

    public class DealsActivity extends BaseActivity {

        LinearLayout dynamicContent,bottonNavBar;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_deals);
            dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
            bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
            View wizard = getLayoutInflater().inflate(R.layout.activity_deals, null);
            dynamicContent.addView(wizard);

            RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
            RadioButton rb=(RadioButton)findViewById(R.id.deals);
            rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_deals_clicked, 0,0);
            rb.setTextColor(Color.parseColor("#3F51B5"));



        }
    }

注:バックプレスを適切に処理するようにしてください。バックプレスのコードは書いていません。 。

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

15
Sushil Dubey

Activityを使用すると、BottomBarをロードするたびにactivityを宣言して初期化する必要があります。

あなたの問題で、私の答えは[〜#〜] no [〜#〜]です。

ところで、あなたは Fragment を使うことができます。

何か新しいことを学ぶ時間です。

[〜#〜] edit [〜#〜]

Fragmentのみを内部(1アクティビティに入れます。そして、BottomBarActivityの中に入れ、他のすべてはFragmentの中にデータとビューを表示します。

やってみなよ!

4
Admiral Kunkka