Java 背景色或图像在recyclerview中滚动时混洗?
我想知道,当我向下或向上滚动时,我的图像和布局的颜色会出现混乱,我使用recyclerview创建了cardview。并将图像(单击时更改颜色以了解其是否为用户喜爱的项目)和setbackgroundcolor(随机选择)设置为父布局,以使cardview具有吸引力。但当我滚动时 1.图像改变位置的图像, 2.布局背景会自动更改颜色 我在这里发布我的适配器代码Java 背景色或图像在recyclerview中滚动时混洗?,java,android,android-recyclerview,Java,Android,Android Recyclerview,我想知道,当我向下或向上滚动时,我的图像和布局的颜色会出现混乱,我使用recyclerview创建了cardview。并将图像(单击时更改颜色以了解其是否为用户喜爱的项目)和setbackgroundcolor(随机选择)设置为父布局,以使cardview具有吸引力。但当我滚动时 1.图像改变位置的图像, 2.布局背景会自动更改颜色 我在这里发布我的适配器代码 public class TOAdapter extends RecyclerView.Adapter<TOAdapter.Vie
public class TOAdapter extends RecyclerView.Adapter<TOAdapter.ViewHolder> {
JSONArray jsonArray;
private String title;
private String image;
private ImageLoader imageLoader;
private String subtitle;
private String subti;
private Context context;
private ImageView clip;
public TOAdapter(JSONArray jsonArray) {
this.jsonArray = jsonArray;
}
// Create new views (invoked by the layout manager)
@Override
public TOAdapter.ViewHolder onCreateViewHolder(final ViewGroup parent,
int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.top_twenty_list, parent, false);
final ViewHolder viewHolder = new ViewHolder(itemLayoutView);
final Random random = new Random(System.currentTimeMillis());// We add 155 since we want at least 155 in each channel.// Then we add to it a random number between 0 and 100.
int r = 155 + random.nextInt(101);
int g = 155 + random.nextInt(101);
int b = 155 + random.nextInt(101);
int color = Color.rgb(r, g, b);
viewHolder.frame.setBackgroundColor(color);
viewHolder.layer.setBackgroundColor(color);
clip = (ImageView) itemLayoutView.findViewById(R.id.ic_clip);
clip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int iColor = Color.parseColor("#0000FF");
int red = (iColor & 0xFF0000) / 0xFFFF;
int green = (iColor & 0xFF00) / 0xFF;
int blue = iColor & 0xFF;
float[] matrix = {0, 0, 0, 0, red
, 0, 0, 0, 0, green
, 0, 0, 0, 0, blue
, 0, 0, 0, 1, 0};
ColorFilter colorFilter = new ColorMatrixColorFilter(matrix);
clip.setColorFilter(colorFilter);
}
});
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(final ViewHolder viewHolder, int position) {
// - get data from your itemsData at this position
// - replace the contents of the view with that itemsData
// myTypeface = Typeface.createFromAsset(context.getAssets(), "fonts/RobotoCondensedBoldItalic.ttf");
try {
JSONObject obj = jsonArray.getJSONObject(position);
title = obj.getString("title");
image = obj.getString("brand_logo");
subtitle = obj.getString("sub_title");
} catch (JSONException e) {
e.printStackTrace();
}
viewHolder.txtViewTitle.setText(subtitle);
viewHolder.subtitle.setText(title);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
String full_Url = "http://mycompany/assets/new" + image;
viewHolder.thumbnail.setImageUrl(full_Url, imageLoader);
viewHolder.btn_get_deal.setTag(position);
viewHolder.btn_get_deal.setOnClickListener(new View.OnClickListener() {
public JSONObject obj;
public ArrayList<String> offerlist = new ArrayList();
@Override
public void onClick(View view) {
Intent offerpage = new Intent(AppController.getInstance().getApplicationContext(), OfferDetails.class);
Integer pos = (Integer) view.getTag();
try {
obj = jsonArray.getJSONObject(pos);
offerpage.putExtra("jsonObj", obj.toString());
} catch (JSONException e) {
e.printStackTrace();
}
//offerpage.getParcelableArrayListExtra(offerlist);
offerpage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
AppController.getInstance().getApplicationContext().startActivity(offerpage);
}
});
//viewHolder.txtViewTitle.setTypeface(myTypeface);
}
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
private final NetworkImageView thumbnail;
private final RelativeLayout frame;
private final RelativeLayout layer;
public TextView txtViewTitle;
public TextView subtitle;
public ImageView clip;
public CardView btn_get_deal;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.txttitle_toptwenty);
subtitle = (TextView) itemLayoutView.findViewById(R.id.sub_title_toptwenty);
thumbnail = (NetworkImageView) itemLayoutView.findViewById(R.id.thumbnail_topwenty);
frame = (RelativeLayout) itemLayoutView.findViewById(R.id.frame);
layer = (RelativeLayout) itemLayoutView.findViewById(R.id.layer);
btn_get_deal = (CardView) itemLayoutView.findViewById(R.id.card_view);
}
}
// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount() {
return jsonArray.length();
}
公共类TOAdapter扩展了RecyclerView.Adapter{
JSONArray JSONArray;
私有字符串标题;
私有字符串图像;
私有图像加载器;
私有字符串字幕;
私有字符串subti;
私人语境;
私人影像剪辑;
公共TOAdapter(JSONArray JSONArray){
this.jsonArray=jsonArray;
}
//创建新视图(由布局管理器调用)
@凌驾
public to Adapter.ViewHolder onCreateViewHolder(最终视图组父级,
int视图类型){
//创建新视图
View itemLayoutView=LayoutFlater.from(parent.getContext())
.充气(R.layout.top_二十_列表,父项,假);
最终ViewHolder ViewHolder=新的ViewHolder(itemLayoutView);
final Random Random=new Random(System.currentTimeMillis());//我们加155,因为我们希望每个通道中至少有155个。//然后我们加上一个0到100之间的随机数。
int r=155+random.nextInt(101);
int g=155+random.nextInt(101);
int b=155+随机。下一个(101);
int color=color.rgb(r,g,b);
viewHolder.frame.setBackgroundColor(颜色);
viewHolder.layer.setBackgroundColor(颜色);
clip=(ImageView)itemLayoutView.findviewbyd(R.id.ic\u clip);
clip.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
int iColor=Color.parseColor(#0000FF”);
int red=(iColor&0xFF0000)/0xFFFF;
int绿色=(iColor&0xFF00)/0xFF;
int蓝色=iColor&0xFF;
float[]矩阵={0,0,0,0,红色
,0,0,0,0,绿色
,0,0,0,0,蓝色
, 0, 0, 0, 1, 0};
ColorFilter ColorFilter=新的ColorMatrixColorFilter(矩阵);
clip.setColorFilter(colorFilter);
}
});
返回视图持有者;
}
//替换视图的内容(由布局管理器调用)
@凌驾
public void onBindViewHolder(最终ViewHolder ViewHolder,int位置){
//-从该位置的itemsData获取数据
//-将视图的内容替换为该项数据
//myTypeface=Typeface.createFromAsset(context.getAssets(),“fonts/robotocensatedBoldItalic.ttf”);
试一试{
JSONObject obj=jsonArray.getJSONObject(位置);
title=obj.getString(“title”);
image=obj.getString(“品牌标识”);
subtitle=obj.getString(“sub_title”);
}捕获(JSONException e){
e、 printStackTrace();
}
viewHolder.txtViewTitle.setText(副标题);
viewHolder.subtitle.setText(标题);
如果(imageLoader==null)
imageLoader=AppController.getInstance().getImageLoader();
字符串完整\u Url=”http://mycompany/assets/new“+图像;
viewHolder.thumbnail.setImageUrl(完整Url,imageLoader);
viewHolder.btn_get_deal.setTag(位置);
viewHolder.btn\u get\u deal.setOnClickListener(new View.OnClickListener()){
公共JSONObject对象;
public ArrayList offerlist=新的ArrayList();
@凌驾
公共void onClick(视图){
Intent offerpage=new Intent(AppController.getInstance().getApplicationContext(),OfferDetails.class);
整数pos=(整数)view.getTag();
试一试{
obj=jsonArray.getJSONObject(pos);
offerpage.putExtra(“jsonObj”,obj.toString());
}捕获(JSONException e){
e、 printStackTrace();
}
//offerpage.getParcelableArrayListExtra(offerlist);
设置标志(意图、标志、活动、新任务);
AppController.getInstance().getApplicationContext().startActivity(offerpage);
}
});
//viewHolder.txtViewTitle.setTypeface(myTypeface);
}
//用于保存对RecyclerView的每个项的引用的内部类
公共静态类ViewHolder扩展了RecyclerView.ViewHolder{
专用最终网络图像视图缩略图;
私有最终相对论框架;
私有最终相对层;
公共文本视图txtViewTitle;
公共文本视图字幕;
公众影像短片;
公共CardView btn\u get\u交易;
公共视图持有者(视图项布局视图){
超级(itemLayoutView);
txtViewTitle=(TextView)itemLayoutView.findViewById(R.id.txttitle_topTown);
subtitle=(TextView)itemLayoutView.findViewById(R.id.sub\u title\u top20);
缩略图=(NetworkImageView)itemLayoutView.findViewById(R.id.thumbnail\u topwenty);
frame=(RelativeLayout)itemLayoutView.findViewById(R.id.frame);
layer=(RelativeLayout)itemLayoutView.findViewById(R.id.layer);
btn_get_deal=(cardwiew)itemLayoutView.findviewbyd(R.id.card_视图);
}
}
//返回itemsData的大小(由布局管理器调用)
@凌驾
public int getItemCount(){
返回jsonArray.length();
}
}我遇到了同样的问题,当我在listView行中使用radioButton时,我解决了它。在每次滚动时,缓存都会被清除,因此必须在每一行中存储更改的数据。检查一下我的密码
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
// if(position==0)
// Toast.makeText(context, ""+QuestionViewPageAdapter.mRadioGroupData, Toast.LENGTH_SHORT).show();
final ViewHolder viewHolder;
View row=convertView;
radioButton=new RadioButton[Integer.parseInt(mNoOfCategoryGreading)];
LayoutInflater inflater =LayoutInflater.from(context);//(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.question_row, parent, false);
viewHolder=new ViewHolder();
viewHolder.tv_qContent=(TextView)row.findViewById(R.id.tv_question);
viewHolder.rg=(RadioGroup)row.findViewById(R.id.rgrp);
for(int i=0;i<Integer.parseInt(mNoOfCategoryGreading);i++)
{
radioButton[i]=new RadioButton(context);
radioButton[i].setLayoutParams(new RadioGroup.LayoutParams(0, LayoutParams.WRAP_CONTENT,1f));
//1f for layout_weight="1"
radioButton[i].setId(i);
radioButton[i].setText(""+(i+1));
viewHolder.rg.addView(radioButton[i]);
}
row.setTag(viewHolder);
for(int i=0;i<QuestionViewPageAdapter.mRadioGroupData.size();i++){
int p=Integer.parseInt(""+QuestionViewPageAdapter.mRadioGroupData.get(i).get("position"));
String id=QuestionViewPageAdapter.mRadioGroupData.get(i).get("Id");
if(p!=-1 && id.equals(""+data.get(position).get(AppData.question_Set_category_QuestionID)))
{//radioButton[p].setSelected(true);
viewHolder.rg.check(p-1);}
//Toast.makeText(context, "Yes "+p, Toast.LENGTH_LONG).show();}
}
viewHolder.tv_qContent.setText((position+1)+". "+data.get(position).get(AppData.question_Set_category_QuestionContent));
viewHolder.tv_qContent.setTypeface(typeface_muso_regular300);
viewHolder.rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
String s=data.get(position).get(AppData.question_Set_category_QuestionID);
isAvailabel(s);
int radioButtonID = viewHolder.rg.getCheckedRadioButtonId();
View radioButton = viewHolder.rg.findViewById(radioButtonID);
int idx = viewHolder.rg.indexOfChild(radioButton);
HashMap<String, String> map=new HashMap<String, String>();
map.put("position", ""+(idx+1));
map.put("Id", ""+data.get(position).get(AppData.question_Set_category_QuestionID));
map.put("CategoryId", ""+mCategoryId);
QuestionViewPageAdapter.mRadioGroupData.add(map);
}
});
return row;
}
private void isAvailabel(String qId){
for(int i=0;i<QuestionViewPageAdapter.mRadioGroupData.size();i++){
if(qId.equals(QuestionViewPageAdapter.mRadioGroupData.get(i).get("Id"))){
position=i;
QuestionViewPageAdapter.mRadioGroupData.remove(i);
}
}
}
class ViewHolder {
TextView tv_qContent ,tv_1,tv_2,tv_3;
RadioGroup rg;
RadioButton rBtn1,rBtn2,rBtn3,rBtn4,rBtn5;
}
@覆盖
公共视图getView(最终整数位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
//如果(位置==0)
//Toast.makeText(context,“+QuestionViewPageAdapter.mRadioGroupData,Toast.LENGTH_SHORT.show();
最终持票人;
视图行=转换视图;
radioButton=新RadioBut
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/background"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
public class ExampleModel {
private final int mColor;
private final String mText;
public ExampleModel(int color, String text) {
mColor = color;
mText = text;
}
public int getColor() {
return mColor;
}
public String getText() {
return mText;
}
}
public class ExampleViewHolder extends RecyclerView.ViewHolder {
private final FrameLayout mBackground;
private final TextView mTextView;
public ExampleViewHolder(View itemView) {
super(itemView);
mBackground = (FrameLayout) itemView.findViewById(R.id.background);
mTextView = (TextView) itemView.findViewById(R.id.textView);
}
public void bind(ExampleModel model) {
mBackground.setBackgroundColor(model.getColor());
mTextView.setText(model.getText());
}
}
public class ExampleAdapter extends RecyclerView.Adapter<ExampleViewHolder> {
private final LayoutInflater mInflater;
private final List<ExampleModel> mModels;
public ExampleAdapter(Context context, List<ExampleModel> models) {
mInflater = LayoutInflater.from(context);
mModels = models;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View itemView = mInflater.inflate(R.layout.item_example, parent, false);
return new ExampleViewHolder(itemView);
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
final ExampleModel model = mModels.get(position);
holder.bind(model);
}
@Override
public int getItemCount() {
return mModels.size();
}
}
final Random mRandom = new Random(System.currentTimeMillis());
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
final List<ExampleModel> models = new ArrayList<>();
for (int i = 0; i < 100; i++) {
final int randomColor = generateRandomPastelColor();
models.add(new ExampleModel(randomColor, String.valueOf(i)));
}
final ExampleAdapter adapter = new ExampleAdapter(getActivity(), models);
recyclerView.setAdapter(adapter);
}
public int generateRandomPastelColor() {
final int baseColor = Color.WHITE;
final int red = (Color.red(baseColor) + mRandom.nextInt(256)) / 2;
final int green = (Color.green(baseColor) + mRandom.nextInt(256)) / 2;
final int blue = (Color.blue(baseColor) + mRandom.nextInt(256)) / 2;
return Color.rgb(red, green, blue);
}
@Override
public long getItemId(int position) {
Object listItem = feeds.get(position);
return listItem.hashCode();
}
adapter = new FeedAdapter(feeds, getContext());
adapter.setHasStableIds(true);