我想知道如何更新視圖/ UI(比方說,做一個TextView無形的)一個ListView的單列內該行點擊因爲每隔時行'的意見也會受到影響。一個很好的例子是打在谷歌Play音樂歌曲,並且均衡動畫將相應地顯示出來。這裏是我的代碼片段:點擊時更新一個ListView的一行中的觀點
MainActivity的onCreate():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Renders only a ListView
ListView songListView = (ListView) findViewById(R.id.list);
List<Song> songList = new ArrayList<>();
for (int i = 1; i <= 200; i++) songList.add(new Song("Song" + i, "Artist" + i));
SongAdapter songAdapter = new SongAdapter(this, songList);
songListView.setAdapter(songAdapter);
songListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long l) {
if (position == view.getId()) {
TextView titleText = (TextView) view.findViewById(R.id.song_title);
titleText.setVisibility(View.INVISIBLE);
}
}
});
}
...和我ArrayAdapter的getView方法和ViewHolder類:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent,
false);
holder = new ViewHolder();
holder.song = (TextView) convertView.findViewById(R.id.song_title);
holder.artist = (TextView) convertView.findViewById(R.id.song_artist);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.setId(position);
Song currentSong = getItem(position);
if (currentSong != null) {
holder.song.setText(currentSong.getTitle());
holder.artist.setText(currentSong.getArtist());
}
return convertView;
}
private static class ViewHolder {
TextView song;
TextView artist;
}
謝謝,但其它視圖仍將消失。 – DaveNOTDavid