从片段到片段遍历BottomNavigationView图标突出显示

时间:2017-01-06 08:38:52

标签: android android-studio android-fragments fragmentmanager bottomnavigationview

我正在尝试实现BottomNavigationView,到目前为止已成功。目前正在尝试将片段实现为片段移动,这也是成功的,但不知何故,当我从一个片段[radio]移动到另一个[radio]时,导航栏应该突出显示图标[stream]但是它没有发生是否有办法我可以通过片段本身设置高亮属性? 以下是我的应用程序的代码和快照:

FragmentOne

FragmentTwo

MainActivity.java

import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.br.tron.bottombar.RadioFragment;
import com.br.tron.bottombar.StreamFragment;
import com.br.tron.bottombar.InfoFragment;

public class MainActivity extends AppCompatActivity {

    BottomNavigationView bottomNavigationView;
    private Fragment fragment;
    private FragmentManager fragmentManager;

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

        fragmentManager = getSupportFragmentManager();
        fragment = new RadioFragment();
        final FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.main_container, fragment).commit();

        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationBar);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.nav_button_one:
                        fragment = new RadioFragment();
                        break;
                    case R.id.nav_button_two:
                        fragment = new StreamFragment();
                        break;
                    case R.id.nav_button_three:
                        fragment = new InfoFragment();
                        break;
                }
                final FragmentTransaction transaction = fragmentManager.beginTransaction();
                transaction.replace(R.id.main_container, fragment).commit();
                return true;
            }
        });
    }

    public void performStreamClick(){
        View view = bottomNavigationView.findViewById(R.id.main_container);
        view.performClick();
    }
}

RadioFragment.java

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class RadioFragment extends Fragment implements Button.OnClickListener  {

    Button buttonman;
    View rootView;

    Activity a;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        if (context instanceof Activity) {
            a = (Activity) context;
        }
    }

    public RadioFragment(){
    };

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_player, container, false);
        buttonman = (Button)rootView.findViewById(R.id.buttonman);
        buttonman.setOnClickListener(this);
        return rootView;
    }

    @Override
    public void onClick(View v) {
        /*Fragment fragment = new StreamFragment();
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.main_container, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();*/
        ((MainActivity)a).performStreamClick();
    }
}

StreamFragment.java

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class StreamFragment extends Fragment {

    public StreamFragment(){};

    @Override
    public View onCreateView(final LayoutInflater inflater,final ViewGroup container,final Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.fragment_stream, container, false);

    }

}

1 个答案:

答案 0 :(得分:1)

您没有初始化buttonman

在RadioFragment.java中

Button buttonman;
View rootView;    
Activity a;

@Override
public void onAttach(Context context) {
super.onAttach(context);

if (context instanceof Activity){
    a=(Activity) context;
}
}

public RadioFragment(){
};

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {

    rootView = inflater.inflate(R.layout.fragment_player, container, false);
    buttonman = (Button)rootView.findViewById(R.id.yourbuttonid); // initialize here
    buttonman.setOnClickListener(this);
    return rootView;

}
@Override
public void onClick(View v) {
   ((MainActivity)a).performStreamClick();

}
MainActivity

中的

public void performStreamClick(){
   View view = bottomNavigationView.findViewById(R.id.nav_button_two);
view.performClick();
}

注意:您也可以通过interfce

来完成

<强>解释

您已添加

Fragment fragment = new StreamFragment(); 
FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.replace(R.id.main_container, fragment); 
fragmentTransaction.addToBackStack(null); 
fragmentTransaction.commit(); 
  

这只会在StreamFragment framelayout中推送main_container而不通知bottomNavigationView

下面

View view = bottomNavigationView.findViewById(R.id.nav_button_two);
view.performClick();
  

此行将以编程方式执行nav_button_two中的bottomNavigationView点击...然后所有事件将由bottomNavigationView处理..然后它将突出显示stream图标。