我建立了一个类似于图像的应用程序我想要点击播放器boutton只有medi女巫附加到seekbar开始玩像whatsapp信使。它的工作完美但我的listView在其他列表项上滚动了seekBar进度。我不知道为什么!我从5天开始尝试一切,但我尝试的一切都不起作用。你能帮帮我吗?谢谢大家的帮助。
如有必要,我会发布我的源代码。
我的自定义视图代码如下:
public class VoiceItemView extends MessageBaseItemView<VoiceItemModel>
{
View view;
private SeekBar audioSeekbar;
private ImageButton audioControls;
private TextView link;
private VoiceRecord mVoiceRecord;
private MediaPlayer mPlayer = null;
private TalkActivity talkActivity = new TalkActivity();
private Handler mHandler;
private OnAudioFocusChangeListener focusChangeListener =
new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) :
mPlayer.setVolume(0.2f, 0.2f);
break;
case (AudioManager.AUDIOFOCUS_LOSS_TRANSIENT) :
mPlayer.pause();
break;
case (AudioManager.AUDIOFOCUS_LOSS) :
mPlayer.pause();
break;
case (AudioManager.AUDIOFOCUS_GAIN) :
mPlayer.start();
break;
default: break;
}
}
};
public VoiceItemView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
init();
}
public VoiceItemView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public VoiceItemView(Context context) {
super(context);
}
@Override
public void bind(VoiceItemModel item){
super.bind(item);
audioControls.setImageResource(R.drawable.ic_play_icon);
link.setText(item.getFilePath());
}
@Override
@SuppressLint("InflateParams")
public void init(){
super.init();
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (View) inflater.inflate(R.layout.talk_voice, null);
link = new TextView(getContext());
link.setVisibility(GONE);
audioSeekbar = new SeekBar(getContext());
audioControls = new ImageButton(getContext());
android.view.ViewGroup.LayoutParams params = new LayoutParams(370, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
audioSeekbar.setProgressDrawable(CustomSeekBar.setProgress(12, 8, getResources().getColor(R.color.blue_soft), Color.WHITE));
audioSeekbar.setThumb(CustomSeekBar.setThumb(getResources().getColor(R.color.blue_soft), 40));
audioSeekbar.setLayoutParams(params);
mVoiceRecord = new VoiceRecord();
mVoiceRecord.setContext(getContext());
audioControls.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AudioManager am = (AudioManager)getContext().getSystemService(Context.AUDIO_SERVICE);
final int result = am.requestAudioFocus(focusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
if(mPlayer == null){
mPlayer = new MediaPlayer();
mHandler = new Handler();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mPlayer.setDataSource(link.getText().toString());
mPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mPlayer.start();
mPlayer.seekTo(audioSeekbar.getProgress());
}
final int duration = mPlayer.getDuration();
audioSeekbar.setMax(duration);
final int amoungToupdate = duration/100;
TimerTask task = new TimerTask() {
@Override
public void run() {
talkActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
if(mPlayer != null){
int mCurrentPosition = mPlayer.getCurrentPosition();
audioSeekbar.setProgress(mCurrentPosition);
}
mHandler.postDelayed(this, 1000);
}
});
}
};
new Timer().schedule(task, amoungToupdate);
}
}
});
setViewToContainer(audioSeekbar);
setViewToContainer(audioControls);
}
}
和bellow源是由父类MessageBaseItemView
abstract public class MessageBaseItemView<T> extends LinearLayout implements ICustomModel<T>{
private TextView timeStamp;
private ImageView avatar;
private RelativeLayout container;
private View viewRight, viewLeft;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final float scale = getResources().getDisplayMetrics().density;
public MessageBaseItemView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
init();
}
public MessageBaseItemView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MessageBaseItemView(Context context) {
super(context);
init();
}
public void setViewToContainer(List<View> listView){
for (View view : listView) {
container.addView(view);
}
}
public void setViewToContainer(View view){
container.addView(view);
}
public RelativeLayout getContainer(){
return this.container;
}
@Override
public void init(){
inflate(getContext(), R.layout.custom_talk_list_bubble_left, this);
viewRight = inflater.inflate(R.layout.custom_talk_list_bubble_right, null);
viewLeft = inflater.inflate(R.layout.custom_talk_list_bubble_left, null);
timeStamp = (TextView) findViewById(R.id.timeStamp);
avatar = (ImageView) findViewById(R.id.avatar);
container = (RelativeLayout) findViewById(R.id.container);
}
}
我在适配器中使用我的VoiceItemView,我在listFragment中使用此适配器,如bellow
public class ProfileFragment extends ListFragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundleSavedInstance){
view = inflater.inflate(R.layout.fragment_profile_profile, viewGroup, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DynamicMessageAdapter adapter = new DynamicMessageAdapter(getActivity());
List<VoiceItemModel> listVoice = new ArrayList<VoiceItemModel>();
listVoice.add(new VoiceItemModel(2, "18:00", "/storage/emulated/0/Flirt/Media/Talk/Voices Mates/2015-05-27/case.aac"));
listVoice.add(new VoiceItemModel(2, "12:09 PM", "/storage/emulated/0/Flirt/Media/Talk/Voices Mates/2015-05-27/destin.aac"));
listVoice.add(new VoiceItemModel(4, "18:00", "/storage/emulated/0/Flirt/Media/Talk/Voices Mates/2015-05-27/boo.aac"));
adapter.bindVoice(listVoice);
setListAdapter(adapter);
}
}