Android attrs获取布局文件

11 篇文章 0 订阅

1. 在style中:

<declare-styleable name="SmartRecyclerView">
    <attr name="smr_empty_layout" format="reference" />
    <attr name="smr_error_layout" format="reference" />
</declare-styleable>

 

2. 在自定义控件SmartRecyclerView中:

public SmartRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
...
TypedArray ta = mContext.obtainStyledAttributes(attrs, R.styleable.SmartRecyclerView); 
mEmptyLayoutId = ta.getResourceId(R.styleable.SmartRecyclerView_smr_empty_layout, -1); 
mErrorLayoutId = ta.getResourceId(R.styleable.SmartRecyclerView_smr_error_layout, -1); 
ta.recycle();
}
...

public void setLayoutManager(RecyclerView.LayoutManager layout) {
    mRecyclerView.setLayoutManager(layout);
    if (layout != null) {
        //inflate之前必须先设定recyclerView的LayoutManager
        if (mEmptyLayoutId > -1) {
            mEmptyView = mInflater.inflate(mEmptyLayoutId, mRecyclerView, false);
        }
        if (mErrorLayoutId > -1) {
            mErrorView = mInflater.inflate(mErrorLayoutId, mRecyclerView, false);
        }
    }
}

3. 在布局文件中使用:

<com.xxx.view.SmartRecyclerView
    android:id="@+id/smart_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    rcv:smr_empty_layout="@layout/empty_layout"
    rcv:smr_error_layout="@layout/error_layout" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值