gpt4 book ai didi

android - RecyclerView 滚动滞后于简单的项目

转载 作者:行者123 更新时间:2023-11-30 00:32:16 31 4
gpt4 key购买 nike

我有一个简单的回收器 View ,其中包含内部只有 1 个 imageView 的卡片 View ,它是从资源中加载的,但是无论如何我在滚动时都有很大的滞后,我尝试了 recycler.setHasFixedSize(true) 但它没有帮助。

Activity 代码

public class MainActivity extends AppCompatActivity {

private final int[] drawables = {
R.drawable.nxt_bluetooth_dongle,
R.drawable.color_sensor,
R.drawable.connector_cabels,
R.drawable.e_motor,
R.drawable.energy_storage,
R.drawable.energy_display,
R.drawable.gyroscopic_sensor,
R.drawable.intelligent_nxt_brick,
R.drawable.keyfob_transponder,
R.drawable.light_sensor,
R.drawable.motion_sensor,
R.drawable.nxt_ir_receiver,
R.drawable.nxt_light_sensor,
R.drawable.nxt_servo_motor,
R.drawable.nxt_sound_sensor,
R.drawable.nxt_touch_sensor,
R.drawable.nxt_ultrasonic_sensor,
R.drawable.rf_id_sensor,
R.drawable.solar_panel,
R.drawable.tilt_sensor,
R.drawable.temperature_sensor,
R.drawable.usb_hub
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
MyAdapter adapter = new MyAdapter(drawables);
recycler.setAdapter(adapter);
recycler.setLayoutManager(new GridLayoutManager(this, 3));
}

private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {

private int[] drawables;

private MyAdapter(int[] drawables) {
this.drawables = drawables;
}

@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.lego_recycler_item, null, false);
return new MyHolder(view);
}

@Override
public void onBindViewHolder(MyHolder holder, int position) {
holder.legoImage.setImageResource(drawables[position]);
}

@Override
public int getItemCount() {
return drawables.length;
}

class MyHolder extends RecyclerView.ViewHolder {

ImageView legoImage;

MyHolder(View itemView) {
super(itemView);
legoImage = (ImageView) itemView.findViewById(R.id.legoImage);
legoImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, LegoDetailActivity.class);
intent.putExtra(LegoDetailActivity.EXTRA_DRAWABLE_ID, drawables[getAdapterPosition()]);
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
MainActivity.this, legoImage, "legoDetail"
);
startActivity(intent, options.toBundle());
}
});
}
}
}
}

Activity xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e0e0e0"
tools:context="com.lol.legomindstorms.MainActivity">

</android.support.v7.widget.RecyclerView>

回收器项目 xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardUseCompatPadding="true"
app:cardCornerRadius="0dp">

<ImageView
android:id="@+id/legoImage"
android:layout_width="115dp"
android:layout_height="115dp"
android:transitionName="legoDetail" />

</android.support.v7.widget.CardView>

最佳答案

根据我的经验,我认为问题在于加载多个图像
由于 android 在内存中加载大量图像确实存在问题,因为默认情况下 android 不允许大量的 gc memeory 大小,检查你的日志你有没有得到 gc 内存过载错误或类似的东西

1.你也可以尝试改变堆大小

2.Try using an external library 它总是说永远不要重新发明轮子尝试滑行或 piccaso

3.尝试自己清除位图内存

PS:我不是那种写手所以如果我的措辞不好请不要介意但是我处理过很多这些问题所以我非常熟悉这个问题

关于android - RecyclerView 滚动滞后于简单的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44085520/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com