2017-08-22 10 views
-1

ボトムナビゲーションアクティビティを使用してプロジェクトを作成しました。サイドバーナビゲーションドロワーを配置する方法が難しいようです。それサイドバーナビゲーションドロワーをAndroidスタジオのボトムナビゲーションアクティビティに追加するにはどうすればいいですか

public class NavDrawerItem { 
private boolean showNotify; 
private String title; 


public NavDrawerItem() { 

} 

public NavDrawerItem(boolean showNotify, String title) { 
    this.showNotify = showNotify; 
    this.title = title; 
} 

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

にこのコードを入れ

<string name="drawer_open">Open</string> 

    <string name="drawer_close">Close</string> 

    <string name="nav_item_home">main</string> 

    <string name="nav_item_setting">setting</string> 

    <string name="nav_item_exit">exit</string> 

    <!-- navigation drawer item labels --> 
<string-array name="nav_drawer_labels"> 

    <item>@string/nav_item_home</item> 

    <item>@string/nav_item_setting</item> 

    <item>@string/nav_item_exit</item> 

</string-array> 

NavDrawerItem.javaを作成して:

答えて

1

HIは

このcompile 'com.android.support:recyclerview-v7:23.0.0'

値>文字列を追加段階的に行う必要があります// res>レイアウト作成レイアウトnav_drawer_row.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:clickable="true"> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="30dp" 
     android:paddingTop="10dp" 
     android:paddingBottom="10dp" 
     android:textSize="15dp" 
     android:textStyle="bold" /> 

</RelativeLayout> 

//解像度>レイアウトレイアウトfragment_navigation_drawer.xmlを作成します。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layoutDirection="rtl" 
    android:background="@android:color/white"> 


    <RelativeLayout 
     android:id="@+id/nav_header_container" 
     android:layout_width="match_parent" 
     android:layout_height="140dp" 
     android:layout_alignParentTop="true" 
     android:background="@color/colorPrimary"> 

     <ImageView 
      android:layout_width="70dp" 
      android:layout_height="70dp" 
      android:src="@drawable/ic_user" 
      android:scaleType="fitCenter" 
      android:layout_centerInParent="true" /> 

    </RelativeLayout> 
    <android.support.v7.widget.RecyclerView   
     android:layout_marginRight="15dp" 
    android:id="@+id/recycler" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/nav_header_container" 

     android:layout_marginTop="15dp" /> 


</RelativeLayout> 

は、クラス// NavigationDrawerAdapter.javaを作成します。

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.TextView; 

import java.util.Collections; 
import java.util.List; 


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

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

    class MyViewHolder extends RecyclerView.ViewHolder { 
     TextView title; 

     public MyViewHolder(View itemView) { 
      super(itemView); 
      title = (TextView) itemView.findViewById(R.id.title); 
     } 

// Framgment>フラグメントFragmentDrawer.javaを作成します。

import android.content.Context; 
import android.net.Uri; 
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.support.v7.widget.Toolbar; 
import android.view.GestureDetector; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 

import java.util.ArrayList; 
import java.util.List; 


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

    public FragmentDrawer() { 

    } 

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

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

     @Override 
     public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 

     } 


    } 

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

編集あなた//activity_main.xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 

    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/container_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

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

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


    </LinearLayout> 


    <fragment 
     android:id="@+id/fragment_navigation_drawer" 
     android:name="com.example.videolabpc.mytoolbar.FragmentDrawer" 
     android:layout_width="260dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:layout="@layout/fragment_navigation_drawer" 
     tools:layout="@layout/fragment_navigation_drawer" /> 

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

編集ご//MainActivity.java

implements FragmentDrawer.FragmentDrawerListener 

とMainActivity.java

private FragmentDrawer drawerFragment; 

drawerFragment = (FragmentDrawer) 
       getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer); 
     drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), mToolbar); 
     drawerFragment.setDrawerListener(this); 
にこのコードを追加

ホープあなたを助けます!

+0

FragmentDrawer.FragmentDrawerListenerの実装場所はどこですか? – Zea

+0

とにかく感謝! <3 – Zea

+0

ああsht .....私は例が必要なようだ...私はそれがアンドロイドスタジオになるとまだnoobです.... – Zea