[英]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.