在RecyclerView的适配器里面有AsyncTask,如何同步?

时间:2016-02-07 14:53:07

标签: android android-asynctask

我在AsyncTask的适配器中使用了RecyclerView。这是迄今为止的代码:

    public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.FeedsViewHolder>
{
    private static final String CHECK = "http";

    DataHolder d1 = new DataHolder();    

    public  class FeedsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        CardView cv;

        ImageView profileDp;
        TextView userName;
        TextView nameOfUser;
        TextView tweetText;
        String url;   


        FeedsViewHolder(final View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.cv);

            profileDp = (ImageView)itemView.findViewById(R.id.profile_pic);
            userName = (TextView)itemView.findViewById(R.id.tweet_user_name);
            nameOfUser = (TextView)itemView.findViewById(R.id.tweet_name_of_user);
            tweetText = (TextView)itemView.findViewById(R.id.tweet_text);

            Typeface face= Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Roboto-Regular.ttf");
            tweetText.setTypeface(face);
            nameOfUser.setTypeface(face);
            userName.setTypeface(face);

            if(!d1.feeds.get(getLayoutPosition()+1).getLinks().isEmpty())
            {
                url = d1.feeds.get(getLayoutPosition()+1).getLinks().get(0);
                new SoupeX().execute(url);

                tweetText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        Intent i = new Intent(Intent.ACTION_VIEW);
                        i.setData(Uri.parse(url));
                        itemView.getContext().startActivity(i);

                    }
                });
            }

            itemView.setOnClickListener(this);

        }


        @Override
        public void onClick(View view)
        {

        }

    }

    private class DataHolder
    {
        List<Status> feeds;

    }

    CustomAdapter(List<Status> mpost){
        this.d1.feeds = mpost;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }


    @Override
    public FeedsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card, 
                                      viewGroup, false);
        FeedsViewHolder pvh = new FeedsViewHolder(v);
        return pvh;
    }

    @Override
    public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
    {

        if(d1.feeds.get(i).getUser().getProfileImageUrlHttps()!=null)
        {

            Picasso.with(feedViewHolder.profileDp.getContext())
                    .load(d1.feeds.get(i).getUser().getProfileImageUrlHttps())
                    .into(feedViewHolder.profileDp);

        }

        if(d1.feeds.get(i).getUser().getName().length()>12)
        {
            feedViewHolder.nameOfUser.setText(d1.feeds.get(i).getUser().getName().substring(0,11));
        }
        else
        {
            feedViewHolder.nameOfUser.setText(d1.feeds.get(i).getUser().getName());
        }


        feedViewHolder.userName.setText("@"+d1.feeds.get(i).getScreenName());
        try
        {
            int temp = d1.feeds.get(i).getText().indexOf(CHECK);

            if (temp != -1)
            {
                 String A = d1.feeds.get(i).getText().substring(0, temp);
                 String B = d1.feeds.get(i).getText().substring(temp);


                if(B.length()>30)
                    B = B.substring(0,30)+"...";

                 String C = "<font color='#008FB3'>" + B + "</font>";
                 feedViewHolder.tweetText.setText(Html.fromHtml(A + C));

            }
            else
            {
                 feedViewHolder.tweetText.setText(d1.feeds.get(i).getText());
            }
        }
        catch (Exception e)
        {e.printStackTrace();}

        //feedViewHolder.profileDp.setImageUrl(d1.feeds.get(i).getUser().getProfileImageUrlHttps(),imageLoader);

    }

    @Override
    public int getItemCount()
    {

        if(d1.feeds!=null)
        {
            return d1.feeds.size();
        }
        else
        {
            return 0;
        }
    }

    private class SoupeX extends AsyncTask <String, String,String>
    {

        @Override
        protected void onPreExecute()
        {

        }

        protected String doInBackground(String... urls)
        {
            try
            {
                Log.w("LOL:","WORK_TEST:"+urls[0]);
                Document doc = Jsoup.connect(urls[0]).userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
                        .referrer("http://www.google.com").timeout(1000*5).get();
                Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]");
                //images.get(0).attr("src");
                for (Element image : images) {

                    Log.w("INCREDIBLE::", image.attr("src"));
                }

            }
            catch (Exception e){e.printStackTrace();}

          return "";
        }

        protected void onPostExecute(String jsonString)
        {

        }
    }

}

现在麻烦的是,我如何将结果填充回RecyclerView,比如我想将一个String填充到TextView,该过程是什么,以及如何获取getLayoutPosition()以填充到确切的位置? 感谢

1 个答案:

答案 0 :(得分:1)

您可以使用

((RecyclerView.LayoutParams)itemView.getLayoutParams())。getViewAdapterPosition()

获取布局位置。

<强> EDITED
您可以像这样访问onPostExecute中的textView。

private class SoupeX extends AsyncTask <String, String,String>
    {
    private TextView textView;
    public SoupeX(TextView textView){
         this.textView = textView;
    }
    ...
    @Override
    protected void onPostExecute(String result) {
      textView.setText(result);
    }

<强> EDITED

你可以在这里绑定xml

    private TextView textView;
    FeedsViewHolder(final View itemView) {
        super(itemView);
        textView = (TextView)itemView.findViewById(R.id.yourtextviewid);