我试图实现水平Recycler视图,但它不滚动光滑。Android简单的水平RecyclerView非常缓慢,不光滑
尝试了所有的以下解决方案我在谷歌发现,但没有一次成功:
popularCategoriesRecyclerView.setHasFixedSize(true);
popularCategoriesRecyclerView.setItemViewCacheSize(10);
popularCategoriesRecyclerView.setDrawingCacheEnabled(true);
popularCategoriesRecyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
setHasStableIds(true);
我也尝试使用Picasso/Glide
,甚至不加载图像可言,但它仍然不滚动顺利。
这里是我的代码:
public class MainActivity extends AppCompatActivity {
private RecyclerView popularCategoriesRecyclerView;
private RecyclerView.Adapter popularCategoriesAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RootCategory demoCategory = CreateDemoModelService.create();
popularCategoriesRecyclerView = (RecyclerView)findViewById(R.id.popular_categories_recycler_view);
popularCategoriesRecyclerView.setHasFixedSize(true);
popularCategoriesRecyclerView.setItemViewCacheSize(10);
popularCategoriesRecyclerView.setDrawingCacheEnabled(true);
popularCategoriesRecyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,true);
popularCategoriesRecyclerView.setLayoutManager(layoutManager);
popularCategoriesAdapter = new FavoriteCategoryAdapter(demoCategory.getFavoriteCategories());
popularCategoriesRecyclerView.setAdapter(popularCategoriesAdapter);
}
}
和适配器:
public class FavoriteCategoryAdapter extends RecyclerView.Adapter {
private List<Category> categories;
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView tv;
ImageView iv;
ViewHolder(View v) {
super(v);
tv = (TextView)v.findViewById(R.id.adapter_favorite_category_tv);
iv = (ImageView)v.findViewById(R.id.adapter_favorite_category_iv);
}
}
public FavoriteCategoryAdapter(List<Category> categories) {
this.categories = categories;
setHasStableIds(true);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_popular_category,parent,false);
return new FavoriteCategoryAdapter.ViewHolder(v);
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Category category = categories.get(position);
FavoriteCategoryAdapter.ViewHolder viewHolder = (FavoriteCategoryAdapter.ViewHolder) holder;
if (viewHolder.tv.getText() != null && viewHolder.tv.getText().equals(category.getName()))
return;
Context context = viewHolder.iv.getContext();
Resources resources = context.getResources();
final int resourceId = resources.getIdentifier(category.getIconName() + "_popular", "drawable",
context.getPackageName());
Picasso.with(context).load(resourceId).fit().centerCrop().into(viewHolder.iv);
viewHolder.tv.setText(category.getName());
}
@Override
public int getItemCount() {
return categories.size();
}
}
adapter_category.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/adapter_popular_category_bg">
<ImageView
android:id="@+id/adapter_favorite_category_iv"
android:layout_width="90dp"
android:layout_height="90dp"
android:scaleType="fitCenter"/>
<TextView
android:id="@+id/adapter_favorite_category_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:textColor="@android:color/white"
android:gravity="center"
android:background="@drawable/adapter_popular_category_tv_bg"/>
</LinearLayout>
activity_main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="right"
android:text="@string/activity_main_popular"
android:textColor="#75A735"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/popular_categories_recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:padding="0dp"
android:scrollbars="horizontal"/>
</LinearLayout>
</LinearLayout>
有人可以帮助我吗?难道我做错了什么?
显示recyclerview xml代码 –
添加了xml以及 –
你有没有试过CustomRecyclerView? – NareshRavva