我无法强调我在这里搜索了多少关于我的问题的可能解决方案。 我已经尝试将视图及其子视图设置为不可聚焦,将listView choiceMode设置为多个。 我也尝试使用实现Checkable的自定义LinearLayout,但无济于事。
这是我的代码(主要活动):
package com.virosys.amd.activities;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.virosys.amd.Amd;
import com.virosys.amd.DatabaseHandler;
import com.virosys.amd.JsonFunctions;
import com.virosys.amd.PessoaHelper;
import com.virosys.amd.PosterHelper;
import com.virosys.amd.R;
import com.virosys.amd.ServiceHandler;
import com.virosys.amd.adapters.ListaGeneroListAdapter;
import com.virosys.amd.adapters.ListaPessoaListAdapter;
import com.virosys.amd.adapters.ListasListAdapter;
import com.virosys.amd.flowLayout.FlowLayout;
import com.virosys.amd.types.Filme;
import com.virosys.amd.types.FilmePessoa;
import com.virosys.amd.types.Genero;
import com.virosys.amd.types.Lista;
import com.virosys.amd.types.Pessoa;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.ExecutionException;
public class FilmeDetalhesActivity extends Activity {
private int id = 0;
private Filme f = null;
private ArrayList<Pessoa> p;
private DatabaseHandler dh;
private Button btCadFilme;
private ServiceHandler sh;
//Here lies a lot of methods unneeded for this question.
//unneededMethods(){}
public void addToLista(View v) { // **********Here is where the fun begins
LayoutInflater layoutInflater = LayoutInflater.from(FilmeDetalhesActivity.this);
View listSelector = layoutInflater.inflate(R.layout.listas_select, null);
AlertDialog.Builder builder = new AlertDialog.Builder(FilmeDetalhesActivity.this);
builder.setTitle("Gerenciar Listas");
final ListView lvListas = (ListView) listSelector.findViewById(R.id.lvListas);
builder.setView(listSelector);
ArrayList<Lista> listas = new ArrayList<>();
if (dh == null) {
dh = new DatabaseHandler(FilmeDetalhesActivity.this);
}
ListasListAdapter lla = new ListasListAdapter(FilmeDetalhesActivity.this, dh.getListas());
try {
lla.isEmpty();
} catch (Exception e) {
lla = null;
}
if (lla != null) {
lvListas.setAdapter(lla);
}
lvListas.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // Tried this, didn't work.
lvListas.setItemsCanFocus(false);// Tried this, didn't work.
lvListas.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
lvListas.setItemChecked(position, !lvListas.isItemChecked(position)); // This ultimately drove me mad.
Toast.makeText(FilmeDetalhesActivity.this, String.valueOf(lvListas.isItemChecked(position)),
Toast.LENGTH_SHORT).show(); // Seems this status is always false.
}
});
builder.setPositiveButton("Atualizar Listas!", null);
builder.setNegativeButton("Cancelar", null);
final AlertDialog alert = builder.create();
alert.show();
// alert.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// alert.cancel();
// }
// });
// alert.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// String lista = lvListas.getText().toString();
// if (!lista.trim().isEmpty()) {
// dh.createLista(lista);
// AlertDialog.Builder ok = new AlertDialog.Builder(FilmeDetalhesActivity.this);
// ok.setMessage("Lista " + lista + " criada com sucesso!");
// ok.setPositiveButton("Continuar", null);
// ok.show();
//// listar();
// alert.dismiss();
// } else {
// Toast.makeText(FilmeDetalhesActivity.this, "Digite um nome para a lista!", Toast.LENGTH_LONG)
// .show();
// }
// }
// });
//
// dh.addToLista(f, listas);
}
}
列表适配器:
package com.virosys.amd.adapters;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.virosys.amd.R;
import com.virosys.amd.types.Filme;
import com.virosys.amd.types.Lista;
import java.util.ArrayList;
public class ListasListAdapter extends ArrayAdapter<Lista> {
private Typeface font;
public ListasListAdapter(Context context, ArrayList<Lista> listas, Typeface font) {
super(context, 0, listas);
this.font = font;
}
public ListasListAdapter(Context context, ArrayList<Lista> listas) {
super(context, 0, listas);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Lista lista = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listaritemmodel, parent, false);
convertView.setBackgroundResource(R.drawable.list_selected);
convertView.setFocusable(false);
convertView.setFocusableInTouchMode(false);
}
TextView tvNome = (TextView) convertView.findViewById(R.id.listaLabelTitulo);
TextView tvId = (TextView) convertView.findViewById(R.id.listaLabelId);
tvNome.setText(lista.getNome());
tvNome.setTypeface(font);
tvNome.setSelected(true);
tvId.setText(String.valueOf(lista.getId()));
return convertView;
}
}
自定义LinearLayout(实现可检查):
package com.virosys.amd;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.CheckedTextView;
import android.widget.LinearLayout;
/*
* This class is useful for using inside of ListView that needs to have checkable items.
*/
public class CheckableLinearLayout extends LinearLayout implements Checkable {
private CheckedTextView _checkbox;
public CheckableLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// find checked text view
int childCount = getChildCount();
for (int i = 0; i < childCount; ++i) {
View v = getChildAt(i);
if (v instanceof CheckedTextView) {
_checkbox = (CheckedTextView)v;
}
}
}
@Override
public boolean isChecked() {
return _checkbox != null && _checkbox.isChecked();
}
@Override
public void setChecked(boolean checked) {
if (_checkbox != null) {
_checkbox.setChecked(checked);
}
if (checked) {
this.setBackgroundColor(getResources().getColor(R.color.DarkPurple));
} else {
setBackgroundColor(Color.TRANSPARENT);
}
}
@Override
public void toggle() {
if (_checkbox != null) {
_checkbox.toggle();
}
}
}
backgroundResource的布局文件(也没用):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/Transparent" />
<item android:state_pressed="true"
android:drawable="@color/DarkPurple" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/DarkPurple" />
</selector>
我该怎么办?
答案 0 :(得分:1)
您不需要创建实现Checkable或选择器xml的LinearLayout。你应该做的是,将listview传递给适配器,并在适配器的getView中,添加如下代码:
if(listView.isItemChecked(position)) {
convertView.setBackgroundResource(R.color.DarkPurple);
} else {
convertView.setBackgroundResource(R.color.Transparent);
}
在你的活动中,只需这样做:
lvListas.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
lla.notifyDataSetChanged();
}
});