[英]Change specific row item layout in listView
我創建了一個listView,我可以將圖像和文本放在listView中,並動態地將行添加到listView中。
但是我不明白如何實現我可以更改listView中特定行項目的布局?
例如,在活動開始時,我將用5行初始化listView,並且當觸發發生時(幾分鍾后),我想更改例如第四個listView項(僅更改第四個條目的布局,所有其他條目保留原始布局)
非常感謝
private ListView lv;
private List<String> text = new ArrayList<String>();
private List<Bitmap> listImages;
private String freindsId;
ArrayList<String> text_sort = new ArrayList<String>();
ArrayList<Bitmap> image_sort = new ArrayList<Bitmap>();
private MyCustomAdapter adapter;
adapter =new MyCustomAdapter(text,listImages);
lv.setAdapter(adapter);
class MyCustomAdapter extends BaseAdapter{
public List<String> text_array = new ArrayList<String>();
public List<Bitmap> image_array = new ArrayList<Bitmap>();
public int getCount(){
return text_array.size();
}
MyCustomAdapter(List<String> text, List<Bitmap> image)
{
text_array = text;
image_array = image;
}
public long getItemId(int position){
return position;
}
public String getItem(int position){
return null;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflate = getLayoutInflater();
View v = inflate.inflate(R.layout.listview, parent, false);
final ImageView image = (ImageView) v.findViewById(R.id.ImageView01);
TextView txtUnderImage =(TextView) v.findViewById(R.id.TextView01);
if(listImages.get(position) != null) {
Bitmap bitmap = image_array.get(position);
image.setImageBitmap(bitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
final byte[] byteArray = stream.toByteArray();
image.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Switching to Register screen
Intent i = new Intent(getApplicationContext(), itemSaleActivity.class);
i.putExtra("picture", byteArray);
startActivity(i);
}
});
txtUnderImage.setText(text_array.get(position));
image.setVisibility(View.VISIBLE);
} else {
image.setVisibility(View.GONE);
image.setImageBitmap(null);
image.setVisibility(View.VISIBLE);
}
return v;
}
public void addObject(String text, Bitmap bitmap) {
text_array.add(text);
image_array.add(bitmap);
notifyDataSetChanged();
}
public void addFirst(String text, Bitmap bitmap) {
image_array.add(0,bitmap);
text_array.add(0,text);
notifyDataSetChanged();
}
public void removeObject(String text,Bitmap bitmap) {
int indexToRemove = image_array.indexOf(bitmap);
image_array.remove(indexToRemove);
text_array.remove(indexToRemove);
notifyDataSetChanged();
}
public void deleteAll() {
image_array.clear();
text_array.clear();
notifyDataSetChanged();
}
}
嘗試像這樣獲取特定位置的視圖
getChildAt(position)您將獲得自己的視圖,
並更改行的布局,然后調用notifyDataSetChanged()。
如果您想突出顯示所選行,請使用Android中OnClickListener的Inflate ListView行?
當事件觸發更改適配器的數據並在Adapater對象上調用notifyDataSetChanged()時。
使用getChildAt()
方法可以訪問要更改的任何行。 例如listView.getChildAt(4)
對第四行項目進行更改。 在適配器上調用notifyDataSetChanged()。
notifyDataSetChanged()通知附加的觀察者基礎數據已更改,任何反映該數據集的View均應刷新自身。
notifyDataSetChanged()刷新您的列表視圖。
這里描述的方式將遍歷整個列表,雖然您只需要更新可見的視圖,但是其余的更改將在“重新繪制”時會得到注意。
因此,請查看THIS LINK上提供的答案,它提供了一個解決方案,您只需識別視圖的位置即可,並且只有當該視圖當時可見時,您才可以更新視圖。
引用該答案-
int iFirst = getFirstVisiblePosition();
int iLast = getLastVisiblePosition();
if ( indexToChange >= numberOfRowsInSection() ) {
Log.i( "MyApp", "Invalid index. Row Count = " + numberOfRowsInSection() );
}
else {
if ( ( index >= iFirst ) && ( index <= iLast ) ) {
// get the view at the position being updated - need to adjust index based on first in the list
View vw = getChildAt( sysvar_index - iFirst );
if ( null != vw ) {
// get the text view for the view
TextView tv = (TextView) vw.findViewById(com.android.myapp.R.id.scrollingListRowTextView );
if ( tv != null ) {
// update the text, invalidation seems to be automatic
tv.setText( "Item = " + myAppGetItem( index ) + ". Index = " + index + ". First = " + iFirst + ". Last = " + iLast );
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.