Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
443 views
in Technique[技术] by (71.8m points)

android - how to set progress bar when recycler view is clicked

I have an app which gets data from json and displays it on recycler view and when each recycler view is clicked it opens a new activity to show full content. all i want to know is how to show a progress dialog befrore the second activity shows thanks. Here is my code

        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);


    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to deal with it in your second activity. There, you should do all your data loading or heavy processing work in a background thread and update the UI as and when you have the data or processing is completed.

AsyncTask is designed to serve exactly this purpose

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() and onPostExecute() run on the UI thread. So you can update your UI here, like showing the image.

Your second activity should look like this now:

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

For further help, check these:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...