我有一个自定义列表视图,其中每个列表项都有两个按钮,一个文本视图和一个搜索栏,其中搜索栏可见性设置为列表项的XML中。
现在我要做的是单击列表项中的按钮1我要隐藏文本视图并显示单击该按钮的列表项的搜索栏并显示隐藏搜索栏并显示文本视图所有其他列表项目。
如果总项目数少于可以在列表视图中一次显示的视图,那么我编写的代码效果很好但是对于很多视图而言这不起作用。
我正在做的是将文件夹中的文件列表加载到列表视图中,并仅在列表视图中播放
请告诉我哪里出错了。 任何帮助将不胜感激。
我的适配器代码:
public class AudioFileListAdapter extends BaseAdapter {
Context context;
ArrayList<String> fileList;
LayoutInflater inflater;
String folder;
ListView mListView;
public MediaPlayer mPlayer;
public Handler seekHandler;
SeekBar mSeekBar;
int clickedPos = -1;
protected static class RowViewHolder {
public TextView fileName;
public Button filePlay;
public Button rate;
public SeekBar fileSeek;
}
public AudioFileListAdapter(Context context, ArrayList<String> fileList,
String folder) {
this.fileList = fileList;
this.context = context;
this.folder = folder;
mListView = (ListView) ((Activity) context)
.findViewById(R.id.Filelistview);
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
seekHandler = new Handler();
}
@Override
public int getCount() {
return fileList.size();
}
@Override
public Object getItem(int position) {
return fileList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RowViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.audio_file_list_item, null);
viewHolder = new RowViewHolder();
viewHolder.fileName = (TextView) convertView
.findViewById(R.id.file_name);
viewHolder.filePlay = (Button) convertView
.findViewById(R.id.file_play);
viewHolder.filePlay.setTag(position);
viewHolder.rate = (Button) convertView.findViewById(R.id.rate);
viewHolder.fileSeek = (SeekBar) convertView
.findViewById(R.id.file_seek);
viewHolder.filePlay.setOnClickListener(mListener);
viewHolder.rate.setOnClickListener(mListener);
convertView.setTag(viewHolder);
} else {
viewHolder = (RowViewHolder) convertView.getTag();
}
viewHolder.fileName.setText(fileList.get(position));
return convertView;
}
OnClickListener mListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.file_play)
{
final int position = mListView.getPositionForView((View) v
.getParent());
Log.d("position"+position, "count"+mListView.getCount());
for (int i = 0 ; i < mListView.getChildCount(); i++) {
RowViewHolder viewholder = (RowViewHolder) ((View) mListView
.getChildAt(i)).getTag();
if (i == position) {
String path = Environment.getExternalStorageDirectory()
.toString()
+ "/analyser/"
+ folder
+ "/"
+ fileList.get(i);
mSeekBar = viewholder.fileSeek;
Log.d("IN the click "+i,path);
// mSeekBar.setEnabled(false);
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(mPlayer != null && fromUser){
mPlayer.seekTo(progress);
}
}
});
viewholder.fileSeek.setVisibility(View.VISIBLE);
viewholder.fileName.setVisibility(View.GONE);
play(path);
} else {
viewholder.fileSeek.setVisibility(View.GONE);
viewholder.fileName.setVisibility(View.VISIBLE);
}
}
}
if (v.getId() == R.id.rate) {
}
}
};
public void play(String path) {
if (mPlayer == null) {
mPlayer = new MediaPlayer();
mPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mSeekBar.setMax(mPlayer.getDuration());
mp.start();
seekUpdation();
}
});
mPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mPlayer.release();
mPlayer = null;
seekHandler.removeCallbacks(run);
}
});
}
if (mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset();
seekHandler.removeCallbacks(run);
}
try {
mPlayer.setDataSource(path);
mPlayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Runnable run = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (mPlayer != null) {
seekUpdation();
}
}
};
public void seekUpdation() {
if (mPlayer != null) {
mSeekBar.setProgress(mPlayer.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
}
}
答案 0 :(得分:0)
您必须在Adapter类getView方法中设置按钮的侦听器。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RowViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.audio_file_list_item, null);
viewHolder = new RowViewHolder();
viewHolder.fileName = (TextView) convertView.findViewById(R.id.file_name);
viewHolder.filePlay = (Button) convertView.findViewById(R.id.file_play);
viewHolder.filePlay.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view){
// Do Your Work Here
}
});
viewHolder.filePlay.setTag(position);
viewHolder.rate = (Button) convertView.findViewById(R.id.rate);
viewHolder.fileSeek = (SeekBar) convertView.findViewById(R.id.file_seek);
viewHolder.filePlay.setOnClickListener(mListener);
viewHolder.rate.setOnClickListener(mListener);
convertView.setTag(viewHolder);
} else {
viewHolder = (RowViewHolder) convertView.getTag();
}
viewHolder.fileName.setText(fileList.get(position));
return convertView;
}