![](/img/trans.png)
[英]ListView Items(TextView) is reloading everytime when Scrolling
[英]ListView items changes when scrolling
我正在填写列表视图,这是代码:
ArrayAdapter<CharSequence> listAdapter1;
ListView toc;
toc=(ListView)findViewById(R.id.listView1);
listAdapter1=ArrayAdapter.createFromResource(Glossary.this,R.array.arabicalphabet,R.layout.glossarysimplerow);
toc.setAdapter(listAdapter1);
我在xml文件中设置了一个列表选择器,当点击列表中的任何项目时,它的颜色将变为蓝色以显示它已被选中。 问题是,当我滚动列表视图时,选择将改变,例如,如果我有一个从A到Z填充的列表,如果我选择字母“B”并且我滚动了很多次,我会选择字母“M”。 ..请帮忙
是的,我在一周前解决了这个问题,原因是“滚动列表视图时,第一个显示项目的位置为0”,因此您的更改视图将影响新项目。
我的解决方案是:
1)。 在其中使用ScrollView
和LinearLayout
而不是使用ListView
2)。 将数组TextView[]
添加到LinearLayout
。
码:
布局:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">
<LinearLayout
android:id="@+id/ll_inflater"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</ScrollView>
Java的:
int prePos = -1;
int sdk = android.os.Build.VERSION.SDK_INT;
LinearLayout ll_inflater = (LinearLayout)findViewById(R.id.ll_inflater);
TextView[] tv_Subs = new TextView[array.size()];//arrary is arrayData
for(int i = 0; i < array.size(); i++){
tv_Subs[i] = new TextView(getApplication());
tv_Subs[i].setLines(3);
tv_Subs[i].setTextColor(getResources().getColor(R.color.sub_color));
tv_Subs[i].setGravity(Gravity.CENTER);
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
//set default background color
} else {
//set default background color
}
tv_Subs[i].setText(array.get(i));
ll_inflater.addView(tv_Subs[i], i, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
final int finalI = i;
tv_Subs[i].setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
if(prePos !=-1){
//change background of preposition tv_Subs[prePost] to default color
}
//change background color here
prePos = finalI;
}
});
}
在Android中,像ListView
这样的AdapterView
启用了缓存,因此当用户滚动时,屏幕外的项目不在内存中,对于滚动后要在视图上显示的项目调用getView()
。
在getView()
方法中传递的View
将被重用,并具有ListView
先前可见view
的属性。 这提高了ListView
的性能,但是必须检查所有可能的条件并重置getView()
接收的视图的所有属性。
现在你使用的通用ArrayAdapter
没有在click上改变颜色的实现,它没有任何方法来保存已经点击的视图的记录。 因此在滚动时结果是意外的。
您可以实现自己的Adapter
类,如下所示,它将解决您的问题:
public class MyAdapter extends ArrayAdapter<String> {
Context context;
List<String> lstStrings;
List<Boolean> isClicked;
public MyAdapter (Context context, int resource, List<String> lstStrings) {
super(context, resource, lstStrings);
this.context = context;
lstStrings = lstStrings;
isClicked = new ArrayList<>(Arrays.asList(new Boolean[lstStrings.length]));
Collections.fill(isClicked, new Boolean(false));
}
@Override
public int getCount() {
return lstStrings.size();
}
@Override
public String getItem(int position) {
return lstStrings.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = LayoutInflater.from( context).inflate(R.layout.list_item, null);
TextView txtTheText = (TextView) convertView.findViewById(R.id.txtTheText);
txtTheText .setText(lstStrings.get(position));
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isClicked.set(!isClicked.get(position));
}
});
if (isClicked.get(position))
convertView.setBackgroundColor(Color.argb(255, 255, 0, 0));
else
convertView.setBackgroundColor(Color.argb(255, 0, 0, 255));
return convertView;
}
}
我有一个决议。 单击一个项目时,记录其position
。 然后重写onConfigurationChanged(Configuration newConfig)
,并调用ListView.setSelection(position)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.