将卡片颜色随机发送给onClick

时间:2019-01-03 16:11:23

标签: android

我有RecyclerView +卡。在卡片中,一个元素必须是随机颜色。 我创建了一个颜色数组并以这种方式实现了任务-

 @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
    View view = layoutInflater.inflate(R.layout.model_poem, parent, false);

    int[] androidColors = view.getResources().getIntArray(R.array.androidColors);
    int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
    if (frameLayout != null) {
        frameLayout.setBackgroundColor(randomAndroidColor);
    }

    return new ViewHolder(view);
}

单击卡片时,将打开具有完整信息的活动,并且工具栏的颜色必须与卡片中元素的颜色一致。

     public ViewHolder(View itemView) {
        super(itemView);

        titleTv = (TextView) itemView.findViewById(R.id.tv_title);
       ..............
        frameLayout = (FrameLayout) itemView.findViewById(R.id.frame_layout);

          itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), ReadActivity.class);
                long poemId = (Long) v.getTag();
                intent.putExtra("randomAndroidColor", frameLayout.getBackground().toString());
                onPoemClickListener.onPoemClick(poemId);
            }
        });
    }
}

public interface OnPoemClickListener {
    void onPoemClick(long poemId);

}

课堂活动

    public class ReadActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
    public static final String EXTRA_POEM_ID = "poem_id";
   .....
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.read);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       tv_Title = (TextView) findViewById(R.id.tv_title);
      .......
        Intent intent = getIntent();
    randomAndroidColor = intent.getStringExtra("randomAndroidColor");
    toolbar.setBackgroundColor(Integer.parseInt(randomAndroidColor));
    poemId = getIntent().getLongExtra(EXTRA_POEM_ID, -1);
        if (poemId == -1) {
            finish();
        }

        getSupportLoaderManager().initLoader(
                0, 
                null, 
                this 
        );
}

应用程序崩溃,错误为E / AndroidRuntime:致命异常:主     流程:rodionova.lyubov.brodsky,PID:31328     java.lang.RuntimeException:无法启动活动ComponentInfo {rodionova.lyubov.brodsky / rodionova.lyubov.brodsky.activity.ReadActivity}:java.lang.NumberFormatException:s == null

2 个答案:

答案 0 :(得分:1)

在这种情况下,我认为最简单的方法是将颜色int保存为FrameLayout的 tag ,然后在要将其传递给活动。这样,您不必担心处理背景可绘制对象和/或将其转换为颜色。

设置标签:

int[] androidColors = view.getResources().getIntArray(R.array.androidColors);
int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
if (frameLayout != null) {
    frameLayout.setBackgroundColor(randomAndroidColor);
    frameLayout.setTag(randomAndroidColor);
}

将标记值传递给活动:

@Override
public void onClick(View v) {
    Intent intent = new Intent(v.getContext(), ReadActivity.class);
    long poemId = (Long) v.getTag();
    intent.putExtra("randomAndroidColor", (int) frameLayout.getTag());
    onPoemClickListener.onPoemClick(poemId);
    // presumably you trimmed out something that actually starts the Intent
}

并获取颜色:

// randomAndroidColor should be an int now
randomAndroidColor = intent.getIntExtra("randomAndroidColor", 0);
toolbar.setBackgroundColor(randomAndroidColor);

答案 1 :(得分:0)

我认为问题是frameLayout.getBackground()不返回颜色int,它返回一个可绘制对象,因此当您在该可绘制对象上使用toString()时,它不会返回颜色字符串,而是返回其他内容。 您可以使用Ben P的编码方法来解决此问题,这很好。