我有两个类。一个是扩展AppcompactActivity的抽象类BaseActivity.java
,另一个是扩展Fragment
的DrawerFragment.java!如何使BaseActivity类实现NavigationItemSelectedListener并处理此活动中的导航项单击事件!我复制粘贴下面的相关代码:
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
private static final String TAG = BaseActivity.class.getSimpleName();
protected Toolbar mToolbar;
protected DrawerFragment mNavigationDrawerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setElevation(0);
// Set navigation drawer
mNavigationDrawerFragment = (DrawerFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_drawer);
mNavigationDrawerFragment.setNavig((DrawerLayout) findViewById(R.id.drawer), mToolbar);
}
Drawer Fragment.java
public class DrawerFragment extends Fragment{
private NavigationView navigView;
private Activity activity;
private static final String TAG = "NavigationDrawer";
private DrawerLayout drawerLayout;
private TextView fullNameTextView;
private ImageView profileImage;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.navigation_drawer_fragment, container, false);
navigView=(NavigationView)view.findViewById(R.id.main_drawer);
// Inflating nav_header layout on top of navigation drawer layout
View temp=getActivity().getLayoutInflater().inflate(R.layout.nav_header,navigView,false);
navigView.addHeaderView(temp);
profileImage= (ImageView) temp.findViewById(R.id.profile_image);
fullNameTextView= (TextView) temp.findViewById(R.id.fullNameTextView);
return view;
}
public void setNavig(DrawerLayout drawerLayout, Toolbar toolbar) {
ActionBarDrawerToggle drawerToggle=new ActionBarDrawerToggle(activity, drawerLayout,toolbar, R.string.drawer_open, R.string.drawer_close);
drawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
this.drawerLayout=drawerLayout;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
activity=getActivity();
}
}
我知道如何在DrawerFragment.java
类上实现项目点击监听器并在那里处理事件。但它不是我想要的。我打算从BaseActivity.java
类
答案 0 :(得分:1)
//MAKE A PUBLIC METHOD IN THE NAVIGATION VIEW THAT RETURNS NAVIGATION VIEW
//in DrawerFragment
public NavigationView getNav(){
return navView;
}
//use that public method from baseactivity.java and set listener on the obtained object
getNav().setNavigationItemSelectedListener(this);
但最好的方法是在导航抽屉上实现监听器 片段本身。因为它使导航抽屉完整并与BaseActivity(或任何其他类)分离。或者你可以这样做,如下面的使用接口机制:
//INSIDE FragmentDrawer
//Let FragmentDrawer Implement OnItemSelectedListener
//then you need to override onItemSelected() method
@Override
public void OnItemSelected(int pos){
//Call The method implemented on your Activity using interface
communicator.displayItem(pos);
//where communicator has reference to your activity and dispayItem() is implemented method
}