android如何实现导航抽屉子类

时间:2016-02-09 12:10:05

标签: java android navigation-drawer material-design

我想创建像TOI app这样的导航抽屉子类别。到目前为止,我已经完成了这个(代码的某些部分):

main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
    android:id="@+id/app_bar"
    layout="@layout/app_bar" />

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/container_body"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>

    <fragment
        android:id="@+id/fragment_navigation_drawer"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:layout="@layout/fragment_navigation_drawer"
        android:name="com.medical.medicaldialogue.Activity.FragmentDrawer"
        tools:layout="@layout/fragment_navigation_drawer" />

</android.support.v4.widget.DrawerLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements FragmentDrawer.FragmentDrawerListener{

private static String TAG = MainActivity.class.getSimpleName();

public static int SELECTED_POSITION = 0;

private SearchView searchView;

private Toolbar toolbar;
private ImageView imageViewToolbar;

private FragmentDrawer drawerFragment;



 private ArrayList<HomeSummary> summaryItemArrayList;

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

    summaryItemArrayList = (ArrayList<HomeSummary>)getIntent().getSerializableExtra("summaryList");

    Log.d("arrayLength -", ""+summaryItemArrayList.size());

    toolbar =(Toolbar)findViewById(R.id.app_bar);
    imageViewToolbar = (ImageView)findViewById(R.id.app_bar_img);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    drawerFragment = (FragmentDrawer)
            getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
    drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
    drawerFragment.setDrawerListener(this);

    // display the first navigation drawer view on app launch
    displayView(0);

}


@Override
public void onDrawerItemSelected(View view, int position) {
        displayView(position);
}


private void displayView(int position){
    Bundle bundle =  new Bundle();
    Fragment fragment = null;
    String title = getString(R.string.app_name);
    switch (position){
        case 0:
            fragment = new EditorPicsFragment();
            title = getString(R.string.title_home);
            imageViewToolbar.setVisibility(View.VISIBLE);
            break;
    }

    if(fragment != null){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        bundle.putString("myFragment",title);
        if(title.equalsIgnoreCase("Title")){
            bundle.putParcelableArrayList("summaryList", summaryItemArrayList);
        } else if(title.equalsIgnoreCase("Editor's Pick")){
            bundle.putString("cat_id", "56");
        } 
        fragment.setArguments(bundle);
        fragmentTransaction.replace(R.id.container_body, fragment);
        fragmentTransaction.commit();

        // set the app_bar title
        getSupportActionBar().setTitle(title);

        PrefStore.setStoryBarTitle(MainActivity.this, title);
    }
}

@Override
protected void onResume() {
    super.onResume();

  //  displayView(0);
}
}

FragmentDrawer.java

public class FragmentDrawer extends Fragment {

private 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 FragmentDrawerListener drawerListener;

public FragmentDrawer() {
    // Required empty public constructor

    Log.d(TAG, "Drawer fragment created 1111");

}

public void setDrawerListener(FragmentDrawerListener listener){
    this.drawerListener = listener;
}

public static List<NavDrawerItem> getData(){
    List<NavDrawerItem> data = new ArrayList<>();

    // Preparing navigation drawer items
    for(int i=0; i<titles.length; i++){
        NavDrawerItem navItem = new NavDrawerItem();
        navItem.setTitle(titles[i]);
        data.add(navItem);
    }
    return data;
}

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

    // drawer labels
    titles = getActivity().getResources().getStringArray(R.array.nav_drawer_labels);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflating view layout
    View layout = inflater.inflate(R.layout.fragment_navigation_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);


            /**
             * Modified Date on 9/2/2016
             * Resizing the Drawer width when
             */
            if(position ==21) {
                ViewGroup.LayoutParams params = containerView.getLayoutParams();
                params.width = 250;
                containerView.setLayoutParams(params);
            }else {
             //   ViewGroup.LayoutParams params = containerView.getLayoutParams();
             //   params.width = 400;
             //   containerView.setLayoutParams(params);
                mDrawerLayout.closeDrawer(containerView);
            }
            Log.d(TAG, "Postion clicked: "+position);
        }

        @Override
        public void onLongClick(View view, int position) {
            Log.d(TAG, "Long press");
            drawerListener.onDrawerItemSelected(view, position);
            mDrawerLayout.closeDrawer(containerView);
        }
    }));

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

            Log.d("Drawer opened","");
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            getActivity().invalidateOptionsMenu();
        }

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
          //  drawerListener.onDrawerItemSelected(drawerView, 4);

           // toolbar.setAlpha(1 - slideOffset / 2);
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();

            Log.d("syncState", "Drawer");
        }
    });


}

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) {
    }

    @Override
    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

    }


}

public interface FragmentDrawerListener{
    public void onDrawerItemSelected(View view, int position);
}


@Override
public void onStart() {
    super.onStart();

    Log.d(TAG, "Drawer fragment created onStart() 222222");
}
}
到目前为止,我能够创建导航抽屉。但我希望这样做 TOI App

如何在上面的代码中添加子类别抽屉。任何帮助将不胜感激。

0 个答案:

没有答案