2017-03-24 224 views
-2

我想使导航抽屉使用回收站视图,但主要问题是,当我运行这个时,它只显示第一行与它的图标,标题,而不是任何其他人。我试着用相机图标,但它没有显示除第一行以外的任何内容。Recycler在导航抽屉中查看不显示多个项目

截图输出:

enter image description here

这里是所有的java文件

MainActivity.java

public class MainActivity extends AppCompatActivity { 

private Toolbar toolbar; 

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

    toolbar=(Toolbar) findViewById(R.id.app_bar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setDisplayShowTitleEnabled(true); 

    Nav_drawer nav_drawer=(Nav_drawer)getSupportFragmentManager().findFragmentById(R.id.f1); 
    nav_drawer.setUp(R.id.f1,(DrawerLayout)findViewById(R.id.activity_main),toolbar); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 
} 

Sadapter.java

public class Sadapter extends RecyclerView.Adapter<Sadapter.MyViewHolder> { 

private LayoutInflater inflater; 
List<Information> data= Collections.emptyList(); 

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


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

@Override 
public void onBindViewHolder(MyViewHolder holder, int position) { 

    Information current=data.get(position); 
    holder.title.setText(current.title); 
    holder.image.setImageResource(current.iconid); 

} 

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


class MyViewHolder extends RecyclerView.ViewHolder{ 

    TextView title; 
    ImageView image; 

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


} 
} 

NavigationDrawer.java

public class Nav_drawer extends Fragment { 

private RecyclerView recyclerView; 
public static final String PREF_NAME="test_pref"; 
public static final String KEY_USER_LERNED_DRAWER="user_learned_drawer"; 
private ActionBarDrawerToggle mactionBarDrawerToggle; 
private DrawerLayout mdrawerLayout; 
private Sadapter sadapter; 
private boolean mUserLearnedDrawer; 
private boolean mFromSavedInstantState; 
public View view; 


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

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mUserLearnedDrawer=Boolean.valueOf(readFromPreferences(getActivity(),KEY_USER_LERNED_DRAWER,"false")); 

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

} 

@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.drawer_list); 
    sadapter=new Sadapter(getActivity(),getData()); 
    recyclerView.setAdapter(sadapter); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 

    return layout; 
} 
public static List<Information> getData(){ 
    List<Information> data=new ArrayList<>(); 
    int[] icons={R.drawable.ic_home_black_18dp,R.drawable.ic_camera_alt_black_18dp}; 
    String[] titles={"Home","Camera"}; 

    for (int i=0;i<titles.length && i<icons.length; i++) 
    { 
     Information current= new Information(); 
     current.iconid=icons[i]; 
     current.title=titles[i]; 
     data.add(current); 

    } 
    return data; 
} 

public void setUp(int fragId, DrawerLayout drawerLayout, Toolbar toolbar) 
{ 
    view=getActivity().findViewById(fragId); 
    mdrawerLayout=drawerLayout; 

    mactionBarDrawerToggle=new ActionBarDrawerToggle(getActivity(),mdrawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close){ 


     @Override 
     public void onDrawerOpened(View drawerView) { 
      if (!mUserLearnedDrawer) 
      { 
       mUserLearnedDrawer=true; 
       saveToPreferences(getActivity(),KEY_USER_LERNED_DRAWER,mUserLearnedDrawer+""); 


      } 

      getActivity().invalidateOptionsMenu(); 
     } 

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


    if (!mUserLearnedDrawer&&!mFromSavedInstantState) 
    { 

     mdrawerLayout.openDrawer(view); 
    } 

    mdrawerLayout.addDrawerListener(mactionBarDrawerToggle); 
    mdrawerLayout.post(new Runnable() { 
     @Override 
     public void run() { 
      mactionBarDrawerToggle.syncState(); 
     } 
    }); 







} 



public void saveToPreferences(Context context,String preferenceName,String preferenceValue) 

{ 


    SharedPreferences sharedPreferences=context.getSharedPreferences(PREF_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_NAME,Context.MODE_PRIVATE); 
    return sharedPreferences.getString(preferenceName,defaultValue); 

} 



} 

Information.java

public class Information { 

int iconid; 
String title; 
} 

这里是所有的XML文件

mainactivity.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 

> 
<RelativeLayout 
    android:fitsSystemWindows="true" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.alshariq.materia.MainActivity"> 

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

<fragment 
    android:layout_width="286dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:id="@+id/f1" 
    app:layout="@layout/fragment_nav_drawer" 
    android:name="com.example.alshariq.materia.Nav_drawer" 
    tools:layout="@layout/fragment_nav_drawer"/> 


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

appbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:local="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="56dp" 
android:background="@color/colorPrimary" 
android:elevation="4dp" 

> 

</android.support.v7.widget.Toolbar> 

customrow.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<ImageView 
    android:id="@+id/im" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@mipmap/ic_launcher" 
    android:padding="8dp" 
    android:layout_gravity="center_vertical" 

    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:text="Dummy Text" 
    android:padding="8dp" 
    android:id="@+id/tv" 
    android:layout_gravity="center_vertical"/> 

</LinearLayout> 

fragmentnavdrawer.xml`

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.alshariq.materia.Nav_drawer" 
android:background="#ffffff" 

android:elevation="16dp"> 
<LinearLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="190dp" 
    android:background="@color/colorPrimary"> 

    <ImageView 

     android:layout_width="match_parent" 
     android:layout_height="77dp" 
     android:src="@mipmap/ic_launcher" 
     android:layout_marginTop="26dp"/> 
</LinearLayout> 

<android.support.v7.widget.RecyclerView 

    android:id="@+id/drawer_list" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:layout_below="@+id/container" 
    android:layout_alignParentStart="true"> 



</android.support.v7.widget.RecyclerView> 


</RelativeLayout> 
+0

集'height'。 –

+0

将android:layout_height =“wrap_content”更改为中的match_parent视图 –

+0

欢迎使用StackOverflow!请阅读用户指南,了解如何在发布问题之前提出一个好问题(http://stackoverflow.com/help/how-to-ask)谢谢 –

回答

0

在你customrow.xml 请注明layout_height="wrap_content"layout_height="100dp"

变化`wrap_content`的自定义行到您的customrow.xml这样,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

<ImageView 
    android:id="@+id/im" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:padding="8dp" 
    android:src="@mipmap/ic_launcher" /> 

<TextView 
    android:id="@+id/tv" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:padding="8dp" 
    android:text="Dummy Text" /> 
</LinearLayout> 
0

在customrow.xml使高度WRAP_CONTENT

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<ImageView 
    android:id="@+id/im" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@mipmap/ic_launcher" 
    android:padding="8dp" 
    android:layout_gravity="center_vertical" 

    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:text="Dummy Text" 
    android:padding="8dp" 
    android:id="@+id/tv" 
    android:layout_gravity="center_vertical"/> 

</LinearLayout>