mercredi 6 mai 2015

Inflating view in view hierarchy in runtime (on user demand)

I have tried to look throughout similar questions, but haven't found any solutions or best practices.

My question is following.

I have custom view, for instance

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.view.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/bottom_indicator"
        android:padding="10dip"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>

This is layout for my custom view (not final there will be some difference , just to make it more readable).

Here you can see CirclePageIndicator. It is hardcoded that it will be this type of indicator in view.
What do I need ?
I am looking for the best way to make able user to select desirable indicator specifying value of my custom view attribute, and than in runtime check user choice and inflate such type of indicator

I have some ideas how to do this, but don't know what is better both for performance and clean code.

  1. Just leave only ViewPager (and whatever I want to have in my custom view) and than in runtime just problematically create indicator object depending on choice and ad it to the view hierarchy
  2. Use ViewStub and than I also have to specify id of desired view to be inflated.
  3. Just have all types of indicators but with "View.GONE" attribute, but I think it is bad idea.

Please help to solve this problem in the most correct and elegant way.

I will be grateful for any advice, explanations and examples.

Thanks everyone in advance.

Aucun commentaire:

Enregistrer un commentaire