如何从View.gone恢复视图。setVisibility(View.VISIBLE) 在 xml 中使用 'android:visibility= "gone"' 后不起作用



我的问题:在我的xml文件中,我定义android:visibility="gone"在标记为assess_layout_list的线性布局中。然后,在course_adapter_layout(整个视图)的onClick()中,我将可见性设置回view。可见,这不起作用,即使在它工作之前的Log调用,称为assess_list_layout的LinearLayout对象不是空的,当我定义visibility="invisible"在XML文件中。我希望它一开始就消失了,点击后可见,因为这符合应用程序的设计。

这是我的course_adapter_view.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/course_adapter_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="left"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginTop="20dp"
android:padding="15dp"
android:elevation="2dp"
android:background="@drawable/course_header_background">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/course_color_circle"/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"/>
<TextView
android:id="@+id/course_adapter_course_code"
android:text="TextView1"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_weight="0.5"/>
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.25"/>

<TextView
android:id="@+id/course_adapter_course_title"
android:text="TextView2"
android:layout_width="wrap_content"
android:layout_height="20dp"/>
</LinearLayout>
<LinearLayout
android:id="@+id/assess_list_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:background="@drawable/course_body_background"
android:padding="20dp"
android:visibility="gone"
>

<ListView
android:id="@+id/course_adapter_assess_list"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_marginBottom="10dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="More" />
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="New"/>
</LinearLayout>

</LinearLayout>

</LinearLayout>

这是我的CourseListAdapter.java文件,我用它来创建课程列表中每个课程的每个视图,减去通常的东西:

package com.example.schoolplanner2.adapters;

public class CourseListAdapter extends ArrayAdapter<Course> {
private static final String TAG = "CourseListAdapter";
private Context context;
int mResource;
public CourseListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Course> objects) {
super(context, resource, objects);
this.context = context;
mResource = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// get info
String course_code = getItem(position).getCourseCode();
Double course_grade = getItem(position).getCurrentGrade();
// make inflater and inflate the layout
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(mResource, parent, false);

TextView tv_course_code = v.findViewById(R.id.course_adapter_course_code);
TextView tv_course_title = v.findViewById(R.id.course_adapter_course_title);
tv_course_code.setText(course_code);
tv_course_title.setText(String.valueOf(course_grade));
// add on click to each list view element
LinearLayout layout = v.findViewById(R.id.course_adapter_layout);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "List view element has been clicked " + course_code);

// expand the view to include a new fragment
LinearLayout assess_list_layout = view.findViewById(R.id.assess_list_layout);
assess_list_layout.setVisibility(View.VISIBLE);
// get the list view and add each course to the course view
ListView assessment_list_view = (ListView) view.findViewById(R.id.course_adapter_assess_list);
AssessmentListAdapter assessAdapter = new AssessmentListAdapter(getContext(), R.layout.assessment_adapter_view, getItem(position).getAssessmentList(), getItem(position));
assessment_list_view.setAdapter(assessAdapter);
}
});
return v;
}
}

如果你还需要什么信息,请告诉我。也会接受其他方法的建议来完成同样的事情。谢谢你的帮助。~赛斯。编辑:当assess_list_layout.setVisibility(View.VISIBLE)在onClick之外时,它确实工作。

进一步编辑:事情我已经尝试到目前为止没有效果:

  • 移动我定义LinearLayout组件的位置
  • 调用父视图invalidate()
  • 使用runOnUiThread ()
  • 在我试图为assess_list_layout查找viewbyid的行中更改视图为v,它们是相同的事情,所以它没有帮助。
  • 在assess_list_layout上调用requestLayout()

更新:我现在已经设法让assess_list_layout节出现时,单击course_adapter_layout。现在唯一的问题是视图不再占用屏幕上的空间,它变成了一个可滚动的视图可以上下滚动以看到整个视图。此外,当我滚动到快速时,它会将视图重置回启动时的方式。

1。视图可见性不工作

