【问题标题】:how to set progress bar when recycler view is clicked单击回收站视图时如何设置进度条
【发布时间】:2017-01-23 13:57:25
【问题描述】:

我有一个应用程序,它从 json 获取数据并将其显示在回收站视图中 并且当单击每个回收器视图时,它会打开一个新活动以显示完整内容。我只想知道如何在第二个活动显示感谢之前显示进度对话框。 这是我的代码

        public CustomViewHolder(View view, Context ctx, ArrayList<FeedItem> feeditem) {
        super(view);

        view.setOnClickListener(this);
        this.ctx = ctx;
        this.feeditem = feeditem;
        this.imageView = (ImageView) view.findViewById(R.id.thumbnail);
        this.textView2 = (TextView) view.findViewById(R.id.date);
        this.textView3 = (TextView) view.findViewById(R.id.excerpt);
        this.categories = (TextView) view.findViewById(R.id.categories);
        this.textView = (TextView) view.findViewById(R.id.title);
    }

    @Override
    public void onClick(View v) {
        int position = getAdapterPosition();
        FeedItem feeditem = this.feeditem.get(position);
        Intent intent = new Intent(this.ctx, ScrollingActivity.class);
        intent.putExtra("excerpt",feeditem.getExcerpt());
        intent.putExtra("content",feeditem.getContent());
        intent.putExtra("title",feeditem.getTitle());
        Html.fromHtml(String.valueOf(intent.putExtra("content",feeditem.getContent()))).toString();
        intent.putExtra("thumbnail",feeditem.getAttachmentUrl());
        this.ctx.startActivity(intent);


    }

【问题讨论】:

  • 在显示您的其他活动之前是否有延迟?为什么要显示进度对话框?
  • 是的,有延迟;图像需要时间加载,因此我希望在渲染图像时显示进度对话框@AbhishekJain

标签: android json android-recyclerview progressdialog


【解决方案1】:

你必须在你的第二个活动中处理它。在那里,您应该在后台线程中完成所有数据加载或繁重的处理工作,并在数据或处理完成时更新 UI。

AsyncTask 正是为此目的而设计的

public class LoadDataTask extends AsyncTask<Void, Void, Void> {
  public LoadDataTask(ProgressDialog progress) {
    this.progress = progress;
  }

  public void onPreExecute() {
    progress.show();
  }

  public void doInBackground(Void... unused) {
    ... load your image here ...
  }

  public void onPostExecute(Void unused) {
    progress.dismiss();
  }
}

onPreExecute()onPostExecute() 在 UI 线程上运行。因此,您可以在此处更新您的 UI,例如显示图像。

您的第二个活动现在应该如下所示:

ProgressDialog progress = new ProgressDialog(this);
progress.setMessage("Loading...");
new LoadDataTask(progress).execute();

如需进一步帮助,请查看以下内容:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多