工具导航抽屉 - 开关盒

时间:2015-02-02 15:23:46

标签: android android-fragments switch-statement navigation-drawer android-toolbar

您好我正在尝试将开关案例语句用于Android应用程序的导航抽屉。无论你要打开哪个片段,我都会在onclick事件中打开一个活动。我知道我必须做的结构(获取所选项目的ID并打开该片段)。有人可以告诉我哪里最好输入一块罐子以及如何开始。

导航抽屉活动:

public class NavigationDrawerFragment extends Fragment implements BCRfmAdapter.ClickListener{


private RecyclerView recyclerView;
public static final String PREF_FILE_NAME = "testpref";
public static final String KEY_USER_LEARNED_DRAWER = "user_learned_drawer";

private ActionBarDrawerToggle mDrawerToogle;
private DrawerLayout mDrawerLayout;

private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;

private BCRfmAdapter adapter;

private View containerView;

public NavigationDrawerFragment() {
    // Required empty public constructor
}

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

    mUserLearnedDrawer = Boolean.valueOf(readFromPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, "false"));

    if (savedInstanceState != null) {
        mFromSavedInstanceState = true;
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
    recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
    adapter = new BCRfmAdapter(getActivity(), getData());
    adapter.setClickListener(this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getActivity(), recyclerView, new ClickListener() {
        @Override
        public void onClick(View view, int position)
        {

        }

        @Override
        public void onLongClick(View view, int position)
        {

        }
    }));

    return layout;
}

public static List<Information> getData() {
    //load only static data inside a drawer
    List<Information> data = new ArrayList<>();
    int[] icons = {R.drawable.ic_facebook, R.drawable.ic_facebook, R.drawable.ic_twitter, R.drawable.ic_action_about, R.drawable.ic_contact, R.drawable.ic_action_about};
    String[] titles = {"Listen Live", "Facebook", "Twitter", "Google+", "About BCR FM", "Contact Us"};
    for (int i = 0; i < 6; i++) {
        Information current = new Information();
        current.iconId = icons[i % icons.length];
        current.title = titles[i % titles.length];
        data.add(current);
    }
    return data;

    //twitter://user?user_id=1721642934
    //Started to add each item to the list rather than using an array..
    // Add Drawer Item to dataList
    /*data.add(new Information("Listen LIve", R.drawable.ic_launcher));
    data.add(new Information("Facebook", R.drawable.ic_launcher));
    data.add(new Information("Twitter", R.drawable.ic_launcher));
    data.add(new Information("About BCRFm", R.drawable.ic_action_about));
    data.add(new Information("Contact Us", R.drawable.ic_action_email));*/
}

//TODO switch case
@Override
public void itemClicked(View view, int position) {
    startActivity(new Intent(getActivity(), devActivity.class));
}

public static void saveToPreferences(Context context, String preferenceName, String preferenceValue) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(preferenceName, preferenceValue);
    editor.apply();
}

public static String readFromPreferences(Context context, String preferenceName, String defaultValue) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
    return sharedPreferences.getString(preferenceName, defaultValue);
}

public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
    containerView = getActivity().findViewById(fragmentId);
    mDrawerLayout = drawerLayout;
    mDrawerToogle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);

            if (!mUserLearnedDrawer) {
                mUserLearnedDrawer = true;
                saveToPreferences(getActivity(), KEY_USER_LEARNED_DRAWER, mUserLearnedDrawer + "");
            }
            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);
        }
    };
    if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
        mDrawerLayout.openDrawer(containerView);
    }
    mDrawerLayout.setDrawerListener(mDrawerToogle);
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToogle.syncState();
        }
    });

}

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 static interface ClickListener{
    public void onClick(View view, int position);
    public void onLongClick(View view, int position);
}
}

这是我的DrawerAdapter代码:

public class BCRfmAdapter extends RecyclerView.Adapter<BCRfmAdapter.MyViewHolder> {
List<Information> data = Collections.emptyList();
private LayoutInflater inflater;
private Context context;
private ClickListener clickListener;


public BCRfmAdapter(Context context, List<Information> data){
    this.context=context;
    inflater=LayoutInflater.from(context);
    this.data=data;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = inflater.inflate(R.layout.custom_row, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Information current=data.get(position);
    holder.title.setText(current.title);
    holder.icon.setImageResource(current.iconId);
}

@Override
public int getItemCount() {return data.size();}

class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView title;
    ImageView icon;

    public MyViewHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
        title = (TextView) itemView.findViewById(R.id.listText);
        icon = (ImageView) itemView.findViewById(R.id.listIcon);
    }

    @Override
    public void onClick(View v) {
        context.startActivity(new Intent(context, devActivity.class));

        if(clickListener != null)
        {
            clickListener.itemClicked(v, getPosition());
        }
    }
}

public interface ClickListener{
    public void itemClicked(View view, int position);
}

public void setClickListener(ClickListener clickListener){
    this.clickListener=clickListener;
}
}

0 个答案:

没有答案