Android 循环视图中的Seekbar返回最后一个位置
嗯,我有一个包含音频文件的Recyclerview,音频播放工作正常,但当我点击任何项目时,seekbar没有更新!seekbar的最后一个位置项正在更新和播放。我没有找到确切的解决办法。这是我的密码 回收适配器Android 循环视图中的Seekbar返回最后一个位置,android,android-recyclerview,avaudioplayer,android-seekbar,Android,Android Recyclerview,Avaudioplayer,Android Seekbar,嗯,我有一个包含音频文件的Recyclerview,音频播放工作正常,但当我点击任何项目时,seekbar没有更新!seekbar的最后一个位置项正在更新和播放。我没有找到确切的解决办法。这是我的密码 回收适配器 public class AudiorecyclerAdapter extends RecyclerView.Adapter<AudiorecyclerAdapter.MyViewHolder> { private ProgressBar pb; priva
public class AudiorecyclerAdapter extends RecyclerView.Adapter<AudiorecyclerAdapter.MyViewHolder> {
private ProgressBar pb;
private List<GettingAudio> datalist;
MediaPlayer mp=new MediaPlayer();
private Context context;
Activity act;
private Dialog dialog;
private String BASE_URL = "http://zuman.e3.amazon/";
private String medialink,titlestring;
private int downloadedSize = 0 ;
TextView cur_val;
SeekBar seek;
private int totalSize = 0;
public AudiorecyclerAdapter(Context context,Activity act,List<GettingAudio> list){
this.datalist=list;
this.context=context;
this.act=act;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.listreff,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position)
{
holder.title.setText(datalist.get(position).getTitle());
holder.postedby.setText(datalist.get(position).getPostedby());
holder.postedon.setText(datalist.get(position).getDatetime());
holder.likes.setText(datalist.get(position).getLikes());
final String media=datalist.get(position).getMedia();
final String tit=holder.title.getText().toString();
Log.d("Harry Coooooovaaa", datalist.toString());
seek=holder.seekBar;
holder.play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
medialink=media;
titlestring=tit;
showProgress();
Toast.makeText(context,medialink,Toast.LENGTH_LONG).show();
new Thread(new Runnable() {
@Override
public void run() {
downloadFile();
}
}).start();
}
});
}
@Override
public int getItemCount() {
return datalist.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
TextView title,postedby,likes,postedon;
Button play;
SeekBar seekBar;
public MyViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.audiotitle);
title=(TextView)itemView.findViewById(R.id.audiotitle);
postedby=(TextView)itemView.findViewById(R.id.postedby);
postedon=(TextView)itemView.findViewById(R.id.date);
likes=(TextView)itemView.findViewById(R.id.likes);
play=(Button)itemView.findViewById(R.id.butplay);
seekBar=(SeekBar)itemView.findViewById(R.id.seekBar);
}
}
private void showProgress(){
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.myprogressdialog);
dialog.setTitle("Download Progress");
cur_val = (TextView) dialog.findViewById(R.id.cur_pg_tv);
cur_val.setText("Starting download...");
dialog.show();
pb = (ProgressBar)dialog.findViewById(R.id.progress_bar);
pb.setProgress(0);
pb.setProgressDrawable(context.getResources().getDrawable(R.drawable.green_progress));
}
private void downloadFile(){
try {
URL url = new URL(BASE_URL+medialink);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
//connect
urlConnection.connect();
final String folder_main = "Apna";
//set the path where we want to save the file
/* File SDCardRoot =new File(Environment.getExternalStorageDirectory(),folder_main);
if (!SDCardRoot.exists()) {
SDCardRoot.mkdirs();
}
//create a new file, to save the downloaded file
File file = new File(SDCardRoot,title);*/
File wallpaperDirectory = new File(Environment.getExternalStorageDirectory(),folder_main);
//have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
//create a File object for the output file
final String perfection=titlestring.replaceAll("\"","");
File outputFile = new File(wallpaperDirectory, perfection+".mp3");
//now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fileOutput = new FileOutputStream(outputFile);
//Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file which we are downloading
totalSize = urlConnection.getContentLength();
act.runOnUiThread(new Runnable() {
public void run() {
pb.setMax(totalSize);
}
});
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
// update the progressbar //
act.runOnUiThread(new Runnable() {
public void run() {
pb.setProgress(downloadedSize);
float per = ((float)downloadedSize/totalSize) * 100;
cur_val.setText("Downloaded " + downloadedSize + "KB / " + totalSize + "KB (" + (int)per + "%)" );
}
});
}
//close the output stream when complete //
fileOutput.close();
act.runOnUiThread(new Runnable() {
public void run() {
dialog.dismiss();
try{
mp.setDataSource(Environment.getExternalStorageDirectory().getPath()+"/Apna/"+perfection+".mp3");//Write your location here
mp.prepareAsync();
// mp.start();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mRunnable.run();
}
});
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
} catch (final MalformedURLException e) {
showError("Error : MalformedURLException " + e);
Log.d("dfdsfsd", e.toString());
e.printStackTrace();
} catch (final IOException e) {
Log.d("dfdsfsd", e.toString());
showError("Error : IOException " + e);
e.printStackTrace();
}
catch (final Exception e) {
Log.d("dfdsfsd", e.toString());
showError("Error : Please check your internet connection " + e);
}
}
private void showError(final String err){
act.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(context, err, Toast.LENGTH_LONG).show();
Log.d("dfdsfsd", err);
}
});
}
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
if(mp != null) {
//set max value
int mDuration = mp.getDuration();
seek.setMax(mDuration);
//update total time text view
//set progress to current position
int mCurrentPosition = mp.getCurrentPosition();
seek.setProgress((int) mp.getCurrentPosition());
// holder.seekBar.setProgress(mCurrentPosition);
//update current time text view
//handle drag on seekbar
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(mp != null && fromUser){
mp.seekTo(progress);
}
}
});
}
//repeat above code every second
mHandler.postDelayed(this, 10);
}
};
}
公共类AudiorecyclerAdapter扩展了RecyclerView.Adapter{
私人ProgressBar pb;
私有列表数据列表;
MediaPlayer mp=新的MediaPlayer();
私人语境;
活动法;
私人对话;
专用字符串BASE_URL=”http://zuman.e3.amazon/";
私有字符串medialink,标题串;
private int downloadedSize=0;
文本视图当前值;
SeekBar-seek;
私有整数totalSize=0;
公共音频回收器适配器(上下文、活动行为、列表){
this.datalist=list;
this.context=context;
这个动作=动作;
}
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.listreff,parent,false);
返回新的MyViewHolder(v);
}
@凌驾
公共无效onBindViewHolder(MyViewHolder,int位置)
{
holder.title.setText(datalist.get(position.getTitle());
holder.postedby.setText(datalist.get(position.getPostedby());
holder.postedon.setText(datalist.get(position.getDatetime());
holder.likes.setText(datalist.get(position.getLikes());
最后一个字符串media=datalist.get(position.getMedia();
最后一个字符串tit=holder.title.getText().toString();
Log.d(“Harry Coooovaaa”,datalist.toString());
seek=holder.seekBar;
holder.play.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
medialink=媒体;
titlestring=tit;
showProgress();
Toast.makeText(上下文、媒体链接、Toast.LENGTH_LONG).show();
新线程(newrunnable()){
@凌驾
公开募捐{
下载文件();
}
}).start();
}
});
}
@凌驾
public int getItemCount(){
返回datalist.size();
}
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
TextView标题、postedby、likes、postedon;
按钮播放;
SeekBar-SeekBar;
公共MyViewHolder(查看项目视图){
超级(项目视图);
title=(TextView)itemView.findViewById(R.id.audiotitle);
title=(TextView)itemView.findViewById(R.id.audiotitle);
postedby=(TextView)itemView.findViewById(R.id.postedby);
postedon=(TextView)itemView.findViewById(R.id.date);
likes=(TextView)itemView.findViewById(R.id.likes);
play=(按钮)itemviewbyd(R.id.butplay);
seekBar=(seekBar)itemviewbyd(R.id.seekBar);
}
}
私有void showProgress(){
对话框=新对话框(上下文);
对话框.requestWindowFeature(窗口.FEATURE\u无\u标题);
setContentView(R.layout.myprogressdialog);
对话框.setTitle(“下载进度”);
cur_val=(TextView)dialog.findviewbyd(R.id.cur_pg_tv);
cur_val.setText(“开始下载…”);
dialog.show();
pb=(ProgressBar)dialog.findviewbyd(R.id.progress\u bar);
pb.setProgress(0);
setProgressDrawable(context.getResources().getDrawable(R.drawable.green_progress));
}
私有void下载文件(){
试一试{
URL=新URL(基本URL+medialink);
HttpURLConnection urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod(“POST”);
urlConnection.setDoOutput(true);
//连接
urlConnection.connect();
最终字符串文件夹\u main=“Apna”;
//设置要保存文件的路径
/*File SDCardRoot=新文件(Environment.getExternalStorageDirectory(),folder\u main);
如果(!SDCardRoot.exists()){
SDCardRoot.mkdirs();
}
//创建新文件,以保存下载的文件
文件文件=新文件(SDCardRoot,标题)*/
File wallperdirectory=新文件(Environment.getExternalStorageDirectory(),文件夹\ u main);
//如果需要,让对象构建目录结构。
wallperDirectory.mkdirs();
//为输出文件创建文件对象
最终字符串完整性=titlestring.replaceAll(“\”,”);
File outputFile=新文件(壁纸目录,Perfecture+“.mp3”);
//现在将OutputStream附加到文件对象,而不是字符串表示
FileOutputStream fileOutput=新的FileOutputStream(outputFile);
//用于从internet读取数据的流
InputStream InputStream=urlConnection.getInputStream();
//这是我们正在下载的文件的总大小
totalSize=urlConnection.getContentLength();
act.rununuithread(新的Runnable(){
公开募捐{
pb.setMax(总尺寸);
}
});
//创建一个缓冲区。。。
字节[]缓冲区=新字节[1024];
int bufferLength=0;
而((bufferLength=inputStream.read(buffer))>0){
fileOutput.write(buffer,0,bufferLength);
downloadedSize+=缓冲区长度;
//更新进度条//
act.rununuithread(新的Runnable(){
公开募捐{
pb.setProgress(下载大小);
每个浮动=((浮动)下载大小/总大小)*100;
cur_val.setText(“已下载”
public class Recyclerviewaudio extends AppCompatActivity {
RecyclerView recyclerView;
List<List<GettingAudio>> arrylist;
List<GettingAudio> mlist;
AudiorecyclerAdapter audiorecyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclerviewaudio);
recyclerView = (RecyclerView) findViewById(R.id.audiorecycler);
init();
}
public void init() {
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://zuman.ec3.amazonaws.com/")
.addConverterFactory(GsonConverterFactory.create()).build();
Apicall api = retrofit.create(Apicall.class);
Call<List<List<GettingAudio>>> call = api.GetAudio("Audio", "1", "0");
call.enqueue(new Callback<List<List<GettingAudio>>>() {
@Override
public void onResponse(Call<List<List<GettingAudio>>> call, Response<List<List<GettingAudio>>> response) {
arrylist = response.body();
mlist = arrylist.get(0);
Log.d("Boooooodyyy", arrylist.toString());
// Log.d("response code", new Gson().toJson(response.body()));
// Log.d("response body", response.body().toString());
audiorecyclerAdapter = new AudiorecyclerAdapter(Recyclerviewaudio.this, Recyclerviewaudio.this, mlist);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
recyclerView.setAdapter(audiorecyclerAdapter);
}
@Override
public void onFailure(Call<List<List<GettingAudio>>> call, Throwable t) {
Log.d("Throwable", t.getMessage());
}
});
}
}
public void onClick(View v){
seekBar=(SeekBar)itemView.findViewById(R.id.seekBar);
.......
//Yourcode
}