标签: gradientdrawable

Android RecyclerView 不会以编程方式更改第一个项目的背景颜色

我正在创建一个带有RecyclerView. 每个列表项都是来自用户的帖子(现在是硬编码的)。每个帖子的背景都是从layer-list可绘制文件夹中的 XML 文件加载的。

文本等一切都按预期工作,但我正在尝试以编程方式更改背景颜色。它改变了每个项目的背景颜色,除了第一个项目,我不明白为什么。

第一项始终获取XML 文件中调用的内部solid颜色指定的背景颜色,因此不会更改,但后面的项获取 color 。shapeitemshape_background#ff22ff

这是适配器的实现:

class PostListAdapter extends RecyclerView.Adapter<PostListAdapter.PostViewHolder>{

    private LayoutInflater inflater;
    private List<PostRow> data = Collections.emptyList();

    PostListAdapter(Context context, List<PostRow> data) {
        inflater = LayoutInflater.from(context);
        this.data = data;
    }

    @Override
    public void onBindViewHolder(PostViewHolder holder, int position) {

        PostRow current = data.get(position);
        holder.text.setText(current.text.toUpperCase());
        holder.time.setText(current.time.toUpperCase());
        holder.answers.setText(current.answers.toUpperCase());

        try {
            // "#ff22ff" will be changed to current.color, unique color for every post
            // That string is parsed …
Run Code Online (Sandbox Code Playgroud)

android shapes layer-list android-recyclerview gradientdrawable

5
推荐指数
1
解决办法
4171
查看次数

如何在Android中以编程方式为GradientDrawable设置TopLeftRadius和BottomLeftRadius

我有一个ShapeDrawable。我想以编程方式设置CornerRadius(TopLeftRadius和BottomLeftRadius)。

下面是我的xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:top="5dp"
    android:bottom="5dp">

    <shape android:shape="rectangle">
        <corners android:topLeftRadius="20dp"
            android:bottomLeftRadius="20dp"/>

        <solid android:color="#A2D368"/>
    </shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

android gradientdrawable

4
推荐指数
2
解决办法
2143
查看次数