从片段到片段的遍历BottomNavigationView图标突出显示 [英] Traversing from fragment to fragment BottomNavigationView icon highlight

查看:136
本文介绍了从片段到片段的遍历BottomNavigationView图标突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个BottomNavigationView,到目前为止已经成功了.当前正在尝试实现片段到片段的移动,这也是成功的,但是以某种方式,当我从一个片段[radio]移到另一个[stream]时,导航栏应该突出显示图标[stream],但没有发生,这是有办法的我可以通过片段本身设置高光属性吗? 下面是我的应用程序的代码和快照:

i am trying to implement a BottomNavigationView, been successful so far. Currently trying to implement the fragment to fragment movement, which is also successful, but somehow when i move from one fragment[radio] to another[stream] the navigationbar is supposed to highlight the icon[stream] but its not happening is there a way i can set the highlight properties through the fragment itself ? Below is the code and snapshot of my application:

MainActivity.java

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

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

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);

    }

}

推荐答案

您没有初始化buttonman

在RadioFragment.java

In 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();
}

注意:您也可以通过界面操作

说明

您已添加

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推入main_container帧布局,而不会通知bottomNavigationView.

This will simply push StreamFragment in main_container framelayout without notify bottomNavigationView.

这里

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

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

This line will programmatically perform click on nav_button_two in bottomNavigationView.. then all event will handle by bottomNavigationView .. Then it will highlight the stream icon.

这篇关于从片段到片段的遍历BottomNavigationView图标突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