2017-06-16 94 views
0

我试图实现水平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> 

有人可以帮助我吗?难道我做错了什么?

+0

显示recyclerview xml代码 –

+0

添加了xml以及 –

+0

你有没有试过CustomRecyclerView? – NareshRavva

回答

0

好吧,我发现了什么问题。

我的活动有一个背景图片,显然是太大了(发现它,因为它没有显示在某些设备上),所以在我降低背景图像的分辨率后,一切工作顺利按预期。

0

您需要使用自己的ViewHolder扩展适配器。

class FavoriteCategoryAdapter extends RecyclerView.Adapter<FavoriteCategoryAdapter.ViewHolder> 

您还需要更改适配器功能的功能 onCreateViewHolder和参数式支架的onBindViewHolder

public void onBindViewHolder(FavoriteCategoryAdapter.ViewHolder holder, int position) 

public FavoriteCategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
+0

更好,仍然没有我想象的那么顺利 –

0

此行的返回类型:final int resourceId = resources.getIdentifier(category.getIconName() +"_popular", "drawable",context.getPackageName());是相当昂贵的,官方文件说, :

注意:不鼓励使用此功能。通过标识符检索资源比按名称检索资源要高效得多

现在在你的情况下,我想你没有选择,因为你想动态地找到正确的图像。你可以做的是在适配器的构造函数中找到所有的资源标识符并将它们存储在Map中。

希望这会有所帮助。