Issue
According to Google's RecyclerView documentation, you can set a specific LayoutManager
from within a layout file by specifying its class name in the RecyclerView
's 'layoutManager' attribute. It also specifically mentions LayoutManager
has a constructor which accepts an AttributeSet
.
My question is since you're specifying the LayoutManager
via an attribute on the RecyclerView
's element, and not as its own element, where/how do you set the attributes targeted for the LayoutManager
itself?
My guess is you add them directly to the RecyclerView
element as well. This would make sense as inside the RecyclerView
's constructor, when it instantiates the LayoutManager
specified in the 'layoutManager' attribute, it could simply pass through the same AttributeSet
that was passed in to it. However, this is only a guess.
Here's an example of what I'm thinking is the correct way:
<MyRecyclerView
app:layoutManager=".MyLayoutManager"
app:attrOnlyUsedByRecyclerView="I'm used by MyRecyclerView"
app:attrOnlyUsedByLayoutManager="I'm used by MyLayoutManager" />
Note how all three attributes are technically set on the MyRecyclerView
element, but the thinking is the third attribute is ignored by, and passed through from MyRecyclerView
's constructor into MyLayoutManager
's constructor.
I'm trying to build a demo app to test that theory out now, but in the meantime, can anyone clarify for sure, or at least point me in the right direction if this is incorrect?
Solution
Based on a little testing it does seems that you can just apply the relevant attributes directly to the RecyclerView
element and they will be passed through to the LayoutManager
.
For example, the relevant constructor for LinearLayoutManager
is:
/**
* Constructor used when layout manager is set in XML by RecyclerView attribute
* "layoutManager". Defaults to vertical orientation.
*
* @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_android_orientation
* @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_reverseLayout
* @attr ref android.support.v7.recyclerview.R.styleable#RecyclerView_stackFromEnd
*/
public LinearLayoutManager(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
Properties properties = getProperties(context, attrs, defStyleAttr, defStyleRes);
setOrientation(properties.orientation);
setReverseLayout(properties.reverseLayout);
setStackFromEnd(properties.stackFromEnd);
setAutoMeasureEnabled(true);
}
...and here's how you specify the 'stackFromEnd' attribute for the LayoutManager
. (Note how it's set it on the RecyclerView
element even though it's destined for the LayoutManager
.)
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:stackFromEnd="true" />
Answered By - Brian Yencho
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.