1

这是一个使用 baseAdapter 获取数据的列表视图,我想单击播放按钮来隐藏它并显示暂停按钮而不是它,反之亦然,我在方法 getView() 中将 onClickListener 添加到 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();
    }
}
4

1 回答 1

0

Android 会重用您的视图,尤其是使用 ViewHolder。在没有 ViewHolder 的情况下,我成功地实现了一个与您类似的列表。您应该尝试在没有 ViewHolder 的情况下制作列表,看看它是否有效。

于 2012-12-19T11:48:11.117 回答