Efficient way to change between Ads views

I'm developing an online radio with ads and I have to change between two views with CoordinatorLayout.

When the ImageView is expanded represents the radio and bottom we can see the ads banner. When it's collapsed, the ads banner changes to top and bottom appears the radio banner.

Efficient way to change between Ads views

I would like to know which is the best option and more efficient for this. I have this code that works correctly but it does not convince me.

This is my xml.

<android.support.design.widget.CoordinatorLayout 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"      xmlns:tools="http://schemas.android.com/tools"      android:background="@android:color/background_light"      android:fitsSystemWindows="false">        <android.support.design.widget.AppBarLayout          android:id="@+id/app_bar"          android:layout_width="match_parent"          android:layout_height="300dp"          android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"          android:fitsSystemWindows="false"          >            <android.support.design.widget.CollapsingToolbarLayout              android:id="@+id/collapsing"              android:layout_width="match_parent"              android:layout_height="match_parent"              app:layout_scrollFlags="scroll|exitUntilCollapsed"              android:fitsSystemWindows="false"              app:contentScrim="?attr/colorPrimary"              app:expandedTitleMarginStart="48dp"              app:expandedTitleMarginEnd="64dp"              >                <ImageView                  android:id="@+id/main.backdrop"                  android:layout_width="match_parent"                  android:layout_height="match_parent"                  android:scaleType="centerCrop"                  android:src="@drawable/radio"                  android:padding="50dp"                  app:layout_collapseMode="parallax"                  />                <android.support.v7.widget.Toolbar                  android:id="@+id/main.toolbar"                  android:layout_width="match_parent"                  android:layout_height="?attr/actionBarSize"                  app:popupTheme="@style/ThemeOverlay.AppCompat.Light"                  android:background="@android:color/transparent"                  android:fitsSystemWindows="false"                  app:layout_collapseMode="pin"                  />              </android.support.design.widget.CollapsingToolbarLayout>      </android.support.design.widget.AppBarLayout>        <android.support.v4.widget.NestedScrollView          android:layout_width="match_parent"          android:layout_height="match_parent"          android:layout_marginBottom="?attr/actionBarSize"          app:layout_behavior="@string/appbar_scrolling_view_behavior"          >            <LinearLayout              android:layout_width="match_parent"              android:layout_height="match_parent"              android:orientation="vertical">                <LinearLayout                  android:id="@+id/linearAdsRadioTop"                  android:layout_width="match_parent"                  android:layout_height="wrap_content"                  android:orientation="horizontal"                  android:gravity="bottom"                  android:layout_gravity="bottom"                  tools:background="@android:color/holo_red_dark">                  <FrameLayout                      android:id="@+id/frameRadioTop"                      android:layout_width="match_parent"                      android:layout_height="match_parent" />                  <FrameLayout                      android:id="@+id/frameAdsTop"                      android:layout_width="match_parent"                      android:layout_height="match_parent" />              </LinearLayout>              <TextView                  android:layout_width="match_parent"                  android:layout_height="match_parent"                  android:textSize="20sp"                  android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed at ante. Mauris eleifend, quam a vulputate dictum, massa quam dapibus leo, eget vulputate orci purus ut lorem. In fringilla mi in ligula. Pellentesque aliquam quam vel dolor. Nunc adipiscing. Sed quam odio, tempus ac, aliquam molestie, varius ac, tellus. Vestibulum ut nulla aliquam risus rutrum interdum. Pellentesque lorem. Curabitur sit amet erat quis risus feugiat viverra. Pellentesque augue justo, sagittis et, lacinia at, venenatis non, arcu. Nunc nec libero. In cursus dictum risus. Etiam tristique nisl a nulla. Ut a orci. Curabitur dolor nunc, egestas at, accumsan at, malesuada nec, magna."                  android:padding="@dimen/activity_horizontal_margin"                  />          </LinearLayout>          </android.support.v4.widget.NestedScrollView>        <LinearLayout          android:layout_marginTop="5dp"          android:id="@+id/linearAdsRadioBottom"          android:layout_width="match_parent"          android:layout_height="?attr/actionBarSize"          android:orientation="horizontal"          android:gravity="bottom"          android:layout_gravity="bottom"          tools:background="@android:color/holo_red_dark">          <FrameLayout              android:id="@+id/frameRadioBottom"              android:layout_width="match_parent"              android:layout_height="match_parent" />          <FrameLayout              android:id="@+id/frameAdsBottom"              android:layout_width="match_parent"              android:layout_height="match_parent" />      </LinearLayout>      </android.support.design.widget.CoordinatorLayout>

And this is my Activity.

public class MainActivity extends AppCompatActivity {      private View bannerBottom, bannerTop;      private View radioBottom, radioTop;      private Toolbar toolbar;      private FrameLayout frameRadioBottom, frameBannerBottom, frameRadioTop, frameBannerTop;        @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          toolbar = (Toolbar) findViewById(R.id.toolbar);          setSupportActionBar(toolbar);            LayoutInflater inflater = LayoutInflater.from(this);            frameRadioBottom = (FrameLayout) findViewById(R.id.frameRadioBottom);          frameBannerBottom = (FrameLayout) findViewById(R.id.frameAdsBottom);          frameRadioTop = (FrameLayout) findViewById(R.id.frameRadioTop);          frameBannerTop = (FrameLayout) findViewById(R.id.frameAdsTop);            bannerBottom = inflater.inflate(R.layout.view_ads, frameBannerBottom, true);          radioBottom = inflater.inflate(R.layout.view_radio, frameRadioBottom, true);          bannerTop = inflater.inflate(R.layout.view_ads, frameBannerTop, true);          radioTop = inflater.inflate(R.layout.view_radio, frameRadioTop, true);            AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);            appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {              @Override              public void onStateChanged(AppBarLayout appBarLayout, State state) {                  if(!state.name().equals("COLLAPSED")) {                      bannerBottom.setVisibility(View.VISIBLE);                      radioBottom.setVisibility(View.GONE);                      bannerTop.setVisibility(View.GONE);                      radioTop.setVisibility(View.GONE);                  } else {                      bannerBottom.setVisibility(View.GONE);                      radioBottom.setVisibility(View.VISIBLE);                      radioTop.setVisibility(View.GONE);                      bannerTop.setVisibility(View.VISIBLE);                  }                }          });        }        private abstract static class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {            public enum State {              EXPANDED,              COLLAPSED,              IDLE          }            private State mCurrentState = State.IDLE;            @Override          public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {              if (i == 0) {                  if (mCurrentState != State.EXPANDED) {                      onStateChanged(appBarLayout, State.EXPANDED);                  }                  mCurrentState = State.EXPANDED;              } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {                  if (mCurrentState != State.COLLAPSED) {                      onStateChanged(appBarLayout, State.COLLAPSED);                  }                  mCurrentState = State.COLLAPSED;              } else {                  if (mCurrentState != State.IDLE) {                      onStateChanged(appBarLayout, State.IDLE);                  }                  mCurrentState = State.IDLE;              }          }            public abstract void onStateChanged(AppBarLayout appBarLayout, State state);      }  }

Thanks.

Replay

Category: android Time: 2016-07-31 Views: 2

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.230 (s). 12 q(s)