Android自定义视图绘制两次

时间:2015-10-29 17:34:00

标签: android

在我的活动A中,此视图调用两次,但在我的活动B中,没有问题。

活动A是非常简单的布局,只有一些linearLayout。我快要疯了,可能是什么问题?

这是我的AdBannerView

public class AdBannerView extends LinearLayout {

    public ImageView adIcon, adInstall;
    public TextView_ adTitle, adDesc;
    public ProgressBar adProgress;
    RelativeLayout adWrapperLay;
    private boolean impSent, adLoaded = false;

    public AdBannerView(Context context) {
        super(context);
    }

    public AdBannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AdBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View wrapper = inflater.inflate(R.layout.view_ad_banner, this, true);

        adIcon = (ImageView) wrapper.findViewById(R.id.adIcon);
        adInstall = (ImageView) wrapper.findViewById(R.id.adInstall);
        adTitle = (TextView_) wrapper.findViewById(R.id.adTitle);
        adDesc = (TextView_) wrapper.findViewById(R.id.adDesc);
        adProgress = (ProgressBar) wrapper.findViewById(R.id.adProgress);
        adWrapperLay = (RelativeLayout) wrapper.findViewById(R.id.adWrapperLay);

        Log.d("AdBannerView", "before loadAd()");

        if(NativeAdManager.getInstance().isAdEnabled)
            loadAd();
    }

    public void loadAd(){

        if(adLoaded)
            return;

        adLoaded = true;

        Log.d("AdBannerView", "loadAd() request");

        NativeAdManager.getInstance().getAd(getContext(), new NativeAdManager.AdListener() {
            @Override
            public void adLoaded(final NativeAdResponse.Ads[] ads) {

                /* load img */
                Picasso
                        .with(getContext())
                        .load(ads[0].adIc)
                        .into(adIcon);

                /* load title */
                adTitle.setText(""+ads[0].adTit);
                adDesc.setText(""+ads[0].adDesc);

                /* click listener */
                adInstall.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        App app = (App) getContext().getApplicationContext();
                        app.getUiController().loadUrlWithoutAdBlocker(ads[0].adClk);

                    }
                });

                /* show this layout */
                showAd(ads[0].adBeacons);

                Log.d("AdBannerView", "loaded with size = " + ads.length);

            }
        });
    }

    private void showAd(final NativeAdResponse.adBeacons[] adBeacons) {

        adProgress.setVisibility(GONE);
        adIcon.setVisibility(VISIBLE);

    }
}

我包括这样的布局:

<.... AdBannerView match_parent etc  />

证明绘图两次的日志:

10-29 20:28:19.219    6698-6698/pack D/AdBannerView﹕ before loadAd()
10-29 20:28:19.219    6698-6698/pack D/AdBannerView﹕ loadAd() request
10-29 20:28:19.295    6698-6698/pack D/AdBannerView﹕ before loadAd()
10-29 20:28:19.295    6698-6698/pack D/AdBannerView﹕ loadAd() request
10-29 20:28:19.636    6698-6698/pack D/AdBannerView﹕ loaded with size = 1
10-29 20:28:19.852    6698-6698/pack D/AdBannerView﹕ loaded with size = 1

有问题的活动A:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >


    <package.AdManager.AdBannerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="40dp"
        />


</RelativeLayout>

活动A.java(I deleted everything in layout and class except of AdBannerView but still same):

package package.Activity;

public class NewsRead extends Base {

    ToolBarView toolBarView;
    RelativeLayout backgroundLayForMainBgColor;

    ImageView imageView;
    TextView_ titleText, contentText, sourceText;
    LinearLayout wrapperLay /* for homeViewRowBg */, relatedNewsLay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId());

        /*
        this.toolBarView = (ToolBarView) findViewById(R.id.toolBarView);
        this.backgroundLayForMainBgColor = (RelativeLayout) findViewById(R.id.backgroundLayForMainBgColor);

        this.imageView = (ImageView) findViewById(R.id.imageView);
        this.titleText = (TextView_) findViewById(R.id.titleText);
        this.contentText = (TextView_) findViewById(R.id.contentText);
        this.sourceText = (TextView_) findViewById(R.id.sourceText);
        this.wrapperLay = (LinearLayout) findViewById(R.id.wrapperLay);

        changeTheme();

        toolBarView.hideDeleteButton().setToolBarClickListener(new ToolBarView.ToolBarClickListener() {
            @Override
            public void backButtonClick() {
                finish();
            }

            @Override
            public void deleteButtonClick() {

            }

        });

        Intent intent = getIntent();

        if(intent == null)
            return;

        loadNewsDetail(intent);

        */

    }

    private void loadNewsDetail(Intent intent) {
        String neTi = intent.getStringExtra("neTi");
        String neCo = intent.getStringExtra("neCo");
        String neSi = intent.getStringExtra("neSi");
        String neIm = intent.getStringExtra("neIm");
        String neUr = intent.getStringExtra("neUr");

        /**/

        Picasso
              .with(this)
              .load(neIm)
               //.placeholder(R.drawable.icon_placeholder)
              .into(imageView);

        titleText.setText(neTi);
        contentText.setText(neCo);
        sourceText.setText("Source: "+ Html.fromHtml("<u>"+neSi+"</u>"));

    }

    private void changeTheme() {
        ThemeModel curTheme = ThemeController.getInstance().getCurrentTheme();

        if(curTheme.hasBgImage()) {
            backgroundLayForMainBgColor.setBackground(curTheme.mainBgDrawable);
        } else {
            backgroundLayForMainBgColor.setBackgroundColor(Color.parseColor(ThemeController.getInstance().getCurrentTheme().mainBgColor));
        }

        wrapperLay.setBackgroundColor(Color.parseColor(curTheme.homeViewRowBg));
    }

    protected int getLayoutResourceId() {
        return R.layout.activity_news_read;
    }

    @Override
    protected void onSoftInputShown() {

    }

    @Override
    protected void onSoftInputHidden() {

    }

    @Override
    protected String getActivityName() {
        return "news_read";
    }

    @Override
    public void onBackPressed(){
        super.onBackPressed();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

}

基地:

public abstract class Base extends Activity {

    private boolean isKeyboardOpened;

    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(getLayoutResourceId());
        keyBoardListener();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                TrackingController.onActivityOpen(getActivityName());
            }
        }, 50);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    protected abstract int getLayoutResourceId();

    public void Toast(String str) {
        Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
    }

    public void Log(String str) {
        Log.d("act_name:"+getActivityName(), str);
    }

    private void keyBoardListener(){
        final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

            int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
            if (heightDiff > 100 ) { // 99% of the time the height diff will be due to a keyboard.

                if(isKeyboardOpened == false){
                    onSoftInputShown();
                }
                isKeyboardOpened = true;
            }else if(isKeyboardOpened == true){
                onSoftInputHidden();
                isKeyboardOpened = false;
            }
            }
        });
    }

    public String getString_(int resId){
        return getResources().getString(resId);
    }

    protected abstract void onSoftInputShown();
    protected abstract void onSoftInputHidden();

    protected abstract String getActivityName();

}

1 个答案:

答案 0 :(得分:2)

这是你的错误:)希望

你已经在你的基础上调用了setContentView,然后在你的派生类中你调用了super create(它调用了setContentView [创建了adElement])但是之后再次调用setContentView(getLayoutResourceId()); (这次来自你的派生类,它会覆盖布局,但即使它没有调用它实际上是我想象的相同内容,这也就是为什么它看起来很正常:)。

所以修复应该很简单 - 从活动A中删除setContentView(getLayoutResourceId()),因为它已经从基础活动调用了

相关问题