我已经实现了这样的自定义视图:
public class HeaderView extends LinearLayout {
@Bind(R.id.bill_summary_layout)
LinearLayout summaryLayout;
@Bind(R.id.bill_balance)
TextView balance;
@Bind(R.id.bill_closedate)
TextView closedDate;
@Bind(R.id.bill_status)
TextView status;
@Bind(R.id.bill_partial_overdue)
ViewStub partialOverdueView;
@Bind(R.id.bill_partial_closed)
ViewStub partialClosedView;
@Bind(R.id.bill_partial_open)
ViewStub partialOpenView;
Bill bill;
public HeaderView(Context context) {
super(context);
initUI(context);
}
public HeaderView(Context context, AttributeSet attrs) {
super(context, attrs);
initUI(context);
}
public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initUI(context);
}
private void initUI(Context context) {
LayoutInflater.from(context).inflate(R.layout.header_view, this);
}
public Bill getBill() {
return bill;
}
public void setBill(Bill bill) {
this.bill = bill;
notifyMustRebind();
}
public void notifyMustRebind() {
setupUI();
}
private void setupUI() {
if(this.bill == null)
throw new RuntimeException("O objeto bill tem que estar setado e nao ser nulo. Talvez esqueceu de chamar o setBill?");
switch (bill.getState()) {
case OVERDUE:
setOverdueState();
break;
case OPEN:
setOpenState();
break;
case CLOSED:
setClosedState();
break;
case FUTURE:
setFutureState();
break;
}
}
private void setOverdueState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.overdue));
LayoutInflater.from(getContext()).inflate(R.layout.partial_overdue_view, this, true);
partialOverdueView.inflate();
}
private void setOpenState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.open));
partialOpenView.inflate();
}
private void setClosedState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.closed));
partialClosedView.inflate();
}
private void setFutureState(){
summaryLayout.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.future));
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
ButterKnife.bind(this);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
ButterKnife.unbind(this);
}
}
header_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/bill_summary_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_content_arrow"
android:backgroundTint="@color/colorAccent"
android:orientation="vertical"
android:paddingBottom="@dimen/header_margin"
android:paddingTop="@dimen/header_margin"
>
<TextView
android:id="@+id/bill_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="@dimen/header_text_margin"
android:text="R$ 2.300,10"
android:textColor="@android:color/white"
android:textSize="22sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/bill_closedate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="@dimen/header_text_margin"
android:text="Vencimento 15 MAI"
android:textColor="@android:color/white"
android:textSize="18sp"
/>
<TextView
android:id="@+id/bill_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="@dimen/header_text_margin"
android:text="Fechamento em 5 de Julho"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textSize="12sp"
/>
</LinearLayout>
<ViewStub
android:id="@+id/bill_partial_overdue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/partial_overdue_view"
/>
<ViewStub
android:id="@+id/bill_partial_closed"
layout="@layout/partial_closed_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ViewStub
android:id="@+id/bill_partial_open"
layout="@layout/partial_open_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorAccent"/>
</LinearLayout>
上面的视图位于另一个名为bill_view的视图中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<br.com.leonardo2204.nubanktest.ui.custom.HeaderView
android:id="@+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/list_header_padding">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:paddingLeft="@dimen/list_header_padding"
android:paddingStart="@dimen/list_header_padding"
android:text="De 5 Mar até 5 abr"
android:textAllCaps="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingEnd="@dimen/list_header_padding"
android:paddingRight="@dimen/list_header_padding"
android:text="Valores em R$"
android:textAllCaps="true" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
上面的视图在PagerAdapter中膨胀,如下所示:
@Override
public Object instantiateItem(ViewGroup container, int position) {
View v = LayoutInflater.from(container.getContext()).inflate(R.layout.bill_view, container, false);
HeaderView header = (HeaderView) v.findViewById(R.id.header_view);
header.setBill(billList.get(position));
container.addView(v);
return v;
}
问题是,我在下面得到了堆栈跟踪:
Process: br.com.leonardo2204.nubanktest, PID: 10815
java.lang.IllegalArgumentException: ViewStub must have a valid layoutResource
at android.view.ViewStub.inflate(ViewStub.java:289)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setClosedState(HeaderView.java:112)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setupUI(HeaderView.java:91)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.notifyMustRebind(HeaderView.java:76)
at br.com.leonardo2204.nubanktest.ui.custom.HeaderView.setBill(HeaderView.java:72)
at br.com.leonardo2204.nubanktest.ui.adapter.ViewPagerAdapter.instantiateItem(ViewPagerAdapter.java:30)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:870)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
at android.view.View.measure(View.java:17547)
我做错了什么?除了instantiateView
内部之外,是否有更好的方法来扩展标题视图?
谢谢!
答案 0 :(得分:4)
USE
android:layout="@layout/xyz"
而不是
layout="@layout/xyz"