我是Android的新手,我尝试使用
在导航抽屉的每一行菜单中添加图标适配器类:
@model IEnumerable<GRCWebApp.ViewModels.NewMembershipTypeViewModel>
和与模型:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.ash.light.R;
import java.util.Collections;
import java.util.List;
import model.NavDrawerItem;
public class NavigationDrawerAdapter extends RecyclerView.Adapter<NavigationDrawerAdapter.MyViewHolder> {
List<NavDrawerItem> data= Collections.emptyList();
private LayoutInflater inflater;
private Context context;
public NavigationDrawerAdapter(Context context, List<NavDrawerItem> data){
this.context=context;
inflater=LayoutInflater.from(context);
this.data=data;
}
public void delete(int position){
data.remove(position);
notifyItemRemoved(position);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view=inflater.inflate(R.layout.nav_row_drawer, parent, false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position){
NavDrawerItem current=data.get(position);
holder.title.setText(current.getTitle());
holder.icon.setImageResource(current.getIcon());
}
@Override
public int getItemCount(){
return data.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView icon;
public MyViewHolder(View itemView){
super(itemView);
title=(TextView) itemView.findViewById(R.id.title);
icon=(ImageView) itemView.findViewById(R.id.rowIcon);
}
}
}
使用FragmentDrawer类:
public class NavDrawerItem {
private boolean shownotify;
private String title;
private int icon;
public NavDrawerItem(){
}
public NavDrawerItem(boolean shownotify, String title, int icon){
this.shownotify=shownotify;
this.title=title;
this.icon=icon;
}
public boolean isShowNotify(){
return shownotify;
}
public void setShowNotify(boolean shownotify){
this.shownotify=shownotify;
}
public String getTitle(){
return title;
}
public void setTitle(String title){
this.title=title;
}
public int getIcon(){
return icon;
}
public void setIcon(int icon){
this.icon=icon;
}
}
和MainActivity:
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.support.v7.widget.Toolbar;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import adapter.NavigationDrawerAdapter;
import model.NavDrawerItem;
public class FragmentDrawer extends Fragment {
private static String TAG = FragmentDrawer.class.getSimpleName();
private RecyclerView recyclerView;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private NavigationDrawerAdapter adapter;
private View containerView;
private static String[] titles=null;
private static int[] icons;
private FragmentDrawerListener drawerListener;
// private OnFragmentInteractionListener mListener;
public FragmentDrawer() {
}
public void setDrawerListener(FragmentDrawerListener listener){
this.drawerListener=listener;
}
public static List<NavDrawerItem> getData(){
List<NavDrawerItem> data=new ArrayList<>();
for(int i=0;i<titles.length;i++){
NavDrawerItem navItem=new NavDrawerItem();
navItem.setTitle(titles[i]);
navItem.setIcon(icons[i]);
data.add(navItem);
}
return data;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
titles=getActivity().getResources().getStringArray(R.array.nav_drawer_labels);
icons=getActivity().getResources().getIntArray(R.array.nav_drawer_icons);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout= inflater.inflate(R.layout.fragment_nav_drawer, container, false);
recyclerView=(RecyclerView) layout.findViewById(R.id.drawerlist);
adapter=new NavigationDrawerAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener(){
@Override
public void onClick(View view, int position){
drawerListener.onDrawerItemSelected(view, position);
mDrawerLayout.closeDrawer(containerView);
}
@Override
public void onLongClick(View view, int position){
}
}));
return layout;
}
public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar){
containerView=getActivity().findViewById(fragmentId);
mDrawerLayout=drawerLayout;
mDrawerToggle=new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close){
@Override
public void onDrawerOpened(View drawerView){
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerSlide(View drawerView, float slideoffset){
super.onDrawerSlide(drawerView, slideoffset);
toolbar.setAlpha(1-(slideoffset/2));
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public static interface ClickListener {
public void onClick(View view, int position);
public void onLongClick(View view, int position);
}
static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener){
this.clickListener=clickListener;
gestureDetector=new GestureDetector(context, new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e){
return true;
}
@Override
public void onLongPress(MotionEvent e){
View child=recyclerView.findChildViewUnder(e.getX(), e.getY());
if(child!=null && clickListener!=null){
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e){
View child=rv.findChildViewUnder(e.getX(), e.getY());
if(child!=null && clickListener!=null && gestureDetector.onTouchEvent(e)){
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e){
}
}
public interface FragmentDrawerListener{
public void onDrawerItemSelected(View view, int position);
}
}
我没有获得带有菜单项的图标,休息一切正常。我知道有各种其他方法可以通过导航栏菜单获取图标,但是 我想只使用这些类(如果可能的话)获得图标,只需进行少量编辑。 请帮帮我......