Разделитель в RecyclerView Android

У меня есть DrawerNavigation, это реплики RecyclerView из 14 элементов. И мне нужно разделить элементы списка с помощью делителя, но мне не нужно делать это с каждым элементом списка. Например, мне нужно установить разделитель после 2 и 5 элемента списка. Я нашел, как сделать разделители для всего списка, но когда я использую drawerList.addItemDecoration(itemDecoration, 2);, он возвращает ошибку. Это основной класс:

drawerList.setHasFixedSize(true);
    mAdapter = new DrawerListItemAdapter(TITLES,ICONS,getResources().getString(R.string.nickname),
            getResources().getString(R.string.drawer_second_line),PROFILE, this);       // Creating the Adapter of DrawerListItemAdapter class(which we are going to see in a bit)
    // And passing the titles,icons,header view name, header view email,
    // and header view profile picture
    drawerList.setAdapter(mAdapter);                              // Setting the adapter to RecyclerView
    mLayoutManager = new LinearLayoutManager(this);                 // Creating a layout Manager
    drawerList.setLayoutManager(mLayoutManager);
    drawerListClick(drawerList, this, toolbar, nameProfile);
    mDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer,R.string.closeDrawer){
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            // code here will execute once the drawer is opened( As I dont want anything happened whe drawer is
            // open I am not going to put anything here)
        }
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            // Code here will execute once drawer is closed
        }
    }; // Drawer Toggle Object Made
    drawerLayout.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle
    mDrawerToggle.syncState();
    RecyclerView.ItemDecoration itemDecoration = new
            DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST);
    Log.d("SIZE OF LIST", "Size " + drawerList.getAdapter().getItemCount());
    drawerList.addItemDecoration(itemDecoration);

Это работает нормально, но работает для всех элементов списка
Это адаптер drawerList:

public class DrawerListItemAdapter extends RecyclerView.Adapter<DrawerListItemAdapter.ViewHolder> {

private static final int TYPE_HEADER = 0;  // Declaring Variable to Understand which View is being worked on
// IF the view under inflation and population is header or Item
private static final int TYPE_ITEM = 1;
private String mNavTitles[]; // String Array to store the passed titles Value from MainActivity.java
private int mIcons[];       // Int Array to store the passed icons resource value from MainActivity.java
private String name;        //String Resource for header View Name
private int profile;        //int Resource for header view profile picture
private String email;       //String Resource for header view email
private BaseActivity context;


// Creating a ViewHolder which extends the RecyclerView View Holder
// ViewHolder are used to to store the inflated views in order to recycle them

public static class ViewHolder extends RecyclerView.ViewHolder {
    int Holderid;

    TextView textView;
    ImageView imageView;
    ImageView profile;
    ImageView dad;
    ImageView mam;
    TextView Name;
    TextView email;
    RelativeLayout layout;


