Android Simple Horizo​​ntal RecyclerView非常慢而且不流畅

时间:2017-06-16 09:36:14

标签: android android-recyclerview

我试图实施水平循环播放器视图,但它根本不滚动。

尝试了我在google中找到的所有以下解决方案,但没有一个能够工作:

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>

有人能帮助我吗?我做错了吗?

3 个答案:

答案 0 :(得分:0)

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

class FavoriteCategoryAdapter extends RecyclerView.Adapter<FavoriteCategoryAdapter.ViewHolder>

您还需要在函数 onBindViewHolder 中更改适配器函数 onCreateViewHolder 的返回类型和持有者的参数类型

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

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

答案 1 :(得分:0)

这一行:package com.acme.templates.components; import com.acme.templates.components.field.definition.ColorPickerDefinition; import info.magnolia.dam.app.ui.config.DamConfig; import info.magnolia.module.blossom.annotation.TabFactory; import info.magnolia.module.blossom.annotation.Template; import info.magnolia.module.blossom.annotation.TemplateDescription; import info.magnolia.ui.form.config.TabBuilder; import info.magnolia.ui.framework.config.UiConfig; import javax.jcr.RepositoryException; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @Template(id = "acmemodule:components/example", title = "Example") @TemplateDescription(" ") public class Example { @RequestMapping("/example") public String render() throws RepositoryException { return "components/example.ftl"; } @TabFactory("LineExample") public void addDialog(UiConfig cfg, TabBuilder tab, DamConfig dam) { ColorPickerDefinition cpd = new ColorPickerDefinition(); cpd.setColorPicker("cp"); cpd.setName("Name"); cpd.setLabel("Label"); tab.fields( ).definition().addField(cpd); } } 非常昂贵,官方文档说:

  

注意:不鼓励使用此功能。按标识符检索资源比按名称

检索资源要高效得多

现在在你的情况下,我猜你没有选择,因为你想动态找到合适的图像。您可以做的是在适配器的构造函数中找到所有资源标识符,并将它们存储在final int resourceId = resources.getIdentifier(category.getIconName() +"_popular", "drawable",context.getPackageName());中。

希望这有帮助。

答案 2 :(得分:0)

好的,我发现了问题所在。

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