我有一个问题。我想在我的Recyclerview导航抽屉中使用一个单独的分隔条。我该怎么做?现在我有一个DividerItemDecoration.java,每个项目后都有一个分隔线,这不是我想要的。我想在特定项目后设置一个单独的分隔线。这是DividerItemDecoration.java电码:
package com.***********.************;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
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);
}
}
这是NavigatonDrawerFragment.java的onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
mDrawerList = (RecyclerView) view.findViewById(R.id.drawerList);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mDrawerList.setLayoutManager(layoutManager);
mDrawerList.setHasFixedSize(true);
mDrawerList.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST));
mDrawerList.setItemAnimator(new DefaultItemAnimator());
final List<NavigationItem> navigationItems = getMenu();
NavigationDrawerAdapter adapter = new NavigationDrawerAdapter(navigationItems);
adapter.setNavigationDrawerCallbacks(this);
mDrawerList.setAdapter(adapter);
selectItem(mCurrentSelectedPosition);
return view;
}
简单解决方案https://pastebin.com/4J7sPaCF
public class SingleDividerDecoration extends DividerItemDecoration {
private final int height;
private int position = -1;
public SingleDividerDecoration(Context context, @DrawableRes int dividerId) {
super(context, LinearLayout.VERTICAL);
Drawable divider = ContextCompat.getDrawable(context, dividerId);
this.height = divider.getIntrinsicHeight();
setDrawable(divider);
}
public void setPosition(int position) {
this.position = position;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
int viewPosition = parent.getChildAdapterPosition(view);
int dividerHeight = viewPosition == position ? height : 0;
outRect.set(0, 0, 0, dividerHeight);
}
}
类似于Gmail的做法,我想在我的导航抽屉后面(在我的情况下)添加一个文本分隔符。我怎样才能做到呢?我想在附近的某个地方 ,对吧?
问题内容: 我有一个包含二进制数字的字符串。如何将其分成几对数字? 假设字符串是: 我想在每2个字符后添加一个分隔符,例如“:”(即冒号)。 我希望输出为: 我怎么能在Swift中做到这一点? 问题答案: Swift 5.1•Xcode 11或更高版本 测试中
我需要在RecyclerView的每个网格之间添加一个分隔器。 请帮帮我.
在字符串中分隔符的最后一次出现时拆分字符串的推荐Python习惯用法是什么?例如: 接受第二个参数,该参数是要拆分的分隔符的引用。与常规列表索引一样,表示从末尾开始的最后一个。如何做到这一点?
有没有办法防止分割器显示在最后一个项目之后?
我在操作栏(实际上是工具栏)中有一个菜单项,单击该菜单项时,会显示一个可供选择的项目列表,类似于单选按钮: 我需要在这个项目列表下面放一个项目,在它和列表之间有一个分隔符。与材料设计指南所示类似(摘自此处): 编辑:下面是我想做的事情的草图: 我找不到办法。 我找到的唯一可能的解决方案是: > 更改活动主题(此处),但这也会影响活动的其他菜单项 方法,当菜单项出现在操作栏上时,在它们之间放置分隔符