Android 如果Recyclerview滚动,则项目id更改如何解决? public PDFListAdapter(上下文上下文、数组列表pdfModelClasses、字符串final_nav_opt_name){ this.context=上下文; this.pd
如果Recyclerview滚动,则项目id更改如何解决?Android 如果Recyclerview滚动,则项目id更改如何解决? public PDFListAdapter(上下文上下文、数组列表pdfModelClasses、字符串final_nav_opt_name){ this.context=上下文; this.pd,android,android-recyclerview,Android,Android Recyclerview,如果Recyclerview滚动,则项目id更改如何解决? public PDFListAdapter(上下文上下文、数组列表pdfModelClasses、字符串final_nav_opt_name){ this.context=上下文; this.pdfModelClasses=pdfModelClasses; this.final_nav_opt_name=final_nav_opt_name; databaseNotes=新的databaseNotes(上下文); } 公共类MyView
public PDFListAdapter(上下文上下文、数组列表pdfModelClasses、字符串final_nav_opt_name){
this.context=上下文;
this.pdfModelClasses=pdfModelClasses;
this.final_nav_opt_name=final_nav_opt_name;
databaseNotes=新的databaseNotes(上下文);
}
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
TextView txtBookName、txtBookTitle、txtBookBookDateOFIssue、txtBookCategory、txtDownload;
线性布局布局\打开\ pdf,布局\下载\注释\选项;
ImageView imgDownloadNote、imgCancelDownloadNote;
ProgressBar进度\下载\备注;
公共MyViewHolder(视图){
超级(视图);
txtBookName=(TextView)view.findViewById(R.id.txtBookName);
txtBookTitle=(TextView)view.findViewById(R.id.txtBookTitle);
txtBookBookDateOFIssue=(TextView)view.findViewById(R.id.txtBookBookDateOFIssue);
txtBookCategory=(TextView)view.findViewById(R.id.txtBookCategory);
txtDownload=view.findviewbyd(R.id.txtDownload);
layout\u open\u pdf=(LinearLayout)view.findviewbyd(R.id.layout\u open\u pdf);
布局下载注释选项=(LinearLayout)视图.findViewById(R.id.layout下载注释选项);
imgDownloadNote=(ImageView)view.findViewById(R.id.imgDownloadNote);
progress\u download\u note=(ProgressBar)view.findviewbyd(R.id.progress\u download\u note);
imgCancelDownloadNote=(ImageView)view.findViewById(R.id.imgCancelDownloadNote);
}
}
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
View itemView=LayoutInflater.from(parent.getContext()).flate(R.layout.layout\u pdf\u适配器,parent,false);
返回新的MyViewHolder(itemView);
}
@凌驾
BindViewHolder上的公共无效(最终MyViewHolder holder1,int索引){
holder=holder1;
最终int位置=索引;
pdfList=pdfModelClasses.get(位置);
最终下载的notesdatabase数据库notes=新下载的notesdatabase(上下文);
holder.txtBookName.setText(pdfList.getSubjectName().toUpperCase());
holder.txtBookTitle.setText(StringUtils.getTrimString(pdfList.getTypeName());
holder.txtbookDateOfIssue.setText(pdfList.getType());
holder.txtBookCategory.setText(StringUtils.getTrimString(pdfList.getDescription());
if(databaseNotes.isPurchasedNoteSaved(pdfList.getId(),final_nav_opt_name)){
holder.txtDownload.setVisibility(View.VISIBLE);
holder.layou\u download\u note\u option.setVisibility(View.GONE);
}否则{
holder.txtDownload.setVisibility(View.GONE);
holder.layout\u download\u note\u option.setVisibility(View.VISIBLE);
}
holder.layout\u open\u pdf.setOnClickListener(newview.OnClickListener()){
@凌驾
公共void onClick(视图v){
pdfList=pdfModelClasses.get(位置);
//holder=holder1;
Log.e(“PDFListAdapter”、“布局\打开\ pdf位置=“+位置”);
Log.e(“PDFListAdapter”,“layout_open_pdf=“+pdfList.getId());
if(databaseNotes.isPurchasedNoteSaved(pdfList.getId(),final_nav_opt_name)){
DownloadeNotesModel DownloadeNotesModel=databaseNotes.getnotesbyd(pdfList.getId(),final_nav_opt_name);
意向意向=新意向(上下文,PDFResults.class);
intent.putExtra(“pdfList”,downloadeNotesModel.getFileLocation());
意向。额外(“来自”、“数据库”);
putExtra(“getSubjectName”,downloadeNotesModel.getSubjectName());
背景。开始触觉(意图);
}否则{
final AlertDialog.Builder AlertDialog=新建AlertDialog.Builder(上下文);
alertDialog.setTitle(“警报”);
alertDialog.setCancelable(真);
alertDialog.setMessage(“未下载笔记。是否要下载它?”);
alertDialog.setPositiveButton(“是”,新的DialogInterface.OnClickListener(){
@RequiresApi(api=Build.VERSION\u code.JELLY\u BEAN\u MR2)
public void onClick(DialogInterface dialog,int which){
downloader=新的downloader();
新建CheckSpace().execute(pdfList.getFileName());
}
});
/*alertDialog.setNegativeButton(“否”,新的DialogInterface.OnClickListener(){
@RequiresApi(api=Build.VERSION\u code.JELLY\u BEAN\u MR2)
public void onClick(DialogInterface dialog,int which){
意向意向=新意向(上下文,PDFResults.class);
intent.putExtra(“pdfList”,pdfList.getFileName());
intent.putExtra(“from”,“url”);
intent.putExtra(“getSubjectName”,pdfList.getSubjectName());
背景。开始触觉(意图);
}
});*/
alertDialog.show();
}
}
});
holder.imgDownloadNote.setOnClickListener(新视图.OnClickListener(){
@RequiresApi(api=Build.VERSION\u code.JELLY\u BEAN\u MR2)
@凌驾
公共void onClick(视图v){
Log.e(“PDFListAdapter”、“imgDownloadNote position=“+position”);
Log.e(“PDFListAdapter”、“imgDownloadNote=“+pdfList.getId());
pdfList=pdfModelClasses.get(位置);
holder=holder1;
如果(!databaseNotes.isPurchasedNoteSaved(pdfList.getId(),final_nav_opt_name)){
if(UtilsMethods.isNetworkAvailable(上下文)){
int result=ContextCompat.checkSelfPermission(上下文,Manifest.permission.READ\u外部存储);
如果(结果==已授予PackageManager.PERMISSION){
downloader=新的downloader();
新建CheckSpace().execute(pdfList.getFileName());
}否则{
到
public PDFListAdapter(Context context, ArrayList<NotesResponseInfo> pdfModelClasses, String final_nav_opt_name) {
this.context = context;
this.pdfModelClasses = pdfModelClasses;
this.final_nav_opt_name = final_nav_opt_name;
databaseNotes = new DatabaseNotes(context);
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView txtBookName, txtBookTitle, txtBookBookDateOFIssue, txtBookCategory, txtDownload;
LinearLayout layout_open_pdf, layout_download_note_option;
ImageView imgDownloadNote, imgCancelDownloadNote;
ProgressBar progress_download_note;
public MyViewHolder(View view) {
super(view);
txtBookName = (TextView) view.findViewById(R.id.txtBookName);
txtBookTitle = (TextView) view.findViewById(R.id.txtBookTitle);
txtBookBookDateOFIssue = (TextView) view.findViewById(R.id.txtBookBookDateOFIssue);
txtBookCategory = (TextView) view.findViewById(R.id.txtBookCategory);
txtDownload = view.findViewById(R.id.txtDownload);
layout_open_pdf = (LinearLayout) view.findViewById(R.id.layout_open_pdf);
layout_download_note_option = (LinearLayout) view.findViewById(R.id.layout_download_note_option);
imgDownloadNote = (ImageView) view.findViewById(R.id.imgDownloadNote);
progress_download_note = (ProgressBar) view.findViewById(R.id.progress_download_note);
imgCancelDownloadNote = (ImageView) view.findViewById(R.id.imgCancelDownloadNote);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.layout_pdf_adapter, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder1, int index) {
holder = holder1;
final int position = index;
pdfList = pdfModelClasses.get(position);
final DownloadedNotesDataBase databaseNotes = new DownloadedNotesDataBase(context);
holder.txtBookName.setText(pdfList.getSubjectName().toUpperCase());
holder.txtBookTitle.setText(StringUtils.getTrimString(pdfList.getTypeName()));
holder.txtBookBookDateOFIssue.setText(pdfList.getType());
holder.txtBookCategory.setText(StringUtils.getTrimString(pdfList.getDescription()));
if (databaseNotes.isPurchasedNoteSaved(pdfList.getId(), final_nav_opt_name)) {
holder.txtDownload.setVisibility(View.VISIBLE);
holder.layout_download_note_option.setVisibility(View.GONE);
} else {
holder.txtDownload.setVisibility(View.GONE);
holder.layout_download_note_option.setVisibility(View.VISIBLE);
}
holder.layout_open_pdf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pdfList = pdfModelClasses.get(position);
// holder = holder1;
Log.e("PDFListAdapter", "layout_open_pdf position = "+position);
Log.e("PDFListAdapter", "layout_open_pdf = "+pdfList.getId());
if (databaseNotes.isPurchasedNoteSaved(pdfList.getId(), final_nav_opt_name)) {
DownloadeNotesModel downloadeNotesModel = databaseNotes.getNotesByID(pdfList.getId(), final_nav_opt_name);
Intent intent = new Intent(context, PDFResults.class);
intent.putExtra("pdfList", downloadeNotesModel.getFileLocation());
intent.putExtra("from", "database");
intent.putExtra("getSubjectName", downloadeNotesModel.getSubjectName());
context.startActivity(intent);
} else {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Alert");
alertDialog.setCancelable(true);
alertDialog.setMessage("Notes not downloaded. Do you want to download it?");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void onClick(DialogInterface dialog, int which) {
downloader = new Downloader();
new CheckSpace().execute(pdfList.getFileName());
}
});
/* alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(context, PDFResults.class);
intent.putExtra("pdfList", pdfList.getFileName());
intent.putExtra("from", "url");
intent.putExtra("getSubjectName", pdfList.getSubjectName());
context.startActivity(intent);
}
});*/
alertDialog.show();
}
}
});
holder.imgDownloadNote.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onClick(View v) {
Log.e("PDFListAdapter", "imgDownloadNote position = "+position);
Log.e("PDFListAdapter", "imgDownloadNote = "+pdfList.getId());
pdfList = pdfModelClasses.get(position);
holder = holder1;
if (!databaseNotes.isPurchasedNoteSaved(pdfList.getId(), final_nav_opt_name)) {
if (UtilsMethods.isNetworkAvailable(context)) {
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
downloader = new Downloader();
new CheckSpace().execute(pdfList.getFileName());
} else {
Toast.makeText(context, "storage permission is not granted", Toast.LENGTH_SHORT).show();
PermissionCheck.checkWritePermission(context);
}
} else {
holder.imgDownloadNote.setVisibility(View.GONE);
holder.imgCancelDownloadNote.setVisibility(View.GONE);
holder.progress_download_note.setVisibility(View.GONE);
context.startActivity(new Intent(context, NoInternetActivity.class));
}
}
else Log.e("","Not in db");
}
});
holder.imgCancelDownloadNote.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("PDFListAdapter", "imgCancelDownloadNote position = "+position);
Log.e("PDFListAdapter", "imgCancelDownloadNote = "+pdfList.getId());
final AlertDialog alertDialog = new AlertDialog.Builder(context, R.style.AlertDialogStyle).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Are you sure want to cancel download?");
alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alertDialog.hide();
downloader.cancel(true);
}
});
alertDialog.show();
}
});
}
@Override
public int getItemCount() {
return pdfModelClasses.size();
}
@Override
public int getItemViewType(int position)
{
return position;
}
private void startSave(final Context context, NotesResponseInfo url) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
final base_url b = new base_url();
Retrofit.Builder builder = new Retrofit.Builder().baseUrl(b.BASE_URL);
Retrofit retrofit = builder.client(httpClient.build()).build();
AllApis downloadService = retrofit.create(AllApis.class);
Call<ResponseBody> call = downloadService.downloadFileByUrl(StringUtils.getCroppedUrl(url.getFileName()));
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
if (response.isSuccessful()) {
mNotifyManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(context);
downloader.execute(response.body());
} else {
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
}
});
}
private class Downloader extends AsyncTask<ResponseBody, Integer, Integer> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mBuilder.setContentTitle("Download")
.setContentText("Download in progress")
.setSmallIcon(R.mipmap.lun);
mBuilder.setProgress(100, 0, false);
mNotifyManager.notify(id, mBuilder.build());
}
@Override
protected void onProgressUpdate(Integer... values) {
mBuilder.setContentTitle("Download")
.setContentText("Download in progress")
.setSmallIcon(R.mipmap.lun);
mBuilder.setProgress(100, values[0], false);
mNotifyManager.notify(id, mBuilder.build());
super.onProgressUpdate(values);
}
@Override
protected void onCancelled() {
super.onCancelled();
holder.imgDownloadNote.setVisibility(View.VISIBLE);
holder.imgCancelDownloadNote.setVisibility(View.GONE);
holder.progress_download_note.setVisibility(View.GONE);
mNotifyManager.cancelAll();
}
@Override
protected Integer doInBackground(ResponseBody... params) {
ResponseBody body = params[0];
try {
URL url = new URL(pdfList.getFileName());
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestProperty("Accept-Encoding", "identity");
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
ContextWrapper wrapper = new ContextWrapper(getApplicationContext());
int lenghtOfFile = c.getContentLength();
Log.w("getContentLength",""+lenghtOfFile);
File file = wrapper.getDir("PDF", MODE_PRIVATE);
file = new File(file, pdfList.getSubjectName() + "_" + TimeUtils.getCurrentTimeStamp() + ".pdf");
FileOutputStream f = new FileOutputStream(file);
InputStream in = c.getInputStream();
float finalValue = 0;
byte[] buffer = new byte[100 * 1024];
int len1 = 0;
int progress = 0;
long total = 0;
if (!(isCancelled())) {
while ((len1 = in.read(buffer)) !=-1) {
if (UtilsMethods.isNetworkAvailable(context)) {
f.write(buffer, 0, len1);
total += len1;
setProgress(Integer.parseInt(("" + (int) ((total * 100) / lenghtOfFile))));
/* progress += len1;finalValue = (float) progress/body.contentLength() *100;
setProgress((int) finalValue);
mBuilder.setProgress((int) finalValue,0,false);*/
} else {
File file1 = new File(file.getPath());
file1.delete();
cancel(true);
}
}
new DownloadedNotesDataBase(context).addDonloadedNotesToDatabase(file.getPath(), pdfList);
} else {
File file1 = new File(file.getPath());
file1.delete();
holder.imgDownloadNote.setVisibility(View.VISIBLE);
holder.imgCancelDownloadNote.setVisibility(View.GONE);
holder.progress_download_note.setVisibility(View.GONE);
Toast.makeText(context, "Cancelled", Toast.LENGTH_SHORT).show();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (ProtocolException e1) {
e1.printStackTrace();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
mBuilder.setContentText("Download complete");
mBuilder.setSmallIcon(R.mipmap.ic_logo);
mBuilder.setProgress(100, 100, false);
mNotifyManager.notify(id, mBuilder.build());
holder.txtDownload.setVisibility(View.VISIBLE);
holder.imgDownloadNote.setVisibility(View.GONE);
holder.imgCancelDownloadNote.setVisibility(View.GONE);
holder.progress_download_note.setVisibility(View.GONE);
}
private void setProgress(int progress) {
mBuilder.setContentText("Downloading...")
.setContentTitle(progress + "%")
.setSmallIcon(R.mipmap.ic_logo)
.setOngoing(true)
.setContentInfo(progress + "%")
.setProgress(100, progress, false);
mNotifyManager.notify(id, mBuilder.build());
holder.progress_download_note.setProgress(progress);
}
}
public class CheckSpace extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String file_size = "";
URL url = null;
try {
url = new URL(params[0]);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
int fileSize = urlConnection.getContentLength();
file_size = UtilsMethods.generateFileSize(fileSize);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file_size;
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
protected void onPostExecute(String result) {
if (UtilsMethods.compareSpace(result)) {
final AlertDialog alertDialog = new AlertDialog.Builder(context, R.style.AlertDialogStyle).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Download this PDF of size " + result + " ?");
alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void onClick(DialogInterface dialog, int which) {
alertDialog.hide();
holder.imgDownloadNote.setVisibility(View.GONE);
holder.imgCancelDownloadNote.setVisibility(View.VISIBLE);
holder.progress_download_note.setVisibility(View.VISIBLE);
startSave(context, pdfList);
}
});
alertDialog.show();
} else {
final AlertDialog alertDialog = new AlertDialog.Builder(context, R.style.AlertDialogStyle).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Unable to download file. Storage space is not available");
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void onClick(DialogInterface dialog, int which) {
alertDialog.hide();
}
});
alertDialog.show();
}
}
}
}