我有一个 ListView,其中包含使用特定布局创建的行。每行都有默认为 View.GONE 的 ProgressBar。单击此元素时,我需要使 ListView 项中的 ProgressBar 可见。问题是,当我单击元素时,ProgressBars 不仅在我单击的行中变得可见,而且在其他随机行中也可见。这是代码:
创建 ListView 并设置 OnItemClickListener:
private void buildTracklist(ArrayList<Audio> differences, ArrayList<Audio> vkAudioList) {
ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(vkAudioList.size());
Map<String, Object> m;
int i;
for (i=0;i<vkAudioList.size();i++) {
m = new HashMap<String, Object>();
m.put(AUDIO_ARTIST, vkAudioList.get(i).artist);
m.put(AUDIO_TITLE, vkAudioList.get(i).title);
if (arrayListContainsAudio(differences, vkAudioList.get(i))) {
m.put(AUDIO_DOWNLOADED, R.drawable.image_blank);
} else {
m.put(AUDIO_DOWNLOADED, R.drawable.image_downloaded);
}
data.add(m);
}
String[] from = {AUDIO_TITLE,AUDIO_ARTIST,AUDIO_DOWNLOADED};
int[] to = {R.id.audioTitleTextView,R.id.audioArtistTextView,R.id.isDownloadedImageView};
sAdapter = new SimpleAdapter(this, data, R.layout.tracklist_item, from, to);
audioListView = (ListView) findViewById(R.id.audioListView);
audioListView.setAdapter(sAdapter);
audioListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG,"position="+position+" id="+id);
view.findViewById(R.id.audioItemProgressBar).setVisibility(View.VISIBLE);
//Here I'm passing progressBar further
}
});
}
tracklistItem 布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/audioTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:gravity="top|left"
android:text="Title"
android:textSize="24dp" />
<TextView
android:id="@+id/audioArtistTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/audioTitleTextView"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"
android:text="Artist"
android:textSize="16sp" />
<ProgressBar
android:id="@+id/audioItemProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/audioArtistTextView"
android:visibility="gone" />
</RelativeLayout>
<ImageView
android:id="@+id/isDownloadedImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="@drawable/downloaded_backgroundv10"
android:visibility="visible" />
</FrameLayout>
我尝试使用 getView() 的 getChildAt() 获取列表视图项目视图,但发生了同样的问题。感谢您的帮助,并为我的英语不好感到抱歉。
更新:当我滚动 ListView 时,我发现我正在使用的 progressBar 更改了父项,但无论 ListView 的哪个部分当前在屏幕上,我都需要它在一行中。