1

I have a list of templates each template in framelayout, each template has main image and 6 retangles representing colors (see the image)

When I click on rectangle in the list it gets the focus and border color changes . and it stays selected even if i click on other template I want only one to be selected at a time, here is the code for the template class.

package com.v12software.android.website.selecttemplate;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.v12software.android.R;
import com.v12software.android.Activities.V12BaseActivity;
import com.v12software.android.Managers.ServiceManager;
import com.v12software.android.Managers.WebTemplate;
import com.v12software.android.custom.NavWidget;
import com.v12software.android.custom.NavWidget.OnClickListener;
import com.v12software.android.custom.TemplateView;
import com.v12software.android.reqs.IonAPIReqs;
import com.v12software.android.utils.SharedPrefUtils;

public class SelecttemplateActivity extends V12BaseActivity {

    private NavWidget nav_bar_home;
    public static LinearLayout listemplates;
    public static String selectedSkinId = "0";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_templates);

        ImageLoader.getInstance().init(
                new ImageLoaderConfiguration.Builder(this)
                        .defaultDisplayImageOptions(
                                new DisplayImageOptions.Builder()
                                        .cacheInMemory(true).cacheOnDisk(true)
                                        .build()).build());

        nav_bar_home = (NavWidget) findViewById(R.id.nav_bar);
        listemplates = (LinearLayout) findViewById(R.id.templateContainer);
        nav_bar_home.addOnClickListener(new OnClickListener() {

            @Override
            public void onNavBtnClicked(View view) {
                // TODO Auto-generated method stub
                switch (view.getId()) {
                case R.id.left_tv:
                    SelecttemplateActivity.this.onBackPressed();
                    finish();
                    break;
                case R.id.center_tv:

                    break;
                case R.id.right_tv:
                    // save template to server
                    save();
                    break;
                }
            }
        });

        try {
            new Loadtemplates().execute().get();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        nav_bar_home.setButtonTitles(R.string.nav_back,
                R.string.website_select_template,
                R.string.vehicledetails_dlg_unsavedchanges_save);
    }

    private class Loadtemplates extends AsyncTask<String, String, ArrayList<WebTemplate>> {

        ArrayList<WebTemplate> templates = new ArrayList<WebTemplate>();

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        protected ArrayList<WebTemplate> doInBackground(String... args) {
            try {
                templates = ServiceManager.Current
                        .get_all_templates(SharedPrefUtils
                                .getAuthToken(getApplicationContext()));

            } catch (Exception e) {
                e.printStackTrace();
            }
            return templates;
        }

        protected void onPostExecute(ArrayList<WebTemplate> webTemplates) {
            for (WebTemplate wt : webTemplates) {
                TemplateView tv = new TemplateView(getApplicationContext(), wt);
                tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
                listemplates.addView(tv);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

//  @Override
//  public void onNavBtnClicked(View view) {
//      // TODO Auto-generated method stub
//      switch (view.getId()) {
//      case R.id.left_tv:
//          SelecttemplateActivity.this.onBackPressed();
//          finish();
//          break;
//      case R.id.center_tv:
//
//          break;
//      case R.id.right_tv:
//          // save template to server
//          save();
//          break;
//      }
//  }

    private void save() {
        // TODO Auto-generated method stub
        IonAPIReqs.savetemplate(getApplicationContext(), SharedPrefUtils.getAuthToken(getApplicationContext()),selectedSkinId , new FutureCallback<JsonObject>() {

            @Override
            public void onCompleted(Exception arg0, JsonObject arg1) {
                // TODO Auto-generated method stub
                if(IonAPIReqs.isReqSuccess(arg1))
                    Toast.makeText(getApplicationContext(), "Saving Template", Toast.LENGTH_LONG).show();
            }
        });
    }

}

and here is the code for the main class instantiating the first one

package com.v12software.android.custom;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.v12software.android.R;
import com.v12software.android.Managers.WebTemplate;
import com.v12software.android.website.selecttemplate.SelecttemplateActivity;

public class TemplateView extends LinearLayout {

    private Context cntx;

    private WebTemplate webTemplate;
    private ArrayList<BorderView> colorsIVs;
    /** The inflater. */
    private LayoutInflater inflater;

    private LinearLayout colorsLayout;

    private ImageView templateIV;

    public WebTemplate getWebTemplate() {
        return webTemplate;
    }

    public void setWebTemplate(WebTemplate webTemplate) {
        this.webTemplate = webTemplate;
    }

    public TemplateView(Context context, WebTemplate webtemplate) {
        super(context);
        this.webTemplate = webtemplate;
        this.colorsIVs = new ArrayList<BorderView>();
        setOrientation(LinearLayout.VERTICAL);
        setGravity(Gravity.CENTER_HORIZONTAL);
        setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
        this.cntx = getContext();
        this.inflater = LayoutInflater.from(cntx);
        this.inflater.inflate(R.layout.template_litem, this);
        this.colorsLayout = ((LinearLayout) findViewById(R.id.liearlayoutimages));
        this.templateIV = ((ImageView) findViewById(R.id.mainimage));

        for (int i = 0; i < webTemplate.getColors().size(); i++) {
            String selectedColor = webTemplate.getColors().get(i);
            final String selectedSkin = webTemplate.getIds().get(i);
            ImageView colorIV = new ImageView(cntx);
            colorIV.setBackgroundColor(Color.parseColor(selectedColor));

            BorderView borderV = new BorderView(cntx, colorIV);
            LayoutParams lparams = new LayoutParams(100, 100);
            lparams.setMargins(5, 5, 5, 5);
            borderV.setLayoutParams(lparams);
            borderV.setTag(selectedColor);
            colorsIVs.add(borderV);

            ImageLoader.getInstance().displayImage(
                    webTemplate.getfisrturltoshow(), templateIV);
            borderV.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ImageLoader.getInstance().displayImage(selectedSkin,
                            templateIV);
                    SelecttemplateActivity.selectedSkinId = selectedSkin
                            .replace(
                                    "http://cdn-w.v12soft.com/photos/templates/images/template-",
                                    "").replace(".jpg", "");

                    for (BorderView btn : colorsIVs) {
                        if (btn != (BorderView) v) {
                        //  colorsLayout.invalidate();
                        //  colorsLayout.refreshDrawableState();

//                          SelecttemplateActivity.listemplates.invalidate();
//                          SelecttemplateActivity.listemplates.refreshDrawableState();
                            btn.removeSelectionBorder();
//                          for(int i=0;i<SelecttemplateActivity.listemplates.getChildCount();i++){
//                              TemplateView tt = (TemplateView)    SelecttemplateActivity.listemplates.getChildAt(i);
//                              for(int k=0;k<tt.getChildCount();k++){
//                                  
//                                  BorderView ti   = (BorderView) tt.getChildAt(k);
//                                  ti.removeSelectionBorder();
//                              }
//                              
                             //Log.v("linear children count ", ""+ln.rem);

                    //  }
                        }else{
                            ((BorderView) v).addSelectionBorder();
                            Log.v("linear children count ", ""+SelecttemplateActivity.listemplates.getChildCount());
                        }
                    }
                    //Toast.makeText(getContext(), "gt children count"+colorsLayout.getChildCount(), Toast.LENGTH_LONG).show();
                    //Toast.makeText(getContext(), "gt children count"+SelecttemplateActivity.listemplates.getChildCount(), Toast.LENGTH_LONG).show();
//                  for(int i=0;i<SelecttemplateActivity.listemplates.getChildCount();i++){
//                      TemplateView ln=(TemplateView)  SelecttemplateActivity.listemplates.getChildAt(i);
//                       Log.v("linear children count ", ""+ln.getChildCount());
//                  }

                    //colorsLayout.getParent().recomputeViewAttributes(v);

                }
            });
            colorsLayout.addView(borderV);
        }
    }

    @Override
    protected void onFinishInflate() {
    }

    static class BorderView extends FrameLayout {
        public ImageView imageView;

        public BorderView(Context context, ImageView iv) {
            super(context);
            this.imageView = iv;
            setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT));
            addView(imageView);
        }

        public ImageView getImageView() {
            return imageView;
        }

        public void setImageView(ImageView imageView) {
            this.imageView = imageView;
        }

        public void addSelectionBorder() {
            int border = 8;
            setPadding(border, border, border, border);
            setBackgroundColor(Color.parseColor("#26FF00"));
        }

        public void removeSelectionBorder() {
            int border = 0;
            setPadding(border, border, border, border);
            setBackgroundColor(Color.TRANSPARENT);
        }
    }
}

