在recyclerView的两侧添加DividerItemDecoration

时间:2017-07-01 03:27:48

标签: android user-interface android-recyclerview

我正在尝试为Android中的应用程序设置 GUI 我是新手。

首先,我制作一个水平recyclerView,添加DividerItemDecoration一个自定义drawable,并设置LinearSnapHelper,以便始终按住中心。

它可以工作,但左侧是屏幕旁边的,因为dividerItemDecorator只在元素之间放置了这条线。

enter image description here

存在任何方式将分隔符放在recycler的开头,我尝试了一些填充或边距但是当我滑动它时“切割”到达屏幕的末尾

提前抱歉为英语

我的代码是

MainActivity

@BindView(R.id.rvRecentNews)
RecyclerView rvRecentNews;
private ArrayList<String> horizontalList;
private HorizontalAdapter horizontalAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ButterKnife.bind(this);

horizontalList=new ArrayList<>();
    horizontalList.add("horizontal 1");
    horizontalList.add("horizontal 2");
    horizontalList.add("horizontal 3");
    horizontalList.add("horizontal 4");
    horizontalList.add("horizontal 5");
    horizontalList.add("horizontal 6");
    horizontalList.add("horizontal 7");
    horizontalList.add("horizontal 8");
    horizontalList.add("horizontal 9");
    horizontalList.add("horizontal 10");

    horizontalAdapter=new HorizontalAdapter(horizontalList);

    LinearLayoutManager horizontalLayoutManagaer
            = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
    rvRecentNews.setLayoutManager(horizontalLayoutManagaer);

    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(rvRecentNews.getContext(),
            horizontalLayoutManagaer.getOrientation());
    dividerItemDecoration.setDrawable(getApplicationContext().getResources().getDrawable(R.drawable.line_divider));

    rvRecentNews.addItemDecoration(dividerItemDecoration);

    SnapHelper helper = new LinearSnapHelper();
    helper.attachToRecyclerView(rvRecentNews);

    rvRecentNews.setAdapter(horizontalAdapter);


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:orientation="vertical"
tools:context="com.sgd.pawfriends.MainActivity"
tools:showIn="@layout/app_bar_main">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:layout_marginTop="8dp"
            android:id="@+id/rvRecentNews"
            android:layout_width="match_parent"
            android:layout_height="200dp"

            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>

</ScrollView>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="salir"
    android:onClick="logout"
    />

</LinearLayout>

提前致谢

2 个答案:

答案 0 :(得分:0)

试试这个:

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    print("AVAudioSession Category Playback OK")
    do {
        try AVAudioSession.sharedInstance().setActive(true)
        print("AVAudioSession is Active")
    } catch let error as NSError {
        print(error.localizedDescription)
    }
} catch let error as NSError {
    print(error.localizedDescription)
}

UIApplication.shared.beginReceivingRemoteControlEvents()

<android.support.v7.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" android:paddingLeft="10dp"/> 表示仅在滚动到达结束/开始点时剪辑。

答案 1 :(得分:0)

我在此页面中发现了许多用于实现自定义ItemDecoration

的选项

https://www.bignerdranch.com/blog/a-view-divided-adding-dividers-to-your-recyclerview-with-itemdecoration/

我只会改变

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(rvRecentNews.getContext(),
            horizontalLayoutManagaer.getOrientation());

    dividerItemDecoration.setDrawable(ContextCompat.getDrawable(this,R.drawable.line_divider));

    rvRecentNews.addItemDecoration(dividerItemDecoration);

这个

rvRecentNews.addItemDecoration(new StartOffsetItemDecoration(ContextCompat.getDrawable(this,R.drawable.line_divider)));

class StartOffsetItemDecoration

public class StartOffsetItemDecoration extends RecyclerView.ItemDecoration {

private int mOffsetPx;
private Drawable mOffsetDrawable;
private int mOrientation;


public StartOffsetItemDecoration(int offsetPx) {
    mOffsetPx = offsetPx;
}

public StartOffsetItemDecoration(Drawable offsetDrawable) {
    mOffsetDrawable = offsetDrawable;
}


@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    super.getItemOffsets(outRect, view, parent, state);


    mOrientation = ((LinearLayoutManager) parent.getLayoutManager()).getOrientation();
    if (mOrientation == LinearLayoutManager.HORIZONTAL) {
        if (mOffsetPx > 0) {
            outRect.left = mOffsetPx;
            outRect.right = mOffsetPx;
        } else if (mOffsetDrawable != null) {
            outRect.left = mOffsetDrawable.getIntrinsicWidth();
            outRect.right = mOffsetDrawable.getIntrinsicWidth();
        }
    } else if (mOrientation == LinearLayoutManager.VERTICAL) {
        if (mOffsetPx > 0) {
            outRect.top = mOffsetPx;
        } else if (mOffsetDrawable != null) {
            outRect.top = mOffsetDrawable.getIntrinsicHeight();
        }
    }
}

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    super.onDraw(c, parent, state);
    if (mOffsetDrawable == null) {
        return;
    }

    if (mOrientation == LinearLayoutManager.HORIZONTAL) {
        drawOffsetHorizontal(c, parent);
    } else if (mOrientation == LinearLayoutManager.VERTICAL) {
        drawOffsetVertical(c, parent);
    }
}


private void drawOffsetHorizontal(Canvas canvas, RecyclerView parent) {
    int parentTop = parent.getPaddingTop();
    int parentBottom = parent.getHeight() - parent.getPaddingBottom();
    int parentLeft = parent.getPaddingLeft();
    int offsetDrawableRight = parentLeft + mOffsetDrawable.getIntrinsicWidth();

    mOffsetDrawable.setBounds(parentLeft, parentTop, offsetDrawableRight, parentBottom);
    mOffsetDrawable.draw(canvas);
}

private void drawOffsetVertical(Canvas canvas, RecyclerView parent) {
    int parentLeft = parent.getPaddingLeft();
    int parentRight = parent.getWidth() - parent.getPaddingRight();
    int parentTop = parent.getPaddingTop();
    int offsetDrawableBottom = parentTop + mOffsetDrawable.getIntrinsicHeight();

    mOffsetDrawable.setBounds(parentLeft, parentTop, parentRight, offsetDrawableBottom);
    mOffsetDrawable.draw(canvas);
}

感谢David Greenhalgh和Shayan Pourvatan的作者创建自定义类的想法