这是一个使用baseAdapter获取数据的listview,我想点击播放按钮隐藏它并显示暂停按钮而不是它反之亦然,我将onClickListener添加到方法getView中baseAdapter中的播放按钮()并尝试隐藏它并显示暂停按钮但没有任何效果...... !! ??结果总是隐藏并显示列表视图的下一行中的按钮,有时不会......?我在baseAdapter和Activity中的clickListener中添加了方法notifyDataSetChanged()但是也没有工作..... !!? PLZ PLZ PLZ帮帮我,我已经坚持了2天。 这是我的Activity类和Adapter类的代码。
public class AudioBookAdapter extends BaseAdapter {
private Context context;
private BookIndex bookIndex;
private ViewHolder holder;
private int selectedPosition = -1;
private MediaPlayer mediaFileTrack = new MediaPlayer();
public AudioBookAdapter(Context context, BookIndex bookIndex) {
this.context = context;
this.bookIndex = bookIndex;
}
/**
* @return the audioFileTrack
*/
public MediaPlayer getMediaFileTrack() {
return mediaFileTrack;
}
/**
* @param audioFileTrack the audioFileTrack to set
*/
public void setMediaFileTrack(MediaPlayer mediaFileTrack) {
this.mediaFileTrack = mediaFileTrack;
}
/**
* @return the context
*/
public Context getContext() {
return context;
}
/**
* @param context the context to set
*/
public void setContext(Context context) {
this.context = context;
}
/**
* @return the bookIndex
*/
public BookIndex getBookIndex() {
return bookIndex;
}
/**
* @param bookIndex the bookIndex to set
*/
public void setBookIndex(BookIndex bookIndex) {
this.bookIndex = bookIndex;
}
@Override
public int getCount() {
return getBookIndex().getBookIndexes().size();
}
@Override
public String getItem(int position) {
return getBookIndex().getBookIndexes().get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
holder = null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.audio_track_row, parent, false);
holder = new ViewHolder();
holder.txtAudioFileTitle = (TextView) convertView.findViewById(R.id.txtAudioFileTitle);
holder.imgSelectedAudioFile = (ImageView) convertView.findViewById(R.id.imgSelected);
holder.imgUnselectedAudioFile = (ImageView) convertView.findViewById(R.id.imgUnselected);
holder.flUnselectedTrack = (FrameLayout) convertView.findViewById(R.id.flUnselectedTrack);
holder.flSelectedTrack = (FrameLayout) convertView.findViewById(R.id.flSelectedTrack);
holder.audioSeekBar = (SeekBar) convertView.findViewById(R.id.audioSeekBar);
holder.btnPause = (ImageButton) convertView.findViewById(R.id.btnPausePaly);
holder.btnNext = (ImageButton) convertView.findViewById(R.id.btnNext);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtAudioFileTitle.setText(getBookIndex().getBookIndexes().get(position));
if (getSelectedPosition() == position) {
Log.i("JML...Info", "Position in Adapter = " + position);
holder.flSelectedTrack.setVisibility(View.VISIBLE);
holder.flUnselectedTrack.setVisibility(View.GONE);
holder.btnPause.setSelected(true);
holder.btnPause.setFocusable(true);
holder.btnPause.setFocusableInTouchMode(true);
} else {
Log.i("JML...Info", "Else Position in Adapter = " + position);
holder.flUnselectedTrack.setVisibility(View.VISIBLE);
holder.flSelectedTrack.setVisibility(View.GONE);
}
holder.btnPause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (getSelectedPosition() == position) {
// getMediaFileTrack().pause();
holder.btnPause.setImageDrawable(getContext().getResources().getDrawable(R.drawable.btn_play));
Log.i("JML...Info", "Pause + Position = " + getSelectedPosition());
notifyDataSetChanged();
}
}
});
return convertView;
}
/**
*
* @param holder
*/
protected void playTrack(ViewHolder holder) {
holder.flUnselectedTrack.setVisibility(View.GONE);
}
/**
*
* @author Jamil
*
*/
private static class ViewHolder {
TextView txtAudioFileTitle;
ImageView imgSelectedAudioFile;
ImageView imgUnselectedAudioFile;
FrameLayout flUnselectedTrack;
FrameLayout flSelectedTrack;
SeekBar audioSeekBar;
ImageButton btnPause;
ImageButton btnNext;
}
/**
*
* @return
*/
public int getSelectedPosition() {
return selectedPosition;
}
/**
*
* @param position
*/
public void setSelectedPosition(int position) {
selectedPosition = position;
notifyDataSetChanged();
}
活动类
/**
* * /
public class DisplayAudioBook extends CustomeActivity {
private Bundle bundle;
private ProgressDialog loadingDialog;
private ListView listView = null;
private MaktabatiHelper maktabatiHelper;
private MediaPlayer mediaFileTrack;
private SeekBar audioSeekBar;
private Handler handlerSeekBar = new Handler();
private AudioBookAdapter audioBookAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_audio_book);
bundle = getIntent().getExtras();
listView = (ListView) findViewById(R.id.lstAudioBookFiles);
if (!isBookFilesUnzip(bundle.getString("BOOK_ID"))) {
new UnzipAudioFileTask().execute(bundle.getString("BOOK_ID"));
}
audioBookAdapter = new AudioBookAdapter(getApplicationContext(), getBookIndex(bundle.getString("BOOK_ID")));
listView.setAdapter(audioBookAdapter);
listView.setDivider(null);
listView.setDividerHeight(0);
audioBookAdapter.notifyDataSetChanged();
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
audioBookAdapter.setSelectedPosition(position);
View oldView = adapter.getChildAt(audioBookAdapter.getSelectedPosition());
FrameLayout rowOfList;
if (oldView != null && audioBookAdapter.getSelectedPosition() != -1) {
rowOfList = (FrameLayout) oldView.findViewById(R.id.flUnselectedTrack);
rowOfList.setVisibility(View.GONE);
}
rowOfList = (FrameLayout) view.findViewById(R.id.flSelectedTrack);
rowOfList.setVisibility(View.VISIBLE);
listView.setAnimationCacheEnabled(false);
listView.setScrollingCacheEnabled(false);
}
});
audioBookAdapter.notifyDataSetChanged();
}
/**
*
* @param bookId
*/
private boolean isBookFilesUnzip(String bookId) {
boolean isExits = false;
File file = new File(Constants.AUDIO_EBOOKS_UNZIP_FILES_DIRECTORY + "/" + bookId);
if (file.isDirectory()) {
maktabatiHelper = new MaktabatiHelper(getApplicationContext());
int indexCount = maktabatiHelper.lstBookIndex(bundle.getString("BOOK_ID"));
File[] listFiles = file.listFiles();
if (indexCount != listFiles.length - 1) {
Toast.makeText(DisplayAudioBook.this.getApplicationContext(), "Misssssssssing audio file", Toast.LENGTH_SHORT).show();
return false;
}
isExits = true;
} else {
new UnzipAudioFileTask().execute(bundle.getString("BOOK_ID"));
isExits = true;
}
return isExits;
}
/**
*
*/
private BookIndex getBookIndex(String BookId) {
maktabatiHelper = new MaktabatiHelper(getApplicationContext());
BookIndex bookIndex = maktabatiHelper.getBookIndex(bundle.getString("BOOK_ID"));
return bookIndex;
}
/**
*
* @author Jamil
*
*/
private class UnzipAudioFileTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
loadingDialog = new RightJustifyDialog(DisplayAudioBook.this);
loadingDialog.setTitle(getString(R.string.loading));
loadingDialog.setMessage(getString(R.string.waiting));
loadingDialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
Utils.unzipFile(Constants.AUDIO_EBOOKS_UNZIP_FILES_DIRECTORY, params[0] + ".zip");
} catch (IOException e) {
e.printStackTrace();
}
return params[0];
}
@Override
protected void onPostExecute(String result) {
AudioBookAdapter audioBookAdapter = new AudioBookAdapter(getApplicationContext(), getBookIndex(result));
listView.setDivider(null);
listView.setDividerHeight(0);
listView.setAdapter(audioBookAdapter);
loadingDialog.dismiss();
}
}
@Override
protected void onStop() {
super.onStop();
// mediaFileTrack.stop();
}
}
答案 0 :(得分:0)
Android会重复使用您的观点,尤其是ViewHolder。我在没有ViewHolder的情况下成功实现了类似的列表。您应该尝试在没有ViewHolder的情况下创建列表,看看它是否有效。