Android custom UI trap: LayoutInflater. from (). inflate () must not work in the parent or virtual class.

Source: Internet
Author: User

Android custom UI trap: LayoutInflater. from (). inflate () must not work in the parent or virtual class.

Problem Background: Some UIS have commonalities, such as the indicator boxes that appear during the first running of common apps, which tell you where to adjust the volume, at which point the screen brightness is adjusted. When you click these views, the VIew disappears automatically. Or disappear automatically after a time. Another problem is that the directive View content loaded in different directions is different.

To this end, we need to abstract the views of these features and write a parent class or a base class. It is a lot of benefit to do this. Advantages:

1. Code can be simplified. The common methods in each sub-View are implemented by the parent class, and each sub-View implements its own logic.

2. Because these views only work once, writing them to the main UI xml seems out of date, and dynamic addition is the best. Because the orientation of the View is involved, the names of the instantiated variables of these views must be given in advance. If they are completely independent from each other, You need to define View1 view1, View2 view2. .. many views, and then notify them one by one when the direction changes. If there is a BaseView, View1 and View2... all are inherited from BaseView. You only need to define BaseView baseView. When you need to display the information, use BaseView to instantiate View1 or View2. for example, baseView = new View1 (...). then, when the direction changes, it determines whether the baseView is empty, and then it is OK to tell the direction.

Let's take a look at the BaseView mentioned above. Here we name it BaseGuideView:

package org.yanzi.ui;import org.yanzi.util.OrientationUtil;import android.content.Context;import android.view.MotionEvent;import android.view.View;import android.widget.RelativeLayout;import android.widget.TextView;public abstract  class BaseGuideView extends RelativeLayout implements Rotatable, View.OnClickListener {protected int mOrientation = 0;protected Context mContext;private GuideViewCallback mGuideViewCallback;public interface GuideViewCallback{public void onGuideViewClick();}public BaseGuideView(Context context, GuideViewCallback callback) {super(context);// TODO Auto-generated constructor stubmContext = context;mGuideViewCallback = callback;setOnClickListener(this);mOrientation = OrientationUtil.getOrientation();}@Overridepublic void setOrientation(int orientation, boolean animation) {// TODO Auto-generated method stubmOrientation = orientation;requestLayout();}protected abstract void initView();@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {// TODO Auto-generated method stubreturn true; //super.onInterceptTouchEvent(ev)}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubmGuideViewCallback.onGuideViewClick();}}

The most important thing is that I have consumed the Click Event in onInterceptTouchEvent, so that the child in the layout will not receive the clicks. Then a GuideViewCallback is written. when clicked, onGuideViewClick is triggered. The implementation of this interface is in another place, such as centralized Ui management. Delete the bullet box. In addition, requestLayout is executed every time the direction changes, and onMeasure and onLayout of the view are re-executed.

Then define a NanShiGuide. java inherited from the above class:

package org.yanzi.ui;import com.example.test1.R;import android.content.Context;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.widget.TextView;public class NanShiGuide extends BaseGuideView {int LAYOUT_ID = R.layout.c_nanshi_guide;View guideNanLayout;TextView guideNanText;public NanShiGuide(Context context, GuideViewCallback callback) {super(context, callback);// TODO Auto-generated constructor stubinitView();}@Overrideprotected void initView() {// TODO Auto-generated method stubLog.i("YanZi", "NanShiGuide initView enter...");View v = LayoutInflater.from(mContext).inflate(LAYOUT_ID, this, true);guideNanLayout = v.findViewById(R.id.guide_nan_layout);guideNanText = (TextView) v.findViewById(R.id.guide_nan_text);Log.i("YanZi", "NanShiGuide initView exit...");}}

In this subclass, resources can be loaded. Corresponding layout c_nanshi_guide.xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <FrameLayout android: id = "@ + id/guide_nan_layout" android: layout_width = "200dip" android: layout_height = "150dip" android: background = "@ drawable/nan1"> <TextView android: id = "@ + id/guide_nan_text" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: gravity = "center" android: layout_gravity = "bottom | center_horizontal" android: textColor = "@ android: color/white "android: textSize =" 20sp "android: text =" Nan Gong Huai Jin. "/> </FrameLayout> </RelativeLayout>
In the initView function, load xml and obtain the instances of various controls. The problem I encountered is that if this initView () is written in the base class (also a virtual class) baseGuideView constructor cannot run normally. Although the initView () function is executed, an error is returned:

07-06 15:17:58.258 I/YanZi   ( 8375): NanShiGuide initView enter...07-06 15:17:58.258 W/ResourceType( 8375): No package identifier when getting value for resource number 0x0000000007-06 15:17:58.258 D/AndroidRuntime( 8375): Shutting down VM07-06 15:17:58.258 W/dalvikvm( 8375): threadid=1: thread exiting with uncaught exception (group=0x410899a8)

The pointer of the package cannot be found. It is reasonable to say that this can be fully used in java syntax. The virtual class calls a virtual method, which is implemented by each subclass, but an error is reported here. The reason is:

View v = LayoutInflater. from (mContext). inflate (LAYOUT_ID, this, true );

Here is a question about this pointer. Who will this point to when initVIew () calls a virtual class? Is it a virtual class itself or a subclass? Therefore, the inflate itself has some special characteristics and cannot be used randomly. I tried not to write the initView in BaseGuideView as virtual, but as an empty function and still reported an error.In this case, the loading layout must be loaded and initialized by each sub-View.

The effect is as follows. The image in the upper left corner is specially displayed, and the background is dimmed:





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.