可视性不工作,因为视图最初没有呈现。删除xml中的可见性,并在适配器类中完全处理可见性。在'assess_list_layout'中,linerlayout的高度可以是硬编码的,因为在这个布局中,listview的高度已经是硬编码的。你可以硬编码到300然后检查。这样可以帮助视图获得初始渲染。

2。

滚动时,已经可见的'assess_list_layout'视图可能不可见。这是因为我们需要处理可见性,这个处理类似于listview中的复选框选择处理。希望课程类是模型类,在其中添加另一个名为的属性isSelected作为布尔值,并设置默认值为false. 请参考以下课程类别

<<p>课程类/strong>
public class Course {
private boolean isSelected = false;
public boolean isSelected() {
return isSelected;
}
public void setSelected(boolean selected) {
isSelected = selected;
}
}

请参考以下适配器类中的代码更改。

public class CourseListAdapter extends ArrayAdapter<Course> {
private static final String TAG = "CourseListAdapter";

private Context context;
int mResource;

public CourseListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<Course> objects) {
super(context, resource, objects);
this.context = context;
mResource = resource;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
// get info
String course_code = getItem(position).getCourseCode();
Double course_grade = getItem(position).getCurrentGrade();

// make inflater and inflate the layout
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(mResource, parent, false);


TextView tv_course_code = v.findViewById(R.id.course_adapter_course_code);
TextView tv_course_title = v.findViewById(R.id.course_adapter_course_title);

//My Change
// expand the view to include a new fragment
LinearLayout assess_list_layout = view.findViewById(R.id.assess_list_layout);

// get the list view and add each course to the course view
ListView assessment_list_view = (ListView) view.findViewById(R.id.course_adapter_assess_list);

assess_list_layout.setVisibility(View.GONE);

if (getItem().get(position).isSelected()) {
assess_list_layout.setVisibility(View.Visible);
AssessmentListAdapter assessAdapter = new AssessmentListAdapter(getContext(), R.layout.assessment_adapter_view, getItem(position).getAssessmentList(), getItem(position));
assessment_list_view.setAdapter(assessAdapter);
}
//My Change

tv_course_code.setText(course_code);
tv_course_title.setText(String.valueOf(course_grade));

// add on click to each list view element
LinearLayout layout = v.findViewById(R.id.course_adapter_layout);
layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i(TAG, "List view element has been clicked " + course_code);

//My Change
getItem().get(position).setSelected(true);          
//My Change
assess_list_layout.setVisibility(View.VISIBLE);

AssessmentListAdapter assessAdapter = new AssessmentListAdapter(getContext(), R.layout.assessment_adapter_view, getItem(position).getAssessmentList(), getItem(position));
assessment_list_view.setAdapter(assessAdapter);
}
});

return v;
}
}

我已经注释为My Change查找代码中的差异

issue 1

// make inflater and inflate the layout
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(mResource, parent, false);

你不重用视图,这导致每次调用getView()时都会膨胀一个新视图;由于膨胀()方法是IO敏感的,它会减慢滚动的平滑度,触发触发器。

试试这个

// make inflater and inflate the layout
View v = null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
v= inflater.inflate(mResource, parent, false);
} else {
v = convertView;
}

问题2

当滚动列表视图时,你需要重置itemview状态,添加"expand"属性设置为课程bean,当单击项目集时,expand = true;然后在layout.setOnClickListener

上面添加流动代码
v.findViewById(R.id.assess_list_layout).setVisibility( item.expand ? View.VISIBLE:View.GONE);
ListView assessment_list_view = (ListView) v.findViewById(R.id.course_adapter_assess_list);
if (item.expand) {
AssessmentListAdapter assessAdapter = new  AssessmentListAdapter(getContext(), R.layout.assessment_adapter_view, 
item.getAssessmentList(), item);
assessment_list_view.setAdapter(assessAdapter);
}

问题3

set setOnClickListener ingetView()方法,每次调用getView()时都会创建一个新的Clicker实例。使用listView.setOnItemClickListener ()而不是

提示:

毕竟,您应该使用RecyclerView而不是ListView,后者是一个功能强大的UI小部件

最新更新