我正在关注slidenerd Material design tutorial(Android RecyclerView示例第1,2,3,4部分)&我正在使用真实设备来测试我的android应用程序。我已成功构建导航抽屉和放大器当我尝试通过添加适配器(ThalakazAdapter.java)将RecylerView添加到其中时,应用程序很快就会停止。
这是我的NavigationDrawerFragment.java
Regexp.union("?","!",".")
这是我的适配器(ThalakazAdapter.java)
/\?|!|\./
我的信息.java类
package com.thaalakaz.nadeesha.materialdesignapp1;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
//import android.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.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class NavigationDrawerFragment extends Fragment {
private RecyclerView recyclerView;
public static final String PREF_FILE_NAME = "testpref";
public static final String KAY_USER_LEARNED_DRAWER ="user_learned_drawer";
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ThalakazAdapter adapter;
private boolean mUserLearnedDrawer;
private boolean mFromSavedInstanceState;
private View containerView;
public NavigationDrawerFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mUserLearnedDrawer=Boolean.valueOf(readFromPreferences(getActivity(),KAY_USER_LEARNED_DRAWER,"false"));
if (savedInstanceState != null){
mFromSavedInstanceState = true;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.drawer_layout);
adapter = new ThalakazAdapter(getActivity(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return layout;
}
public static List<Information> getData(){
List<Information> data = new ArrayList<>();
int[] icons ={R.drawable.ic_m,R.drawable.ic_m1,R.drawable.ic_m2,R.drawable.ic_m3};
String[] titles = {"Nadeesha","Thilakarathne","Semini","Roshani"};
for (int i=0;i<titles.length && i<icons.length;i++){
Information current = new Information();
current.iconId = icons[i];
current.titlel = titles[i];
data.add(current);
}
return data;
}
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);
if(!mUserLearnedDrawer){
mUserLearnedDrawer = true;
saveToPreferences(getActivity(),KAY_USER_LEARNED_DRAWER,mUserLearnedDrawer+"");
}
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
if (slideOffset<0.6){
toolbar.setAlpha(1-slideOffset);
}
}
};
if (!mUserLearnedDrawer && mFromSavedInstanceState){
mDrawerLayout.openDrawer(containerView);
}
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public static void saveToPreferences(Context context,String prferenceName,String preferenceValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(prferenceName,preferenceValue);
editor.apply();
}
public static String readFromPreferences(Context context,String prferenceName,String defaultValue){
SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_FILE_NAME,Context.MODE_PRIVATE);
return sharedPreferences.getString(prferenceName,defaultValue);
}
}
您可以从here
查看我的整个项目logcat的
package com.thaalakaz.nadeesha.materialdesignapp1;
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.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
/**
* Created by hp on 7/22/2015.
*/
public class ThalakazAdapter extends RecyclerView.Adapter <ThalakazAdapter.myViewHolder>{
private LayoutInflater inflater;
List<Information> data = Collections.emptyList();
public ThalakazAdapter(Context context, List<Information> data){
inflater=LayoutInflater.from(context);
this.data = data;
}
@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;
}
@Override
public void onBindViewHolder(myViewHolder holder, int position) {
Information current = data.get(position);
holder.title.setText(current.titlel);
holder.icon.setImageResource(current.iconId);
}
@Override
public int getItemCount() {
return 0;
}
class myViewHolder extends RecyclerView.ViewHolder{
ImageView icon;
TextView title;
public myViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.listText);
icon = (ImageView) itemView.findViewById(R.id.listIcon);
}
}
}
fragment_navigation_drawer.xml
package com.thaalakaz.nadeesha.materialdesignapp1;
/**
* Created by hp on 7/22/2015.
*/
public class Information {
int iconId;
String titlel;
}
答案 0 :(得分:2)
你正在寻找错误的id。变化
recyclerView = (RecyclerView) layout.findViewById(R.id.drawer_layout);
与
recyclerView = (RecyclerView) layout.findViewById(R.id.drawerList);
我还注意到,在你的Adapter
构造函数中,你正在向自己说明这个数据。变化
this.data = this.data;
到
this.data = data
且getItemCount
不应该return 0
,但是数据的大小(如果它不为空)