滚动 listView 时,ListView 项目的背景会混淆
ListView Items' background get mixed up when scrolling listView
我正在尝试更改标题为 "Different Item" 的列表视图中特定项目的背景。当我向下滚动列表时,所有项目的背景都是正确的。当我到达列表末尾("Different Item" 所在的位置)并向上滚动时,有一些随机项目的背景与 "Different Item" 相同。我相信这是由于我正在使用的视图持有者模式以及 android 回收每个视图以再次使用这一事实,但我不太了解这里的问题以及如何解决它。
这是我的 MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(new CustomAdapter(this));
}
}
这是我的CustomAdapter.javaclass
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class CustomAdapter extends BaseAdapter {
private Context mContext;
private List<String> mItems;
public CustomAdapter(Context context) {
mContext = context;
mItems = new ArrayList<>();
for (int i = 0; i < 20; i++) {
mItems.add("Item " + i);
}
mItems.add("Different Item");
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Object getItem(int i) {
return mItems.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View view;
ViewHolder viewHolder;
TextView textView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item_template, viewGroup, false);
} else {
view = convertView;
}
if (view.getTag() == null) {
textView = (TextView)view.findViewById(R.id.items_text_view);
viewHolder = new ViewHolder(textView);
view.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder)view.getTag();
textView = viewHolder.getTextView();
}
textView.setText(mItems.get(position));
if (mItems.get(position).equals("Different Item")) {
textView.setBackgroundColor(Color.parseColor("#DDDDDD"));
}
return view;
}
private class ViewHolder {
private TextView mTextView;
public ViewHolder(TextView textView) {
this.mTextView = textView;
}
public TextView getTextView() {
return this.mTextView;
}
}
}
这是我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:padding="16dp">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
和我的list_item_template.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/items_text_view"
android:textSize="20sp"
android:padding="20dp"
tools:text="test text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
及问题截图:
起初,当我向下滚动列表时,一切看起来都很好:
上下滚动列表视图后,视图变得混乱:
本节之后:
if (mItems.get(position).equals("Different Item")) {
textView.setBackgroundColor(Color.parseColor("#DDDDDD"));
}
你应该有一个像这样的 else:
else
textView.setBackgroundColor(Color.parseColor("#FFFFFF"));
它将解决您的问题。
你的直觉是正确的。
当您在 "Different Item" 视图上设置背景时,有时该视图会在离开屏幕时被 ListView
回收和重用,并且适配器再次需要它。
它会被其他 "Different Item" 以外的项目重复使用,如果您没有明确地将背景重置为那些 "Item [x]" 项目的默认颜色,您将 运行 进入这个背景不一致的问题。
此外,此上下文中的 else
语句对性能有 零 影响。
我正在尝试更改标题为 "Different Item" 的列表视图中特定项目的背景。当我向下滚动列表时,所有项目的背景都是正确的。当我到达列表末尾("Different Item" 所在的位置)并向上滚动时,有一些随机项目的背景与 "Different Item" 相同。我相信这是由于我正在使用的视图持有者模式以及 android 回收每个视图以再次使用这一事实,但我不太了解这里的问题以及如何解决它。 这是我的 MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView)findViewById(R.id.list_view);
listView.setAdapter(new CustomAdapter(this));
}
}
这是我的CustomAdapter.javaclass
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class CustomAdapter extends BaseAdapter {
private Context mContext;
private List<String> mItems;
public CustomAdapter(Context context) {
mContext = context;
mItems = new ArrayList<>();
for (int i = 0; i < 20; i++) {
mItems.add("Item " + i);
}
mItems.add("Different Item");
}
@Override
public int getCount() {
return mItems.size();
}
@Override
public Object getItem(int i) {
return mItems.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View view;
ViewHolder viewHolder;
TextView textView;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item_template, viewGroup, false);
} else {
view = convertView;
}
if (view.getTag() == null) {
textView = (TextView)view.findViewById(R.id.items_text_view);
viewHolder = new ViewHolder(textView);
view.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder)view.getTag();
textView = viewHolder.getTextView();
}
textView.setText(mItems.get(position));
if (mItems.get(position).equals("Different Item")) {
textView.setBackgroundColor(Color.parseColor("#DDDDDD"));
}
return view;
}
private class ViewHolder {
private TextView mTextView;
public ViewHolder(TextView textView) {
this.mTextView = textView;
}
public TextView getTextView() {
return this.mTextView;
}
}
}
这是我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:padding="16dp">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
和我的list_item_template.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/items_text_view"
android:textSize="20sp"
android:padding="20dp"
tools:text="test text"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
及问题截图: 起初,当我向下滚动列表时,一切看起来都很好:
上下滚动列表视图后,视图变得混乱:
本节之后:
if (mItems.get(position).equals("Different Item")) {
textView.setBackgroundColor(Color.parseColor("#DDDDDD"));
}
你应该有一个像这样的 else:
else
textView.setBackgroundColor(Color.parseColor("#FFFFFF"));
它将解决您的问题。
你的直觉是正确的。
当您在 "Different Item" 视图上设置背景时,有时该视图会在离开屏幕时被 ListView
回收和重用,并且适配器再次需要它。
它会被其他 "Different Item" 以外的项目重复使用,如果您没有明确地将背景重置为那些 "Item [x]" 项目的默认颜色,您将 运行 进入这个背景不一致的问题。
此外,此上下文中的 else
语句对性能有 零 影响。