    public ViewHolder(View itemView,int ViewType) {                 // Creating ViewHolder Constructor with View and viewType As a parameter
        super(itemView);
        // Here we set the appropriate view in accordance with the the view type as passed when the holder object is created
        if(ViewType == TYPE_ITEM) {
            textView = (TextView) itemView.findViewById(R.id.rowText); // Creating TextView object with the id of textView from item_row.xml
            imageView = (ImageView) itemView.findViewById(R.id.rowIcon);// Creating ImageView object with the id of ImageView from item_row.xml
            Holderid = 1;                                               // setting holder id as 1 as the object being populated are of type item row
            layout = (RelativeLayout) itemView.findViewById(R.id.item_layout);
        }
        else{
            Name = (TextView) itemView.findViewById(R.id.name);         // Creating Text View object from header.xml for name
            email = (TextView) itemView.findViewById(R.id.email);       // Creating Text View object from header.xml for email
            profile = (ImageView) itemView.findViewById(R.id.circleView);// Creating Image view object from header.xml for profile pic
            Holderid = 0;                                                // Setting holder id = 0 as the object being populated are of type header view
            dad = (ImageView) itemView.findViewById(R.id.circleViewDad);
            mam = (ImageView) itemView.findViewById(R.id.circleViewMam);
        }
    }


}

public DrawerListItemAdapter(String Titles[], int Icons[], String Name, String Email, int Profile, BaseActivity context){ // DrawerListItemAdapter Constructor with titles and icons parameter
    // titles, icons, name, email, profile pic are passed from the main activity as we
    mNavTitles = Titles;                //have seen earlier
    mIcons = Icons;
    name = Name;
    email = Email;
    profile = Profile;                     //here we assign those passed values to the values we declared here
    //in adapter
    this.context = context;
}



//Below first we ovverride the method onCreateViewHolder which is called when the ViewHolder is
//Created, In this method we inflate the item_row.xml layout if the viewType is Type_ITEM or else we inflate header.xml
// if the viewType is TYPE_HEADER
// and pass it to the view holder

@Override
public DrawerListItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, final int viewType) {
    if (viewType == TYPE_ITEM) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row,parent,false); //Inflating the layout
        ViewHolder vhItem = new ViewHolder(v,viewType); //Creating ViewHolder and passing the object of type view
        return vhItem; // Returning the created object
        //inflate your layout and pass it to view holder
    } else if (viewType == TYPE_HEADER) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header,parent,false); //Inflating the layout
        ViewHolder vhHeader = new ViewHolder(v,viewType); //Creating ViewHolder and passing the object of type view
        return vhHeader; //returning the object created
    }
    return null;
}
//Next we override a method which is called when the item in a row is needed to be displayed, here the int position
// Tells us item at which position is being constructed to be displayed and the holder id of the holder object tell us
// which view type is being created 1 for item row
@Override
public void onBindViewHolder(final DrawerListItemAdapter.ViewHolder holder, int position) {
    if(holder.Holderid ==1) {                              // as the list view is going to be called after the header view so we decrement the
        // position by 1 and pass it to the holder while setting the text and image
        holder.textView.setText(mNavTitles[position - 1]); // Setting the Text with the array of our Titles
        holder.imageView.setImageResource(mIcons[position - 1]);// Settimg the image with array of our icons
    }
    else{

        holder.profile.setImageResource(profile);           // Similarly we set the resources for header view
        holder.profile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Bundle bndl = new Bundle();
                //bndl.putInt("ToolbarHeight", toolbar.getLayoutParams().height);
                //ProfileFragment eventsViewPagerFragment = new ProfileFragment();
                //FragmentTransaction transaction = context.getSupportFragmentManager()
                //        .beginTransaction();
                //eventsViewPagerFragment.setArguments(bndl);
                //transaction.replace(R.id.general_fragment, eventsViewPagerFragment);
                //toolbar.setBackgroundColor(Color.TRANSPARENT);
                //transaction.commit();
                Intent in = new Intent(context, ProfileActivity.class);
                in.putExtra("Image", 0);
                context.startActivity(in);
            }
        });
        holder.dad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(context, ProfileActivity.class);
                in.putExtra("Image", 2);
                context.startActivity(in);
            }
        });
        holder.mam.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in = new Intent(context, ProfileActivity.class);
                in.putExtra("Image", 1);
                context.startActivity(in);
            }
        });
        holder.Name.setText(name);
        holder.email.setText(email);
    }
}
// This method returns the number of items present in the list
@Override
public int getItemCount() {
    return mNavTitles.length+1; // the number of items in the list will be +1 the titles including the header view.
}

// Witht the following method we check what type of view is being passed
@Override
public int getItemViewType(int position) {
    if (isPositionHeader(position))
        return TYPE_HEADER;
    return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
    return position == 0;
}
}

И этот класс для делителя:

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private static final int[] ATTRS = new int[]{
        android.R.attr.listDivider
};

public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;

public DividerItemDecoration(Context context, int orientation) {
    final TypedArray a = context.obtainStyledAttributes(ATTRS);
    mDivider = a.getDrawable(0);
    a.recycle();
    setOrientation(orientation);
}

public void setOrientation(int orientation) {
    if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
        throw new IllegalArgumentException("invalid orientation");
    }
    mOrientation = orientation;
}

@Override
public void onDraw(Canvas c, RecyclerView parent) {
    if (mOrientation == VERTICAL_LIST) {
        drawVertical(c, parent);
    } else {
        drawHorizontal(c, parent);
    }
}

public void drawVertical(Canvas c, RecyclerView parent) {
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int top = child.getBottom() + params.bottomMargin;
        final int bottom = top + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

public void drawHorizontal(Canvas c, RecyclerView parent) {
    final int top = parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount; i++) {
        final View child = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                .getLayoutParams();
        final int left = child.getRight() + params.rightMargin;
        final int right = left + mDivider.getIntrinsicHeight();
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
    if (mOrientation == VERTICAL_LIST) {
        outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    } else {
        outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    }
}

}

Мой вопрос: как сделать разделитель между определенными элементами списка?


person metalink    schedule 09.01.2016    source источник


Ответы (1)


Вы можете использовать эту библиотеку https://github.com/yqritc/RecyclerView-FlexibleDivider для получить желаемый эффект.

Функция, которая на самом деле поможет вам скрыть разделитель, следующая:

 @Override
public boolean shouldHideDivider(int position, RecyclerView parent) {
    if (position == 14 || position == 15) {
        return true;
    }
    return false;
}

Если вы ознакомитесь с классами ComplexActivity и ComplexAdapter в образце, вы узнаете, как на самом деле использовать эту функцию.

person Mukesh Rana    schedule 09.01.2016
comment
Вы должны поделиться некоторым кодом и объяснением для лучшего качества ответа. Потому что, когда ссылка больше недоступна, ответ больше не будет иметь смысла. - person DhiaTN; 09.01.2016
comment
@DhiaTN Я понимаю вашу точку зрения, но пользователю необходимо добавить эту зависимость от библиотеки в свой проект, поэтому в любом случае необходимо указать ссылку в ответе, чтобы любой, кто ищет это решение, мог пройти через образец программы. - person Mukesh Rana; 09.01.2016