Frage

I have such small layout:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/selectorCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/imageCountryFlag" />

    <ImageView
        android:id="@+id/selectorFlag"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/imageCountryFlag"
        android:src="@drawable/us" />
</LinearLayout>

I would like to connect this layout to specific class for handling changing of ImageView and checking of CheckBox depend on parameters in constructor.

 class MyPanel
 {
     public MyPanel(boolean isChecked, String flagPath)
     {
          changeFlag(flagPath);
          changeCheckOut(isChecked);
     }

     private void changeFlag(String flagPath)
     { /*change flag*/ }

     private void changeCheckOut(boolean isChecked)
     { /*change check out*/ }
 }

After this i would like to duplicate this MyPanel and i would like to have couple of checkBoxes with different flags and change they from class MyPanel. Something like this: enter image description here

Is it possible and how to do this?

War es hilfreich?

Lösung 2

I figured out it.
In my opinion, best way to do multiplayed component is to make it almost fully in Java code.
Solution presented below allows you to pass additional arguments in contructors of classes, but disadvantage is that you have to place everything with code which is not so convenient as using builder.
Now some sketch of how to do it:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="top"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left|bottom" >

        <!-- SOME STUFF WHICH IS NOT IMPORTANT -->
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/resultOutputLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <!-- HERE WILL BE SHOWNED COMPONENTS ADDED IN JAVA CODE (LOOK INTO CLASSES PRESENTED BELOW) -->
    </RelativeLayout>

</LinearLayout>

CLASSES:

This class contains panel with multiplayed components:

public class MainActivity extends Activity
{
    private PanelWithMultipliedComponents;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_icd_search);

        initGUI();
    }

    private void initGUI()
    {
        RelativeLayout resultOutputLayout = (RelativeLayout) findViewById(R.id.resultOutputLayout);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        // adding a panel which contains an multiplied elements
        final int LANG_SEL_PAN_ID = 666;  //may be usefull later
        langSelPanel = new PanelWithMultipliedComponents(this, null);
        langSelPanel.setId(LANG_SEL_PAN_ID);
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        langSelPanel.setLayoutParams(params);
        resultOutputLayout.addView(langSelPanel);
    }
}

This class is a panel with multiplayed components:

public class PanelWithMultipliedComponents extends LinearLayout 
{
    private Paint innerPaint;
    private Context mainActivity;     //this is MainActivity in fact

    public PanelWithMultipliedComponents (Context context)
    {
        super(context);
        initGUI(context);
    }

    public PanelWithMultipliedComponents (Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initGUI(context);
        VersionManager.lockLanguages(languageSelectors);
    }

    private void initGUI(Context context)
    {
        mainActivity= context;  // you can cast it later to use methods of MainActivity
        setOrientation(VERTICAL);
        multiplyComponents(Context context);
    }

    private void multiplyComponents(Context context)
    {
        MultipliedComponent[] comp= { new MultipliedComponent(context, null, this), new MultipliedComponent(context, null, this), new MultipliedComponent(context, null, this), new MultipliedComponent(context, null, this)};

        for (int i = 0; i < comp.length; i++)
        {
            addView(comp[i]);
            languageSelectors.add(comp[i]);
        }
    }
}

This class is a multiplayed component (which contains check box and some picture):

    public class MultipliedComponent extends LinearLayout
    {
        private PanelWithMultipliedComponents container;
        private ImageView somePicture;
        private CheckBox someCheck;

        public MultipliedComponent(Context context, AttributeSet att)
        {
            super(context, att);
        }

    public MultipliedComponent(Context context, AttributeSet att, PanelWithMultipliedComponents container)
    {
        this(context, att);
        this.container = container;
        initGUI(context);
    }

    public void initGUI(Context context)
    {
        setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        setGravity(Gravity.CENTER_VERTICAL);

        someCheck= new CheckBox(context);
        langSelection.setChecked(true);   // or false
        addView(someCheck);

        somePicture= new ImageView(context);
        try
        {
            Bitmap bitmap = BitmapFactory.decodeStream(/**get your assets manager somehow - eg. throug static reference to you application**/);
            somePicture.setScaleType(ScaleType.FIT_CENTER);
            somePicture.setImageBitmap(bitmap);
            somePicture.setPadding(0, 0, 10, 0);
        }
        catch (IOException e)
        {
            Log.e("IMAGE_OPEN_EXC", e.toString());
        }
        addView(somePicture);
    }
}

Andere Tipps

You can create a custom view and inflate this layout init for example:

public class MyPanel extends LinearLayout {

    private CheckBox cb;
    private ImageView iv;

    public MyPanel(Context context) {
        this(context, null);
    }


    public MyPanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        inflater.inflate(R.layout.your_layout, this, true);

        cb= (CheckBox) findViewById(R.id.selectorCheckBox);
            iv= (ImageView)findViewById(R.id.selectorFlag);

    }

  private void changeFlag(String flagPath)
 { /*change flag*/ }

 private void changeCheckOut(boolean isChecked)
 { /*change check out*/ }
}

you can use this in xml:

<your_pkg_name.MyPanel
    android:id="@+id/my_panel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</your_pkg_name.MyPanel>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top