导航抽屉中的Recycler View未显示多个项目

时间:2017-03-24 07:10:37

标签: android android-layout listview android-fragments

我正在尝试使用回收器视图制作导航抽屉,但主要问题是,当我运行它时,它仅显示带有其图标的第一行,并且其中标题为home而不是任何其他行。我尝试使用Camera图标,但除第一行外没有显示任何内容。

输出屏幕截图:

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>

2 个答案:

答案 0 :(得分:0)

在customrow.xml中make height 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>

答案 1 :(得分:0)

customrow.xml中 请指定layout_height="wrap_content"layout_height="100dp"

像这样更改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>