1 Answer 1

1

I think you should deselect all other border views from other templates that you have in your code. To do so :

1- add this method in the TemplateView class :

private void deselectOtherBorderViews(TemplateView currentSelectedTemplate, LinearLayout layoutTemplateContainer) {
    if (layoutTemplateContainer != null) {
        int childCount = layoutTemplateContainer.getChildCount();
        if (childCount > 0) {
            int currentSelectedTemplateTag = (Integer) currentSelectedTemplate.getTag();
            for (int i = 0; i < childCount; i++) {
                View currentChild = layoutTemplateContainer.getChildAt(i);
                if (currentChild != null) {
                    // cast the current view to a web template
                    TemplateView currentTemplate = (TemplateView) currentChild;
                    int currentTemplateTag = (Integer) currentTemplate.getTag();

                    if (currentTemplateTag != currentSelectedTemplateTag) {
                        // deselect the border view of the currentTemplate
                        ArrayList<BorderView> currentTemplateColorsIVs = currentTemplate.getColorsIVs();
                        if (currentTemplateColorsIVs != null && !currentTemplateColorsIVs.isEmpty()) {
                            for (BorderView btn : currentTemplateColorsIVs) {
                                btn.removeSelectionBorder();
                            }
                        }
                    }
                }
            }
        }
    }
}

2- Call it in the onClick() method ( when a user click on a color in a specific template view :

borderV.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // Deselect other border views from other web template views
                deselectOtherBorderViews(TemplateView.this, SelecttemplateActivity.listemplates);
// The rest of your code...

NB : Don't forget to set integer tags to your template views while loading them in the onPostExecute() :

protected void onPostExecute(ArrayList<WebTemplate> webTemplates) {
        int counter = 0;
        for (WebTemplate wt : webTemplates) {
            TemplateView tv = new TemplateView(getApplicationContext(), wt);
            tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            tv.setTag(counter);
            counter++;
            listemplates.addView(tv);
        }

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.