我是android应用程序开发的新手,我最近制作了第一个应用程序,其中包含一个基本的TabLayout,并在MainActivity中包含两个片段。
在我的第二个选项卡中,它仅包含一个RecyclerView作为根布局。我所做的是用我自己的dataModel的数组列表填充RecyclerView。
我的RecyclerView有13个项目。这些项目的视图是https://github.com/florent37/ExpansionPanel的扩展布局,我完全按照他对RecyclerView所做的方式进行操作,并且效果很好。
但是,我的应用程序的启动时间已大大增加到 1s ++ 。我发现RecyclerViewAdapter中的onBindViewHolder方法是罪魁祸首。我尝试注释掉onBindViewHolder方法的内容,并且启动时间减少了一半,至600ms,这对我来说还可以。
所以我的问题是如何解决这个问题? 我应该以其他方式绑定数据吗?
我不想启动时间慢。 我看到了一些有关使用数据绑定API的帖子,它们在不同的线程和异步上运行,但我不知道该怎么做。
请帮助〜预先感谢!
这是我的适配器:
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private ArrayList<DataModel> list;
private final ExpansionLayoutCollection expansionsCollection = new ExpansionLayoutCollection();
MyAdapter(ArrayList<DataModel> list, Context context) {
this.list = list;
this.context = context;
expansionsCollection.openOnlyOne(true);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView == LayoutInflater.from(parent.getContext()).inflate(R.layout.expansion_panel, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
DataModel currentData = list.get(position);
switch (currentData.getType()) {
case DataModel.NO_FORMULA:
holder.titleText.setText(currentData.getTitle());
holder.contentText.setText(currentData.getContent());
holder.formulaView.removeAllViews();
expansionsCollection.add(holder.expansionLayout);
break;
case DataModel.WITH_FORMULA:
View formulaLayout = ((Activity)context).getLayoutInflater().inflate(currentData.getFormulaLayoutId(),null);
holder.titleText.setText(currentData.getTitle());
holder.contentText.setText(currentData.getContent());
holder.formulaView.removeAllViews();
holder.formulaView.addView(formulaLayout);
expansionsCollection.add(holder.expansionLayout);
break;
}
}
@Override
public int getItemCount() {
return list.size();
}
private static class ViewHolder extends RecyclerView.ViewHolder {
TextView titleText, contentText;
ExpansionLayout expansionLayout;
FrameLayout formulaView;
ViewHolder(View itemView) {
super(itemView);
this.titleText = itemView.findViewById(R.id.title_text);
this.contentText = itemView.findViewById(R.id.content_text);
this.expansionLayout = itemView.findViewById(R.id.expansionLayout);
this.formulaView = itemView.findViewById(R.id.formula_view);
}
}
这是我的物品布局:
<?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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:ignore="ContentDescription,RtlHardcoded,HardcodedText">
<com.github.florent37.expansionpanel.ExpansionHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
app:expansion_headerIndicator="@id/headerIndicator"
app:expansion_layout="@id/expansionLayout"
app:expansion_toggleOnClick="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:minHeight="50dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:theme="@style/RecyclerViewTheme">
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/headerIndicator"
android:textColor="#000" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/headerIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:adjustViewBounds="true"
app:srcCompat="@drawable/ic_expansion_header_indicator_grey_24dp" />
</RelativeLayout>
</com.github.florent37.expansionpanel.ExpansionHeader>
<com.github.florent37.expansionpanel.ExpansionLayout
android:id="@+id/expansionLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:expansion_expanded="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#F5F5F5">
<TextView
android:id="@+id/content_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:textColor="#000" />
<FrameLayout
android:id="@+id/formula_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</com.github.florent37.expansionpanel.ExpansionLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.7dp"
android:background="?android:attr/listDivider" />
</LinearLayout>
这是我的数据模型:
public class DataModel {
public static final int WITH_FORMULA = 0;
public static final int NO_FORMULA = 1;
private String title, content;
private int type, formulaLayoutId;
DataModel(int type, String title, String content) {
this.type = type;
this.title = title;
this.content = content;
}
DataModel(int type, String title, String content, int formulaLayoutId) {
this.type = type;
this.title = title;
this.content = content;
this.formulaLayoutId = formulaLayoutId;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public int getFormulaLayoutId() {
return formulaLayoutId;
}
public int getType() {
return type;
}
*我已经创建了13个带有长标题和长段落作为内容的DataModel对象,并将它们放在arraylist中。当用户单击扩展面板的标题时,扩展布局将展开以显示其内容。
在recyclerview的13个项目中,只有2个是WITH_FORMULA的类型,该类型必须使FormulaLayout膨胀。
答案 0 :(得分:0)
如果您确定问题的根源是onBindViewHolder
,那么唯一的问题就是通货膨胀。
inflate
是非常昂贵的操作,因为它先解析XML,然后创建视图层次结构。不要在onBindViewHolder
中这样做。改为在onCreateViewHolder
中对视图进行一次充气。如果您不想在NO_FORMULA
情况下显示它,只需将其可见性设置为GONE
,在VISIBLE
情况下设置为WITH_FORMULA
。更改可见性是更简单的操作。