【问题标题】:Unable to add Fragment to the Navigation Drawer无法将片段添加到导航抽屉
【发布时间】:2016-02-28 02:56:29
【问题描述】:

我试图在导航抽屉中添加一个片段,但奇怪的是最终输出看起来不像导航抽屉。在下图中,抽屉在页面上是静态的,没有框架布局内容和工具栏。我的疑问是我们是否可以在 Fragment 中添加 RecyclerView 然后显示它还是应该只使用 RecyclerView?

以下是文件:

MainActivity.java:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setupToolbar();
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerMainActivity);
    setupDrawerToggle();
    Fragment squadFragment = new SquadFragment();
    FragmentTransaction fragmentTransaction =  getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.containerView,squadFragment,null);
    fragmentTransaction.commit();

}

void setupToolbar(){
    toolbar = (Toolbar) findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
}

void setupDrawerToggle(){
    drawerToggle = new  ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_n   ame);
    //This is necessary to change the icon of the Drawer Toggle upon state  change.
    drawerToggle.syncState();
}

}

ReyclerViewFragment.java:

public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.recyclerfragment,container,false);


    navTitles = getResources().getStringArray(R.array.navDrawerItems);
    navIcons = getResources().obtainTypedArray(R.array.navDrawerIcons);
    recyclerViewAdapter = new  Adapter(navTitles,navIcons,getActivity().getApplicationContext());
   recyclerView = (RecyclerView) v.findViewById(R.id.recyclerView);
    recyclerView.setAdapter(new  Adapter(null,null,getActivity().getApplicationContext()));
    recyclerView.setLayoutManager(new  LinearLayoutManager(getActivity().getApplicationContext()));
    recyclerView.setAdapter(recyclerViewAdapter);

    return v;
}

activity_main.xml:

 <android.support.v4.widget.DrawerLayout   xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:local="http://schemas.android.com/apk/res-auto"
 android:id="@+id/drawerMainActivity"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <include android:id="@+id/toolBar"
        layout="@layout/app_bar"/>
    <FrameLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/containerView">


    </FrameLayout>
</LinearLayout>

<fragment
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:name="com.example.hp.recyclernavigation.RecyclerFragment"
    android:id="@+id/fragment"
    >

    </fragment>

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

recyclerfragment.xml:

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

<android.support.v7.widget.RecyclerView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/recyclerView"
    android:background="#FFFFFF"
    android:layout_gravity="start"
    />
</LinearLayout>

Adapter.java:

 Adapter(String[] titles , TypedArray icons , Context context){

    this.titles = titles;
    this.icons = icons;
    this.context = context;
 //        inflator=LayoutInflater.from(context);
 }

public class ViewHolder extends RecyclerView.ViewHolder  {

    TextView  navTitle;
    ImageView navIcon;
    Context context;

    public ViewHolder(View drawerItem , int itemType , Context context){

        super(drawerItem);
        this.context = context;
      //  drawerItem.setOnClickListener(this);
        if(itemType==1){
            navTitle = (TextView) itemView.findViewById(R.id.tv_NavTitle);
            navIcon = (ImageView) itemView.findViewById(R.id.iv_NavIcon);
        }
    }
}

 @Override
 public Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)    {

    LayoutInflater layoutInflater = (LayoutInflater)  parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if(viewType==1){
        View itemLayout =    layoutInflater.inflate(R.layout.drawer_item_layout,null);
        return new ViewHolder(itemLayout,viewType,context);
    }
    else if (viewType==0) {
        View itemHeader =   layoutInflater.inflate(R.layout.header_layout,null);
        return new ViewHolder(itemHeader,viewType,context);
    }
    return null;
}
@Override
public void onBindViewHolder(Adapter.ViewHolder holder, int position) {

    if(position!=0){
        holder.navTitle.setText(titles[position - 1]);
        holder.navIcon.setImageResource(icons.getResourceId(position-1,-1));
    }
}
@Override
public int getItemCount() {
    return titles.length+1;
}
@Override
public int getItemViewType(int position) {
    if(position==0)return 0;
    else return 1;
}

谁能帮帮我!!

【问题讨论】:

标签: android android-fragments android-recyclerview navigation-drawer drawerlayout


【解决方案1】:

您可以通过 Google 查看 Android Navigation Drawer 示例: https://github.com/googlesamples/android-NavigationDrawer

【讨论】:

    【解决方案2】:

    您似乎未能为 DrawerLayout 指定 layout_gravity 和 width 属性

    您的布局应该看起来像这样

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:clickable="true"
            android:layout_height="match_parent"/>
    
        <fragment
            android:id="@+id/fragment_drawer"
            android:name="wsl.com.dryv.fragment.MainNavigationDrawerFragment"
            android:layout_width="@dimen/navigation_drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:layout="@layout/fragment_navigation_drawer"/>
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

    • 我在片段中有recyclerview,我在抽屉布局中添加了这个片段。“dimen/navigation_drawer_width”和“layout/fragment_navigation_drawer”指向哪些值?他们会参考recyclerview的宽度和布局吗?
    • 宽度完全取决于你。但是,我会推荐 200dp-250dp 之间的东西。布局应该参考你的主要片段的布局。应该是@layout/recyclerfragment。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    相关资源
    最近更新 更多