gpt4 book ai didi

android - 自定义线性布局android中的多项选择问题

转载 作者:行者123 更新时间:2023-11-30 01:55:50 25 4
gpt4 key购买 nike

我有一个模板列表,每个模板都在框架布局中,每个模板都有主图像和 6 个代表颜色的矩形(见图像)

当我单击列表中的矩形时,焦点和边框颜色发生变化。即使我点击其他模板,它也会保持选中状态我只想一次选择一个,这是模板类的代码。

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();
}
});
}

}

这是主类实例化第一个类的代码

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);
}
}
}

最佳答案

我认为您应该从代码中的其他模板中取消选择所有其他边框 View 。为此:

1- 在 TemplateView 类中添加此方法:

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- 在 onClick() 方法中调用它(当用户在特定模板 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...

注意:在 onPostExecute() 中加载它们时,不要忘记为模板 View 设置整数标签:

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);
}

希望对您有所帮助。

关于android - 自定义线性布局android中的多项选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32253671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com