当我在android中的listview中向下和向上滚动时,列表中每个单元格的背景颜色不断变化

时间:2018-03-26 18:21:09

标签: java android listview scroll

当我向下滚动列表视图中单元格的背景颜色更改时,我有包含图像和文本的列表视图。当我向上滚动背景颜色时,也会再次改变。我的适配器中是否有任何遗漏或我需要更改

这是我的适配器

 public class MainActivityListAdapater extends BaseAdapter {


private LayoutInflater mInflater;


private List<MainActivityData> maindatalist;

public MainActivityListAdapater(Context activity, List<MainActivityData> maindatalist) {
    this.maindatalist = maindatalist;
    mInflater = LayoutInflater.from(activity);
}


@Override
public void notifyDataSetChanged() {


    super.notifyDataSetChanged();
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return maindatalist.size();
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return maindatalist.get(arg0);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
  ViewHolder holder;
 //        if (mInflater == null)
 //            mInflater = (LayoutInflater) 
 activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.offer_card, null,true);
        holder = new ViewHolder();
        holder.name = (TextView) convertView.findViewById(R.id.title);
        holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
        convertView.setTag(holder);


    }else {
        holder = (ViewHolder) convertView.getTag();
        holder.name.setText(maindatalist.get(position).getName());
        holder.thumbnail.setImageResource(maindatalist.get(position).getStoreCover());
    }


    holder.name.setText(maindatalist.get(position).getName());
    holder.thumbnail.setImageResource(maindatalist.get(position).getStoreCover());
    //97697691


    return convertView;
}
static class ViewHolder {
    TextView name;
    ImageView thumbnail;
}
 }

这是我的活动

 lv = (ListView) findViewById(R.id.cardview);
    adapter = new MainActivityListAdapater(MainActivity.this, mainactivityDataList);
    lv.setAdapter(adapter);

    mainactivityDataList.clear();
    for (int i = 0; i < covers.length; i++) {
        MainActivityData storesData = new MainActivityData();
        storesData.setName(storename[i]);
        storesData.setNumOfOffers(numberoffers[i]);
        storesData.setStoreCover(covers[i]);
        storesData.setStoreImage(storeimage[i]);

        mainactivityDataList.add(storesData);
    }
    adapter.notifyDataSetChanged();



    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        Intent intent ;
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
           // int pos = MyRecyclerView.indexOfChild(view);
            final Intent intent;
            sharedpreferences = getSharedPreferences("Settings", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedpreferences.edit();
            intent =  new Intent(MainActivity.this, Brands.class);
            editor.putString("categoryname", mainactivityDataList.get(position).getName());
            editor.putInt("categoryimage", mainactivityDataList.get(position).getStoreCover());



            editor.apply();
            startActivity(intent);
        }
    });

这是cell_xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#808080">

<android.support.v7.widget.CardView
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_margin="@dimen/card_album_radius"
    android:elevation="3dp"
    android:background="#808080"
    card_view:cardCornerRadius="@dimen/card_album_radius"
    card_view:cardBackgroundColor="?attr/colorControlNormal">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/thumbnail"
            android:layout_width="match_parent"
            android:layout_height="@dimen/album_cover_height"
            android:background="?attr/selectableItemBackgroundBorderless"
            android:clickable="true"
            android:scaleType="fitXY" />

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/thumbnail"
            android:paddingLeft="@dimen/album_title_padding"
            android:paddingRight="@dimen/album_title_padding"
            android:paddingTop="@dimen/album_title_padding"
            android:textColor="@color/album_title"
            android:textSize="@dimen/album_title" />

        <TextView
            android:id="@+id/expire"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/title"
            android:paddingBottom="@dimen/songs_count_padding_bottom"
            android:paddingLeft="@dimen/album_title_padding"
            android:paddingRight="@dimen/album_title_padding"
            android:textColor="@color/album_title"
            android:textSize="@dimen/songs_count" />

    </RelativeLayout>

</android.support.v7.widget.CardView>

 </LinearLayout>
顺便说一句,我已将相同的代码应用于不同的项目,并且它有效 任何帮助将不胜感激

0 个答案:

没有答案