Android setVisibility函数无法正常工作

时间:2018-06-01 11:46:14

标签: java android xml

我一直试图在以下代码中使用setVisibility()函数几个小时:

这是适配器类:

$files = glob('path/to/temp/{,.}*', GLOB_BRACE);

自定义列表的布局:

public class ExamScheduleAdapter extends ArrayAdapter<String>{
private ArrayList<String> id;
private ArrayList<String> name;
private ArrayList<String> date, subject, total, passing;
private Activity context;

Button idview;
TextView nameview, dateview, subjectview, totalview, passview;

public ExamScheduleAdapter(Activity context, ArrayList<String>id, ArrayList<String>name, ArrayList<String>date) {
    super(context, R.layout.fragment_exam_shedule, id);
    this.id = id;
    this.name = name;
    this.date = date;
    this.context = context;
}

public ExamScheduleAdapter(Activity context, ArrayList<String>id, ArrayList<String>name, ArrayList<String>date,
                           ArrayList<String>subject, ArrayList<String>total, ArrayList<String>passing) {
    super(context, R.layout.fragment_exam_shedule, id);
    this.id = id;
    this.name = name;
    this.date = date;
    this.subject = subject;
    this.total = total;
    this.passing = passing;
    this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){

    LayoutInflater inflater = context.getLayoutInflater();
    View listviewItem = inflater.inflate(R.layout.list_es,null,true);
    idview=(Button) listviewItem.findViewById(R.id.es_list_id);
    nameview=(TextView)listviewItem.findViewById(R.id.es_list_exam_name);
    dateview=(TextView)listviewItem.findViewById(R.id.es_list_date);

    subjectview=listviewItem.findViewById(R.id.es_list_exam_subject);
    totalview=listviewItem.findViewById(R.id.es_list_exam_t_marks);
    passview=listviewItem.findViewById(R.id.es_list_exam_p_marks);

    idview.setText(id.get(position));
    nameview.setText(name.get(position));
    dateview.setText(date.get(position));

    idview.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(subjectview.getVisibility()==View.GONE){
                subjectview.setVisibility(View.VISIBLE);
                totalview.setVisibility(View.VISIBLE);
                passview.setVisibility(View.VISIBLE);

                subjectview.invalidate();
                totalview.invalidate();
                passview.invalidate();
                Toast.makeText(getContext(), "Now visible", Toast.LENGTH_LONG).show();
            }
            else{
                subjectview.setVisibility(View.GONE);
                totalview.setVisibility(View.GONE);
                passview.setVisibility(View.GONE);

                subjectview.invalidate();
                totalview.invalidate();
                passview.invalidate();
                Toast.makeText(getContext(), "Now invisible", Toast.LENGTH_LONG).show();
            }
        }
    });

    return listviewItem;
}
}

onclick功能正在被触发,它也能够在吐司消息正确显示时检查可见性(&#34;现在可见&#34;起初,然后是其他点击消息&#34;) 可能是什么问题呢?我尝试过各种选择。我做了无效,没有无效,使用了view.gone,view.invisible等,但似乎没有任何效果。

Plz帮忙!提前谢谢。

2 个答案:

答案 0 :(得分:0)

您的subjectviewtotalviewpassview变量的范围在适配器中,而不是在每个适配器项中。因此,使用getView()实例化的最新一个适配器项将覆盖具有其自己值的那些。

将变量更改为getView()中的本地变量,以便每个OnClickListener获得自己的副本。

答案 1 :(得分:0)

调用适配器中的

$grid.tooltip({ items: "td[title]", content: function() { var element = $(this), title = element.attr("title"); if (title) { return '<span class="fa fa-lg fa-fw fa-edit"></span>' + title; } } }); 方法来创建屏幕上显示的每个列表项视图。这样,您的代码每次都会覆盖全局变量

getView()

你永远不知道它最终会包含哪些列表项视图。

不要声明全局变量,而是使用Button idview; TextView nameview, dateview, subjectview, totalview, passview; 本地变量:

final

(别忘了删除)

final Button idview=(Button) listviewItem.findViewById(R.id.es_list_id);
final TextView nameview=(TextView)listviewItem.findViewById(R.id.es_list_exam_name);
final TextView dateview=(TextView)listviewItem.findViewById(R.id.es_list_date);

final TextView subjectview=listviewItem.findViewById(R.id.es_list_exam_subject);
final TextView totalview=listviewItem.findViewById(R.id.es_list_exam_t_marks);
final TextView passview=listviewItem.findViewById(R.id.es_list_exam_p_marks);