私は曲をダウンロードするアプリケーションを作成しています。私のアプリケーションでは、曲のリストを含むRecyclerView
があり、すべての曲にはダウンロードの進行状況を示すSeekBar
があります。私がダウンロードボタンを押すとうまくいきますが、それは間違ってSeekBar
を更新します。私は特定の位置で特定のSeekBar
を更新したいと思います。ここでRecyclerView
のための私のアダプタは次のとおりです。これを実現する方法についてRecyclerView内の特定の位置でSeekBarを更新します。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp">
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/downloadLink"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="10dp" />
<TextView
android:id="@+id/downloadButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/custom_button"
android:text="DOWNLOAD"
android:textSize="15sp" />
<TextView
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@drawable/custom_button"
android:text="PLAY"
android:textSize="15sp"
android:visibility="gone" />
</LinearLayout>
<SeekBar
android:id="@+id/downloadProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/LinearLayout"
android:layout_marginTop="10dp" />
</RelativeLayout>
任意の提案:
@Override
public SongListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
itemViewLayout = LayoutInflater.from(parent.getContext()).inflate(R.layout.song_list_single_list, null);
holder = new ViewHolder(itemViewLayout);
return holder;
}
@Override
public void onBindViewHolder(SongListAdapter.ViewHolder holder, final int position) {
mSongListModel = mSongListArray.get(position);
holder.downloadLink.setText(mSongListModel.getDownloadLink());
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DownloadFileAsync().execute(String.valueOf(position));
}
});
}
@Override
public int getItemCount() {
return mSongListArray.size();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
System.out.println("Permission: " + permissions[0] + "was " + grantResults[0]);
//resume tasks needing this permission
//downloadStreamingAudio();
}
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView downloadLink;
private SeekBar downloadProgress;
private TextView downloadButton;
public ViewHolder(View itemView) {
super(itemView);
downloadLink = (TextView) itemView.findViewById(R.id.downloadLink);
downloadButton = (TextView) itemView.findViewById(R.id.downloadButton);
downloadProgress = (SeekBar) itemViewLayout.findViewById(R.id.downloadProgress);
}
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
ProgressDialog pDialog;
Uri uri;
String timeStamp;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(c);
pDialog.setMessage("Downlaoding Audio...");
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
if (isStoragePermissionGranted()) {
File root = Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/AppDownloads/");
timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
System.out.println("Time Stamp::----" + timeStamp);
try {
if (dir.exists() == false) {
dir.mkdirs();
}
RootFile = new File(dir, timeStamp + ".mp3");
} catch (Exception e) {
e.printStackTrace();
}
}
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
int count;
System.out.println("DownloadUrl::::----"+mSongListArray.get(Integer.parseInt(params[0])).getDownloadLink());
URL url = new URL(mSongListArray.get(Integer.parseInt(params[0])).getDownloadLink());
connection = (HttpURLConnection) url.openConnection();
connection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = connection.getContentLength();
// downlod the file
input = new BufferedInputStream(url.openStream());
output = new FileOutputStream(RootFile);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
//publishProgress((int)(total*100/lenghtOfFile));
updateDownloadProgress((int) (total * 100/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e)
{
Log.d("Error....", e.toString());
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (pDialog.isShowing())
pDialog.dismiss();
Toast.makeText(c, "Download Complete", Toast.LENGTH_LONG).show();
}
}
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (c.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
System.out.println("Permission is granted");
return true;
} else {
System.out.println("Permission is revoked");
ActivityCompat.requestPermissions((Activity) c, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
} else {
System.out.println("Permission is granted");
return true;
}
}
private void updateDownloadProgress(int progress) {
holder.downloadProgress.setProgress(progress);
}
ここRecyclerView
のための私のレイアウトファイルはありますか?
と更新されシークバー? –
ランダム....しかしほとんど最後のもの – AbhayBohra
多分問題はupdateDownloadProgressにあります。ホルダーの進行状況バーを更新して特定のアイテムではなく、ホルダー内の特定のアイテムを更新しようとします。 –