Android 导致重大性能问题的RecyclerView onBindViewHolder
我有一个Android 导致重大性能问题的RecyclerView onBindViewHolder,android,android-recyclerview,Android,Android Recyclerview,我有一个测试活动: public class TestActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test); // Setup the T
测试活动
:
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
// Setup the Tabs ViewPager
final SectionsPagerAdapter pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
ViewPager pager = (ViewPager) findViewById(R.id.pager);
// Set the number of tabs to keep in memory
pager.setOffscreenPageLimit(3);
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
int currentPosition = 0;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int newPosition) {
FragmentLifecycle fragmentToShow = (FragmentLifecycle)pagerAdapter.getItem(newPosition);
fragmentToShow.onResumeFragment();
FragmentLifecycle fragmentToHide = (FragmentLifecycle)pagerAdapter.getItem(currentPosition);
fragmentToHide.onPauseFragment();
currentPosition = newPosition;
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
pager.setAdapter(pagerAdapter);
// Attach the ViewPager to the TabLayout
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(pager);
}
private class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new TestFragment();
case 1:
return new TestFragment();
case 2:
return new TestFragment();
case 3:
return new TestFragment();
}
return null;
}
@Override
public int getCount() {
return 4;
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Test1";
case 1:
return "Test2";
case 2:
return "Test3";
case 3:
return "Test4";
}
return null;
}
}
}
它通过ViewPager
创建4个TestFragment
:
public class TestFragment extends Fragment implements FragmentLifecycle {
private RecyclerView testRecycler;
private ArrayList<TestItem> items;
public TestFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View layout = inflater.inflate(R.layout.fragment_test, container, false);
/////////////////// CREATE TEST LIST //
items = new ArrayList<TestItem>();
items.add(new TestItem(0, "First1", "Last1"));
items.add(new TestItem(1, "First2", "Last2"));
items.add(new TestItem(2, "First3", "Last3"));
items.add(new TestItem(3, "First4", "Last4"));
items.add(new TestItem(4, "First5", "Last5"));
items.add(new TestItem(5, "First6", "Last6"));
items.add(new TestItem(6, "First7", "Last7"));
items.add(new TestItem(7, "First8", "Last8"));
items.add(new TestItem(8, "First9", "Last9"));
items.add(new TestItem(9, "First10", "Last10"));
items.add(new TestItem(10, "First11", "Last11"));
items.add(new TestItem(11, "First12", "Last12"));
items.add(new TestItem(12, "First13", "Last13"));
items.add(new TestItem(13, "First14", "Last14"));
items.add(new TestItem(14, "First15", "Last15"));
items.add(new TestItem(15, "First16", "Last16"));
items.add(new TestItem(16, "First17", "Last17"));
items.add(new TestItem(17, "First18", "Last18"));
items.add(new TestItem(18, "First19", "Last19"));
items.add(new TestItem(19, "First20", "Last20"));
items.add(new TestItem(20, "First21", "Last21"));
items.add(new TestItem(21, "First22", "Last22"));
items.add(new TestItem(22, "First23", "Last23"));
items.add(new TestItem(23, "First24", "Last24"));
items.add(new TestItem(24, "First25", "Last25"));
items.add(new TestItem(25, "First26", "Last26"));
items.add(new TestItem(26, "First27", "Last27"));
items.add(new TestItem(27, "First28", "Last28"));
items.add(new TestItem(28, "First29", "Last29"));
items.add(new TestItem(29, "First30", "Last30"));
/////////////////// SETUP THE TEST LIST //
testRecycler = (RecyclerView) layout.findViewById(R.id.test_recycler);
TestAdapter testAdapter = new TestAdapter(getContext(),
items);
testRecycler.setAdapter(testAdapter);
GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
testRecycler.setLayoutManager(gridLayoutManager);
return layout;
}
@Override
public void onPauseFragment() {
}
@Override
public void onResumeFragment() {
}
}
但在绑定过程中,它的速度太慢了。加载所有4个碎片最多需要5秒钟
我已经尝试将setAdapter方法移动到AsyncTask
,这样我可以加载片段,然后绑定数据,但由于UI线程隔离,这无法工作
但我忍不住认为我做错了什么,因为这不应该花这么长时间
我感觉问题可能出在RecyclerView
中使用的cardwiew
的视图模型中:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="2dp"
android:theme="@style/NormalTextAppearance">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image_view"
android:layout_width="60dp"
android:layout_height="60dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/ic_default_pic"/>
<TextView
android:id="@+id/fullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="TextView"
app:layout_constraintLeft_toRightOf="@+id/image_view"
card_view:layout_constraintBottom_toBottomOf="@+id/radioGroup"
card_view:layout_constraintTop_toTopOf="@+id/radioGroup"/>
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:padding="10dp"
app:layout_constraintBottom_toBottomOf="@+id/image_view"
app:layout_constraintTop_toTopOf="@+id/image_view"
card_view:layout_constraintEnd_toEndOf="parent">
<RadioButton
android:id="@+id/failing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:buttonTint="@color/belowExpectations"
card_view:layout_constraintEnd_toEndOf="parent"
card_view:layout_constraintTop_toTopOf="parent"/>
<RadioButton
android:id="@+id/struggling"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:buttonTint="@color/meetingExpectations"
card_view:layout_constraintEnd_toStartOf="@+id/belowExpectations"
card_view:layout_constraintTop_toTopOf="parent"/>
<RadioButton
android:id="@+id/meeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:buttonTint="@color/exceedingExpectations"
card_view:layout_constraintEnd_toStartOf="@+id/meetingExpectations"
card_view:layout_constraintTop_toTopOf="parent"/>
<RadioButton
android:id="@+id/exceeding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:buttonTint="@color/excellingExpectations"
card_view:layout_constraintEnd_toStartOf="@+id/exceedingExpectations"
card_view:layout_constraintTop_toTopOf="parent"/>
</RadioGroup>
<CheckBox
android:id="@+id/absentCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Absent"
app:layout_constraintBottom_toBottomOf="@+id/radioGroup"
app:layout_constraintEnd_toStartOf="@+id/radioGroup"
app:layout_constraintTop_toTopOf="@+id/radioGroup"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
与此抗争。。。如果有人能提出一种延迟数据绑定的方法,并且只需先加载片段,那将是一种最糟糕的情况。有一个碎片对话框
说“加载”正在进行。当我尝试这样做时,正如前面提到的,我失败得很惨。findViewById
在性能方面非常昂贵。
您应该做的是仅在创建ViewHolder
时使用它(在ViewHolder构造函数中),在ViewHolder
中保留对视图的引用,然后在绑定ViewHolder
时访问它,如:
holder.textView.setText("hello")
持证人:
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public CheckBox checkBox;
public ViewHolder(CardView view) {
super(view);
textView = view.findViewByid(...)
...
}
}
我将尝试一下,看看它是否能提高性能。干杯没什么影响。。。但是谢谢你的邀请suggestion@dantan04奇怪,我敢打赌这会奏效。你做的任何事情都不会导致任何性能问题,我已经更新了测试适配器上的代码来反映这一点,你觉得还好吗?@dantan04是的,我觉得一切都很好。项目布局也很好,不是太嵌套。您确定onBindView中发生了延迟吗?为什么GridLayoutManager
?看起来您可以使用LinearLayoutManager
。目前还不清楚这是否有助于提高性能,但值得一试。我认为LinearLayoutManager
至少会稍微好一点。还有为什么pager.setOffscreenPageLimit(4)
?这将导致一次生成五个(当前+屏幕外4个)页面。真的需要吗?我选择了GridLayoutManager
,因为我想为用户提供动态更改布局的选项。然而,我只是尝试了一下LinearLayoutManager
,但没有取得任何成功。不过我还是很感激你的选择。谢谢你发现了屏幕外的凝胶,我刚刚整理好了。
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public CheckBox checkBox;
public ViewHolder(CardView view) {
super(view);
textView = view.findViewByid(...)
...
}
}