- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个根 recyclerview,每个项目都填充了这个布局:
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:id="@+id/headerText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/accent_gradient"
android:ellipsize="end"
android:maxLines="1"
android:padding="8dp"
android:paddingStart="16dp"
android:text="My original"
android:textColor="@color/colorWhite"
android:textSize="14sp" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rootRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
/>
</LinearLayout>
如您所见,每个项目中都有另一个recyclerview。我想用headers实现分段recyclerview的效果。 (所以标题>列表标题>列表)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_toStartOf="@id/relativeLayout3"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:id="@+id/foodName_fv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:text="Food name"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/colorBlack"
android:textSize="18sp"
android:transitionName="Food_Name"/>
<TextView
android:layout_below="@id/foodName_fv"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:id="@+id/foodGrams_fv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text="Grams"
android:textColor="@color/colorLightGrey"
android:textSize="15sp" />
<LinearLayout
android:id="@+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:orientation="vertical"
android:layout_alignParentEnd="true"
>
<TextView
android:id="@+id/foodCalories_fv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Calories"
android:textColor="@color/colorBlack"
android:textSize="18sp"
android:transitionName="Food_Cal"/>
<TextView
android:layout_gravity="center"
android:id="@+id/calText_fv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingBottom="8dp"
android:text="@string/calText_fv"
android:textColor="@color/colorLightGrey"
android:textSize="14sp" />
</LinearLayout>
</RelativeLayout>
这是一个相当简单的布局,但由于某些奇怪的原因,子 recyclerview 需要花费大量时间来填充这些行。我做了一些测试,加载 300 行(平均)需要 1200 毫秒!
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_view, parent, false);
return new ViewHolder(view);
}
总之,child recyclerview 需要花费太多时间来完成这项工作。是的,我知道我可以做“延迟加载”,但我想弄清楚为什么它这么慢?
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class SelectFoodRootRecyclerAdapter extends RecyclerView.Adapter<SelectFoodRootRecyclerAdapter.ViewHolder> {
private static final String TAG = "SelectFoodRootRecyclerAdapter";
private Context mContext;
private ArrayList<SectionedFoodGroup> items;
private ArrayList<SelectFoodChildRecyclerAdapter> adapters; //For filter use outside this class
RecyclerView.RecycledViewPool recycledViewPool;
public SelectFoodRootRecyclerAdapter(Context mContext) {
this.mContext = mContext;
items = new ArrayList<>();
adapters = new ArrayList<>();
recycledViewPool = new RecyclerView.RecycledViewPool();
SectionedFoodGroup section1 = new SectionedFoodGroup(mContext.getString(R.string.myFoods),
FoodsDBHelper.getAllFoodRows(Food.DBType.USER_CREATED_FOODS_DB.ordinal()));
SectionedFoodGroup section2 = new SectionedFoodGroup(mContext.getString(R.string.foods),
FoodsDBHelper.getAllFoodRows(Food.DBType.REGULAR_FOOD_DB.ordinal()));
addSection(section1);
addSection(section2);
}
private void addSection(SectionedFoodGroup sectionedFoodGroup){
if(sectionedFoodGroup.getFoods().size() > 0){
items.add(sectionedFoodGroup);
adapters.add(new SelectFoodChildRecyclerAdapter(mContext, sectionedFoodGroup.getFoods()));
}
}
public ArrayList<SelectFoodChildRecyclerAdapter> getAdapters(){
return adapters;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_sfa_viewholder, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
SectionedFoodGroup sectionedFoodGroup = items.get(position);
holder.headerText.setText(sectionedFoodGroup.getHeaderName());
holder.foodsRecycler.setAdapter(adapters.get(position));
holder.foodsRecycler.setRecycledViewPool(recycledViewPool);
}
@Override
public int getItemCount() {
return items != null ? items.size() : 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView headerText;
RecyclerView foodsRecycler;
public ViewHolder(@NonNull View itemView) {
super(itemView);
headerText = itemView.findViewById(R.id.headerText);
foodsRecycler = itemView.findViewById(R.id.rootRecycler);
}
}
}
子 recyclerview 的代码(包含所有实际行的代码):
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import java.text.DecimalFormat;
import java.util.ArrayList;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
public class SelectFoodChildRecyclerAdapter extends RecyclerView.Adapter<SelectFoodChildRecyclerAdapter.ViewHolder>{
private static final String TAG = "SelectFoodChildRecyclerAdapter";
private Context mContext;
private ArrayList<Food> original;
private DecimalFormat decimalFormat;
public SelectFoodChildRecyclerAdapter(Context mContext, ArrayList<Food> foods) {
this.mContext = mContext;
this.original = foods;
decimalFormat = new DecimalFormat("#.0");
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_view, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int foodPos) {
Food currFood = original.get(foodPos);
holder.foodName.setText(currFood.getName());
float grams = 100;
if(currFood.getAmount() != 0) grams = (float)currFood.getAmount();
int roundedGrams = (int)Math.round(grams);
holder.foodGrams.setText(roundedGrams +" "+ mContext.getResources().getString(R.string.gramsWord));
float caloriesToShow = (float)currFood.getCalories();
holder.foodCalories.setText(decimalFormat.format(caloriesToShow));
}
@Override
public int getItemCount() {
return original != null ? original.size() : 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView foodName;
TextView foodGrams;
TextView foodCalories;
public ViewHolder(View itemView) {
super(itemView);
foodName = itemView.findViewById(R.id.foodName_fv);
foodGrams = itemView.findViewById(R.id.foodGrams_fv);
foodCalories = itemView.findViewById(R.id.foodCalories_fv);
}
}
}
调用 Activity onCreate 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_food);
foodsRecyclerView = findViewById(R.id.rootRecycler);
selectFoodRootRecyclerAdapter = new SelectFoodRootRecyclerAdapter(this);
foodsRecyclerView.setAdapter(selectFoodRootRecyclerAdapter);
}
同样,正如您所看到的,所有代码都相当简单..
最佳答案
好吧,因为没有人能够回答我的问题,而我已经找到了我的答案,然后我会与其他可能会在 future 看到这个并认为的人分享它,哇,这正是我所需要的。
所以.. 事实证明在 RecyclerView 中使用 RecyclerView 是一个很大的禁忌。那是因为 onCreateViewHolder 方法被调用的次数与您的项目数组大小一样多,而且这非常重,特别是如果您有一个很大的列表。 (我认为它与 ScrollView 中的 RecyclerView 相同) - 所以尽量避免使用这种类型的实现。
所以,你可以做什么?
选项 1 - 创建一个多 View RecyclerView 适配器。这意味着您有一个适配器,它可以处理多种 View 类型(非常适合我想要的。一种 View 类型是标题,第二种 View 类型是另一种布局,用作我的主行)。
您可以在 google 中查找有关如何实现它的良好结果,但主要
所以声明这些变量:
private int VIEW_TYPE_HEADER = 0;
private int VIEW_TYPE_MAIN_ROW = 1;
Next 覆盖 next 方法,这基本上是做什么的
@Override
public int getItemViewType(int itemPos) {
if(items.get(itemPos) instanceof String){
return VIEW_TYPE_HEADER;
}
return VIEW_TYPE_MAIN_ROW;
}
在 onCreateViewHolder 上,我们可以测试以查看我们正在处理的行类型并相应地膨胀。
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if(viewType == VIEW_TYPE_HEADER){
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.header)_layout, parent, false);
return new HeaderViewHolder(view);
}
//Else a regular row.
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_row, parent, false);
return new MainViewHolder(view);
}
当然,为每种类型制作一个单独的 View 持有者。
关于Android studio RecyclerView onCreateViewHolder 布局膨胀好慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63365747/
我需要使用view.getWidth(),但我不能,因为我还没有膨胀 View 。在使用 view.getWidth() 之前如何膨胀 View ? MainActivity 类: p
这个问题很难尝试和表述,但我会尽力而为。 基本上,我有一个应用程序,我想将代码拆分得更多。为了尝试解释这一点,我将举一个我的屏幕示例。 在我的主屏幕上,我有一个标题、用户详细信息、余额、下一个账单详细
我有一个 TableLayout,我在其中动态添加行,一开始是空的,我想在用户单击它时在该行中加载一个 xml。 我已经给行赋值了OnClick方法,但是不知道在onclick方法中进入时如何加载xm
我使用的是 C++ 中的 libcurl 库,这是一个相当大的库(.lib 文件大约 2MB),项目是用 CURL_STATICLIB 编译的 至于现在我只有一个 .cpp 文件,其中包含 heade
背景: 我正在努力使一堆 PNG 尽可能小。我正在使用诸如 PngOut、PngCrush 和 OptiPng 之类的工具。 问题: 我遇到了一个大小为 1434 KB 但只有 230 x 230 像
我一直在努力找出我的应用程序使用的内存不断增长的原因,直到在 heroku 上的生产中它失败并下降。 经过一些相当广泛的研究,使用 Top 观察 RSIZE 在本地增长并使用 Oink 尝试查明我只是
我有一个使用 C++ 版本的 zlibs deflate 压缩的数据 ArrayBuffer(使用默认值)。由于没有服务器,我现在需要在客户端中增加这些数据。我已经使用 C++ 中的默认 inflat
我的代码有问题: @Override public View getView(int position, View convertView, ViewGroup parent) {
我有一个 fragment ,我像下面一样对其进行膨胀,但它给出了运行时错误: public View onCreateView(LayoutInflater inflater, ViewGroup
我正在学习 Fragment,并且我是通过在 Fragment 中使用 FB Login 来学习的。但我不断收到异常 Error inflateing class com.facebook.login
第一种方法: LinearLayout parent = ...; View child = LayoutInflator.inflate(context, parent, true); 第二种方法:
我刚刚完成了一个站点构建,在该站点构建中,我们必须将文件提供给第 3 方以与他们的系统集成。构建必须是独立的,因为会有多方部署它们,能力水平各不相同。 我遇到的问题是,在下载我的 Javascript
我正在寻找一种方法来“扩充”Android XML 布局,这在编译时是未知的。我已经搜索了几个小时,总能找到答案,这是不可能的,因为 LayoutInflater 不能使用简单的 XML 文件。好的,
我有一个从 Faragment 扩展而来的类,叫做 Agenda。它的布局有一个 listView,但是当这个类返回到主 Activity 时,我得到了一个错误。这是错误和代码: > 03-11 13
我的应用程序一启动就崩溃了。我收到以下错误: android.view.InflateException: Binary XML file line #34: Error inflating clas
我想膨胀 R.id.catText,但如果我自己膨胀它,它永远不会显示。如果我inflate R.id.assets(容器),那么两个元素都会正常显示。我只是不想要容器。如何在不膨胀 R.id.ass
我尝试创建自定义 ViewGroup 类,但是当我使用方法 findViewById() 时它返回 null,但展开 View 是可以的。 代码是: public class HorizontalLi
我正在尝试创建一个在屏幕上滑动的 textView,但它会产生错误,但我不知道为什么。这是我的 xml: 还有我的 Activity 课: package com.exampl
我正在构建一个应用程序,我在其中使用了 glomadrain 的动画切换按钮。完成编码后出现以下错误: at com.android.internal.os.ZygoteInit.main(Zygot
我只是想按照 android dev 上的示例代码来膨胀 Activity 中的 fragment 。我有一个更复杂的项目正在工作,但我遇到了与这里的简单项目相同的错误。谁能指出我正确的方向? 扩展
我是一名优秀的程序员,十分优秀!