我实现了导航抽屉适配器,我想看一下这样的事情:
但是我有错误,我不知道为什么一个"如果"正在使用。当我删除行布局并且只删除header.xml和drawer_list_item.xml时,它可以工作。我刚刚学习,你可以帮助我吗?
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.example.test.adapters.NavDrawerAdapter.onBindViewHolder(NavDrawerAdapter.java:182)
at com.example.test.adapters.NavDrawerAdapter.onBindViewHolder(NavDrawerAdapter.java:31)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4138)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3448)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
at android.view.View.layout(View.java:15684)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:931)
at android.view.View.layout(View.java:15684)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15684)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15684)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15684)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15684)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15684)
at android.view.ViewGroup.layout(ViewGroup.java:4981)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2186)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1920)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1106)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6018)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:792)
at android.view.Choreographer.doCallbacks(Choreographer.java:596)
at android.view.Choreographer.doFrame(Choreographer.java:557)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:778)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5696)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
这是我的适配器
public class NavDrawerAdapter extends RecyclerView.Adapter<NavDrawerAdapter.ViewHolder> {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private static final int TYPE_LINE = 2;
private String mNavTitles[];
private Integer mNavTitlesId[];
private String name;
private String email;
Context context;
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
int Holderid;
TextView textView;
ImageView mHashImageView;
ImageView profile;
TextView Name;
TextView email;
Context mContext;
String mTitles[];
Integer mTitlesId[];
TextView mLine;
public ViewHolder(View itemView,int ViewType, Context context) { // Creating ViewHolder Constructor with View and viewType As a parameter
super(itemView);
mContext = context;
itemView.setClickable(true);
itemView.setOnClickListener(this);
if(ViewType == TYPE_ITEM) {
textView = (TextView) itemView.findViewById(R.id.hashTextView)
mHashImageView = (ImageView) itemView.findViewById(R.id.hashImageView);
Holderid = 1;
} else if(ViewType == TYPE_HEADER) {
Name = (TextView) itemView.findViewById(R.id.name);
email = (TextView) itemView.findViewById(R.id.email);
profile = (ImageView) itemView.findViewById(R.id.circleView);
Holderid = 0;
} else if(ViewType == TYPE_LINE){
mLine = (TextView) itemView.findViewById(R.id.lineTextView);
Holderid = 2;
}
}
@Override
public void onClick(View v) {
Toast.makeText(mContext, "The Item Clicked is: " + getPosition(), Toast.LENGTH_SHORT).show();
}
}
public NavDrawerAdapter(String Name, String Email, Context context, String[] mTitles, int[] mTitlesId){
name = Name;
email = Email;
this.context = context;
mNavTitles = mTitles;
mNavTitlesId = mTitlesId;
}
@Override
public NavDrawerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_list_item,parent,false);
ViewHolder vhItem = new ViewHolder(v,viewType,context);
return vhItem;
} else if (viewType == TYPE_HEADER) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header,parent,false);
ViewHolder vhHeader = new ViewHolder(v,viewType,context);
return vhHeader;
} else if(viewType == TYPE_LINE) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.line,parent,false);
ViewHolder vhLine = new ViewHolder(v,viewType,context);
return vhLine;
}
return null;
}
@Override
public void onBindViewHolder(NavDrawerAdapter.ViewHolder holder, int position) {
if(holder.Holderid == 1) {
holder.textView.setText(mNavTitles[position - 1]);
}
else if(holder.Holderid == 0){
holder.Name.setText(name);
holder.email.setText(email);
} else {
holder.mLine.setText("Text");
}
}
@Override
public int getItemCount() {
return mNavTitles.length+2; // the number of items in the list will be +1 the titles including the header view.
}
@Override
public int getItemViewType(int position) {
if (position == 0)
return TYPE_HEADER;
else if (position == 1)
return TYPE_ITEM;
return TYPE_LINE;
}
}
我在MainActivity中使用它:
mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View
mRecyclerView.setHasFixedSize(true); // Letting the system know that the list objects are of fixed size
mAdapter = new NavDrawerAdapter(NAME, EMAIL, PROFILE, this.getApplicationContext());
mRecyclerView.setAdapter(mAdapter); // Setting the adapter to RecyclerView
mLayoutManager = new LinearLayoutManager(this); // Creating a layout Manager
mRecyclerView.setLayoutManager(mLayoutManager); // Setting the layout Manager
mDrawerLayout = (DrawerLayout) findViewById(R.id.DrawerLayout); // mDrawerLayout object Assigned to the view
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open_drawer, R.string.close_drawer) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
}; // mDrawerLayout Toggle Object Made
mDrawerLayout.setDrawerListener(mDrawerToggle); // mDrawerLayout Listener set to the mDrawerLayout toggle
mDrawerToggle.syncState();
这是header.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="178dp"
android:background="@drawable/background_night_sky"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:textColor="#ffffff"
android:text="Name Surname"
android:textSize="14sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:layout_marginLeft="16dp"
android:layout_marginTop="5dp"
android:text="name.surname@gmail.com"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/avatar_small"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"
android:id="@+id/circleView"
/>
</RelativeLayout>
这是line.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/lineTextView"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimary"/>
</LinearLayout>
这是drawer_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:orientation="horizontal"
android:clickable="true"
android:background="?attr/selectableItemBackground">
<ImageView
android:layout_width="@dimen/hash_size_y"
android:layout_height="@dimen/hash_size_x"
android:scaleType="fitCenter"
android:layout_marginStart="8dp"
android:layout_alignParentStart="true"
android:id="@+id/hashImageView"
android:src="@drawable/ic_hash_orange_small"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="HashTag"
android:id="@+id/hashTextView"
android:textColor="@color/drawer_background"
android:textStyle="bold|italic"
android:layout_toEndOf="@id/hashImageView"/>
</RelativeLayout>