java如何从视图中恢复视图。跑了。setVisibility(View.VISIBLE)在xml中使用“安卓:visibility=“gone”后不工作
我的问题: 在我的xml文件中,我在标记为assessment\u layout\u list的线性布局中定义了安卓:visibility=“gone”。然后,在onClick()当然是整个视图的适配器布局中,我将可见性设置回视图。VISIBLE,这不起作用,即使在日志调用刚刚开始工作之前,名为assessment_list_layout的LinearLayout对象不为null,并且当我在xml文件中定义visibility=“invisible”时,它确实起作用。我希望它一开始就消失,点击后可见,因为这符合应用程序的设计
这是我的课程适配器视图。xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
安卓:id="@+id/course_adapter_layout"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical"
安卓:gravity="left"
>
<LinearLayout
安卓:orientation="horizontal"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginHorizontal="20dp"
安卓:layout_marginTop="20dp"
安卓:padding="15dp"
安卓:elevation="2dp"
安卓:background="@drawable/course_header_background">
<ImageView
安卓:layout_width="20dp"
安卓:layout_height="20dp"
安卓:src="@drawable/course_color_circle"/>
<Space
安卓:layout_width="0dp"
安卓:layout_height="match_parent"
安卓:layout_weight="0.25"/>
<TextView
安卓:id="@+id/course_adapter_course_code"
安卓:text="TextView1"
安卓:layout_width="wrap_content"
安卓:layout_height="20dp"
安卓:layout_weight="0.5"/>
<Space
安卓:layout_width="0dp"
安卓:layout_height="match_parent"
安卓:layout_weight="0.25"/>
<TextView
安卓:id="@+id/course_adapter_course_title"
安卓:text="TextView2"
安卓:layout_width="wrap_content"
安卓:layout_height="20dp"/>
</LinearLayout>
<LinearLayout
安卓:id="@+id/assess_list_layout"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="vertical"
安卓:layout_marginRight="40dp"
安卓:layout_marginLeft="40dp"
安卓:background="@drawable/course_body_background"
安卓:padding="20dp"
安卓:visibility="gone"
>
<ListView
安卓:id="@+id/course_adapter_assess_list"
安卓:layout_width="match_parent"
安卓:layout_height="250dp"
安卓:layout_marginBottom="10dp"/>
<LinearLayout
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:orientation="horizontal">
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="left"
安卓:text="More" />
<Space
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_weight="1"/>
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_gravity="left"
安卓:text="New"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
这是我的课程适配器。我用来为课程列表中的每门课程创建每个视图的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;
}
}
如果您需要更多信息,请告诉我。他还将听取关于实现同样目标的其他方法的建议。谢谢你的帮助。 ~z~赛斯
编辑:当评估列表布局时。setVisibility(View.VISIBLE)在onClick之外,它可以工作
进一步编辑:到目前为止我尝试过但没有成功的事情:
- 移动定义LinearLayout组件的位置
- 对父视图调用invalidate()
- 使用runOnUiThread()
- 在我试图为assessment\u list\u layout查找dviewbyd的那一行中,将view更改为v,它们是相同的,因此没有帮助李>
- 在评估列表布局上调用requestLayout()
更新:我现在已经设法在点击课程适配器布局时显示评估列表布局部分。现在唯一的问题是视图不再占用屏幕上的空间,它只是变成了一个可滚动的视图,可以上下滚动查看整个视图。 另外,当我滚动到fast时,它会将视图重置回启动时的状态
# 1 楼答案
保持这个变量的全局性,不需要使用
View.VISIBLE,
保持变量的唯一性不要在
your XML,
中使用这个,因为它会被混淆XML文件:
# 2 楼答案
1。视图可见性不工作
由于最初未渲染视图,因此可见性不起作用。移除xml中的可见性,并在适配器类中完全处理可见性。在“评估列表布局”行中,布局高度可以是硬编码的,因为在此布局中,列表视图高度已经硬编码。你可以硬编码到300并检查。这种方式将帮助视图获得初始渲染
2。滚动问题
滚动已经可见的“评估列表布局”视图时可能不可见。这是因为我们需要处理可见性,这种处理类似于listview中的复选框选择处理。希望课程类是model类,在其中添加另一个名为的属性isSelected为boolean,并将默认值设置为false。请参考以下课程:
课程类
请参阅适配器类中的以下代码更改
我已将我的更改注释为,以查找代码中的差异
# 3 楼答案
第1期
您不重用视图,这导致每次调用getView()时都会膨胀一个新视图;由于inflate()方法对IO敏感,它会降低滚动的平滑度,触发jank
试试这个
第二期
当Scorl listview时,需要重置itemview状态,在单击item setexpand=true时,向课程bean添加一个“expand”属性;然后在布局上方添加流动代码。setOnClickListener
第三期
在getView()方法中设置setOnClickListener,每次调用getView()时都会创建一个新的Clicker实例。使用列表视图。改为setOnItemClickListener()
提示:
毕竟,你应该使用RecyclerView而不是ListView,它是一个强大的UI小部件