我想创建自定义视图,需要根据不同的情况更改背景资源。
我的CustomViewClass:
public class MessageCloudView extends LinearLayout{
private enum State {
STABLE, CHECKED, HOVERED, RESEND, ERROR;
}
/**
* 0 - stable view background
* 1 - checked_background
* 2 - hovered_background
* 3 - resend_background
*/
private State currentState, prevState;
private int[] res;
public MessageCloudView(Context context) {
super(context);
init(context, null);
}
public MessageCloudView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
this.currentState = State.STABLE;
this.res = new int[4];
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MessageCloudView, 0, 0);
try {
res[0] = a.getInteger(R.styleable.MessageCloudView_stable_background, 0);
res[1] = a.getInteger(R.styleable.MessageCloudView_checked_background, 0);
res[2] = a.getInteger(R.styleable.MessageCloudView_hovered_background, 0);
res[3] = a.getInteger(R.styleable.MessageCloudView_resend_background, 0);
} finally {
a.recycle();
}
setBackgroundResource(res[0]);
}
public MessageCloudView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public MessageCloudView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context, attrs);
}
@Override
public void setPressed(boolean pressed) {
this.prevState = this.currentState;
if(pressed){
this.currentState = State.HOVERED;
} else {
this.currentState = this.prevState;
}
super.setPressed(pressed);
onDrawableCheck();
}
@Override
public void setSelected(boolean selected) {
this.prevState = this.currentState;
if(selected){
this.currentState = State.HOVERED;
} else {
this.currentState = this.prevState;
}
super.setSelected(selected);
onDrawableCheck();
}
private void onDrawableCheck(){
if(this.currentState == State.STABLE) setBackgroundResource(res[0]);
else if(this.currentState == State.CHECKED) setBackgroundResource(res[0]);
else if(this.currentState == State.HOVERED) setBackgroundResource(res[0]);
else if(this.currentState == State.RESEND) setBackgroundResource(res[0]);
else if(this.currentState == State.ERROR) setBackgroundResource(res[0]);
}
public void setResent(boolean isResent){
if(isResent) this.currentState = State.RESEND;
else this.currentState = State.STABLE;
onDrawableCheck();
}
public void setError(boolean isError){
}
}
attrs 文件包含:
<declare-styleable name="MessageCloudView">
<attr name="stable_background" format="integer" />
<attr name="checked_background" format="integer" />
<attr name="hovered_background" format="integer" />
<attr name="resend_background" format="integer" />
</declare-styleable>
xml文件中的:
<custom.MessageCloudView
android:id="@+id/ll_message_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="2dp"
android:layout_marginTop="5dp"
android:minWidth="65dp"
android:orientation="vertical"
android:paddingBottom="2dp"
android:paddingLeft="2dp"
android:paddingRight="9dp"
prop:stable_background="@drawable/cloud_messages_contact_document_1080"
prop:checked_background="@drawable/cloud_messages_contact_document_1080_selected_06"
prop:hovered_background="@drawable/cloud_messages_contact_document_1080_selected_06"
prop:resend_background="@drawable/cloud_messages_user_document_1080_red"
android:paddingTop="0dp">
但是当我试图通过以下方式在适配器中扩展我的视图时:
inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.chat_messages_item_me, parent, false);
我收到错误,我认为这是因为我在自定义视图中出错:
android.view.InflateException: Binary XML file line #23: Error inflating class custom.MessageCloudView
at android.view.LayoutInflater.createView(LayoutInflater.java:613)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
at list_adapters.ChatMessagesAdapter.inflateNewView(ChatMessagesAdapter.java:142)
at list_adapters.ChatMessagesAdapter.getView(ChatMessagesAdapter.java:70)
at android.widget.AbsListView.obtainView(AbsListView.java:2465)
at android.widget.ListView.makeAndAddView(ListView.java:1775)
at android.widget.ListView.fillUp(ListView.java:712)
at android.widget.ListView.layoutChildren(ListView.java:1609)
at android.widget.AbsListView.onLayout(AbsListView.java:2300)
at ChatListView.onLayout(ChatListView.java:355)
at android.view.View.layout(View.java:14072)
at android.view.ViewGroup.layout(ViewGroup.java:4607)
at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:581)
at android.view.View.layout(View.java:14072)
at android.view.ViewGroup.layout(ViewGroup.java:4607)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14072)
at android.view.ViewGroup.layout(ViewGroup.java:4607)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
at android.view.View.layout(View.java:14072)
at android.view.ViewGroup.layout(ViewGroup.java:4607)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
at android.view.View.layout(View.java:14072)
at android.view.ViewGroup.layout(ViewGroup.java:4607)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14072)
at android.view.ViewGroup.layout(ViewGroup.java:4607)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
at android.view.View.layout(View.java:14072)
at android.view.ViewGroup.layout(ViewGroup.java:4607)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:14072)
at android.view.ViewGroup.layout(ViewGroup.java:4607)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1997)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1818)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4526)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
at android.view.LayoutInflater.createView(LayoutInflater.java:587)