我正在提出一个问题,可能就像一个重复的问题,但我已经尝试了一切,对我来说没有任何作用。我创建了一个 listview ,每个项目都有两个视图,一个是 textview ,一个是复选框。它就像一个多选列表视图。每件物品都有自己的水平:简单,正常,坚硬。当从下拉列表中选择一个级别时:All,Easy,Normal,Hard ..列表会发生变化,就像过滤系统一样。
但是当我写listView.getChildAt(i).setVisibility(View.GONE);
时,行的内容将被删除,但占用的空间不会被释放。
任何帮助?
这是我的代码:
public class CreatePresentation extends Activity
{
DatabaseHelper db = new DatabaseHelper(this);
MyCustomAdapter dataAdapter = null;
List<Slider> list;
ListView listView;
String Text;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.create_presentation);
displayListView();
checkButtonClick();
}
private void displayListView()
{
list = new ArrayList<Slider>();
ArrayList<Slider> oldList = db.getAllSliders();
for (Slider anOldList : oldList) {
String s = anOldList.toString();
int in = anOldList.getId();
String lev = anOldList.getLevel();
Slider slider = new Slider(in, s, lev, false);
list.add(slider);
}
dataAdapter = new MyCustomAdapter(this, R.layout.list_check_box, list);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(dataAdapter);
}
private class MyCustomAdapter extends ArrayAdapter<Slider> {
private ArrayList<Slider> list;
public MyCustomAdapter(Context context, int textViewResourceId, List<Slider> list) {
super(context, textViewResourceId, list);
this.list = new ArrayList<Slider>();
this.list.addAll(list);
}
private class ViewHolder {
TextView text;
CheckBox checkbox;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
final Slider slider = list.get(position);
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list_check_box, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.sliderTitle);
holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox);
convertView.setTag(holder);
Spinner dropdown = (Spinner)findViewById(R.id.spinner);
String[] items = new String[]{"Tutto", "Facile", "Medio", "Difficile"};
ArrayAdapter<String> adapter = new ArrayAdapter<String (CreatePresentation.this, android.R.layout.simple_spinner_item, items);
dropdown.setAdapter(adapter);
Text = dropdown.getSelectedItem().toString();
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Slider slider = (Slider) cb.getTag();
slider.setSelected(cb.isChecked());
}
});
try {
if (list.get(position).getLevel().equals("Facile"))
convertView.setBackgroundColor(Color.parseColor("#477C3D"));
else if (list.get(position).getLevel().equals("Medio"))
convertView.setBackgroundColor(Color.parseColor("#936019"));
else if (list.get(position).getLevel().equals("Difficile"))
convertView.setBackgroundColor(Color.parseColor("#A02307"));
} catch (Throwable e) {
e.printStackTrace();
}
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, final int position, long id) {
Text = parentView.getItemAtPosition(position).toString();
try {
if (Text.equals("All")){
Runnable run = new Runnable(){
public void run(){
for (int i = 0; i < list.size(); i++) {
listView.getChildAt(i).setVisibility(View.VISIBLE);
}
}
};
runOnUiThread(run);
}
if (Text.equals("Easy")){
Runnable run = new Runnable(){
public void run(){
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getLevel().equals("Easy")) {
listView.getChildAt(i).setVisibility(View.VISIBLE);
}
else {
listView.getChildAt(i).setVisibility(View.GONE);
}
}
}
};
runOnUiThread(run);
}
if (Text.equals("Normal")){
Runnable run = new Runnable(){
public void run(){
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getLevel().equals("Normal"))
listView.getChildAt(i).setVisibility(View.VISIBLE);
else {
listView.getChildAt(i).setVisibility(View.GONE);
}
dataAdapter.notifyDataSetChanged();
}
}
};
runOnUiThread(run);
}
if (Text.equals("Hard")){
Runnable run = new Runnable(){
public void run(){
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getLevel().equals("Hard"))
listView.getChildAt(i).setVisibility(View.VISIBLE);
else
listView.getChildAt(i).setVisibility(View.GONE);
}
}
};
runOnUiThread(run);
}
} catch (Throwable e) {
e.printStackTrace();
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
holder.checkbox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Slider slider = (Slider) cb.getTag();
slider.setSelected(cb.isChecked());
}
});
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(slider.getQuestion());
holder.checkbox.setChecked(slider.isSelected());
holder.checkbox.setTag(slider);
return convertView;
}
}
答案 0 :(得分:1)
由于您要移除视图但将项目保留在列表中,我建议您使用其他方法。当用户选择一个会导致项目隐藏的值时,只需将该值设置为您的项目,然后调用
dataAdapter.notifyDataSetChanged();
这样做你必须修改你的getView()里面的逻辑,我的意思是如果你发现一个有资格获得隐藏的项而不是在你的customAdapter的getView()方法中返回convertView只返回一个空视图,比如此项目将不会显示,但仍会显示在您的列表中;)
答案 1 :(得分:0)
您可以使用项目的父布局来解决问题:
例如:
<LinearLayout ...>
<!-- Here Your item content starts -->
<LinearLayout android:id="@+id/content">
...
</LinearLayout>
<!-- Here Your content ends -->
</LinearLayout>
Java代码:
listView.getChildAt(i).getChildAt(0).setVisibility(View.GONE);
答案 2 :(得分:0)
just like a filter system
。为什么“喜欢”,为什么不让它实际上可过滤?有两种选择。更容易覆盖滑块的toString
。另一个是创建自定义过滤器,它不使用对象的toString
方法。但是,我不记得第二个怎么做,只有这样才有可能。
Slider:
String toString(){
return this.type;
}
When spinner selection changes:
adapter.getFilter().filter(selectedDifficulty);
这将自动显示您想要查看的项目。
答案 3 :(得分:0)
您不应更改适配器生成的视图的可见性 - 每次滚动listview时它们都会更改。相反,您应该更改基础适配器的行为。
答案 4 :(得分:0)
您可以尝试此布局而不是ListView
,因为只有4个过滤器:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include
android:id="@+id/filter_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/<your_listview_adapter>"
android:visibility="visible" />
<include
android:id="@+id/filter_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/<your_listview_adapter>"
android:visibility="visible" />
<include
android:id="@+id/filter_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/<your_listview_adapter>"
android:visibility="visible" />
<include
android:id="@+id/filter_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/<your_listview_adapter>"
android:visibility="visible" />
</LinearLayout>
当您想要隐藏其中一个过滤器时,您可以执行以下操作:
findViewById(R.id.filter_4).setVisibility(View.GONE);
修改强>
例如,如果您要向TextView
内的first include
添加信息,则必须像这样调用View
:
View filter1 = findViewById(R.id.filter_1);
TextView tv1 = (TextView) filter1.findViewById(R.id.<the_id_of_the_textview);
tv1.setText("StackOverflow filter");
答案 5 :(得分:0)
最好的方法是从列表中删除该项并调用dataAdapter.notifyDataSetChanged()
答案 6 :(得分:0)
防止在布局外添加其他布局。您只需隐藏项目的所有子视图,而不是项目本身。
例如:
if(i == 1){ // assume we need hide the first item.
//item itself
ViewGroup parent = ((ViewGroup) holder.convertView);
for (int j = 0; j < parent.getChildCount(); j++) {
parent.getChildAt(j).setVisibility(View.GONE);
}
}
我测试了这段代码,对我来说很好。