【发布时间】:2018-04-01 02:49:49
【问题描述】:
在我的 android 活动中,我使用的是 RecyclerView,其中包含 MathViews 的数量。 MathView 是一个显示 LaTeX 内容的第三方库(这个
有点类似于WebView。在这个android项目上可以看到MathView的实现。 github.com/lingarajsankaravelu/Katex)。
问题是,要渲染这个MathView 的内容,需要更长的时间。因为我在RecycleView 中使用了很少的MathView 组件,并且渲染时间增加了更多。因此,当 Activity 启动时,首先在视图中会显示一些空白区域几秒钟,然后呈现相关内容。
作为这个问题的解决方案,我需要显示一个进度条,直到 Activity 的所有布局内容完全渲染,渲染完成后显示 Activity。
相关源码如下所示。
数学视图;
<?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"
xmlns:auto="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:id="@+id/equation_item"
android:clickable="true"
android:foreground="?attr/selectableItemBackground">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="4dp">
<katex.hourglass.in.mathlib.MathView
android:id="@+id/math_view"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
app:setClickable="true"
app:setTextColor="@color/colorPrimary"
app:setTextSize="10sp"
/>
</android.support.v7.widget.CardView>
<include layout="@layout/item_divider"/>
</LinearLayout>
回收站视图;
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.a37moons.mathcheatsheets.ContentActivity"
tools:showIn="@layout/activity_content">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_equations"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
ViewHolder 类
public class ViewHolder extends RecyclerView.ViewHolder {
public MathView mathView;
public ViewHolder(View itemView) {
super(itemView);
mathView = itemView.findViewById(R.id.math_view);
}
}
回收器适配器类
public class RecyclerAdapterEquations extends RecyclerView.Adapter<ViewHolder>{
private List<Equation> equations;
View view;
public RecyclerAdapterEquations(List<Equation> equations){
this.equations = equations;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.equation_view_item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Equation sampleEquation = equations.get(position);
holder.mathView.setDisplayText(sampleEquation.equationString);
// holder.mathView2.setText(sampleEquation.equationString);
Log.d("MATH_APP","position "+position+" mathview text set ");
}
@Override
public int getItemCount() {
return equations.size();
}
}
终于实现了。
RecyclerView recyclerView;
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(new RecyclerAdapterEquations(sample));
【问题讨论】:
-
如果您有预期的复杂视图和布局。我不确定延迟是否是由系统引起的,如果您真的按照预期使用 RecyclerView,除非它运行在非常非常低的硬件配置文件中。听起来问题可能是您如何获取数据以填充 RV
-
您所指的项目是用 JavaScript 编写的。你是在使用 React Native 还是类似的东西进行开发?
-
除非您使用实际代码更新问题,否则不清楚您在问什么。
-
我可以从你的代码中看到 RecyclerView 的实现是可以的。在 bindView() 方法中,您没有进行任何昂贵的操作。您的适配器简单明了。它与每个 recyclerView 项目中 MathView 项目的渲染有关。为了确保您可以用一些简单的 TextView 替换您的 MathView 并查看行为。如果行为看起来不错,您需要再次检查库以检查其行为缓慢的原因。
-
@AshikeeAbHi 如果我们使用 TextView,它会以正常方式运行并且不会延迟渲染。但是 MathView 本身需要一些时间来呈现其内容。这是 MathView 的正常行为。
标签: java android android-layout android-webview android-view