当从android中的服务器加载图像时,RecyclerView不能平滑滚动

时间:2016-03-31 07:16:05

标签: android android-recyclerview

我是Android Material Design的新手,我正在尝试实施RecyclerView。 我正在从服务器加载数据(包括图像)。我使用ION库将图像加载到Imageview。 我必须根据我正在检查的条件来夸大几个视图。目前我正在针对不同的条件对相同的观点进行膨胀。 正确加载图像和其他数据,但是当我滚动回收器视图时,它不是平滑的并且给人一种生涩的感觉。 任何人都可以告诉我如何使滚动顺利和我出错的地方。 我的代码如下:

NewsFeedAdapter

public class NewsFeedAdapter extends RecyclerView.Adapter<NewsFeedAdapter.ParentViewHolder> {
ArrayList<OneFragmentData> arr_data;
Context context;
public static int height;
public static int IMAGE_POLL = 1;

LinearLayoutManager linearLayoutManager;
ArrayList<OptiondData> arr_suboptions = new ArrayList<OptiondData>();
ArrayList<OptiondData> arr_options = new ArrayList<OptiondData>();
ArrayList<QuestionsData> arr_suboptions_answers = new ArrayList<QuestionsData>();
RecyclerView recyclerview;
Activity activity;

public NewsFeedAdapter(ArrayList<OneFragmentData> arr_data, Context context, LinearLayoutManager linearLayoutManager, RecyclerView recyclerview, Activity activity) {
    this.arr_data = arr_data;
    this.context = context;
    this.linearLayoutManager = linearLayoutManager;
    this.recyclerview = recyclerview;
    this.activity = activity;


}

@Override
public int getItemViewType(int position) {
    if ((arr_data.get(position).getPoll_type().compareToIgnoreCase("ques_poll") == 0) && (arr_data.get(position).getItem_type().compareToIgnoreCase("poll") == 0)) {
        return IMAGE_POLL;//poll with image and text options
    } else if ((arr_data.get(position).getPoll_type().compareToIgnoreCase("img_poll") == 0) && (arr_data.get(position).getItem_type().compareToIgnoreCase("poll") == 0)) {
        return IMAGE_POLL;
    } else {
        return IMAGE_POLL;
    }
    //return super.getItemViewType(position);
}

@Override
public ParentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    if (viewType == IMAGE_POLL) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.poll_new_card, parent, false);

        return new PollHolder(view);
    } else {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.poll_new_card, parent, false);
        return new PollHolder(view);
    }

}

@Override
public void onBindViewHolder(ParentViewHolder holder, final int position) {
    if (holder.getItemViewType() == IMAGE_POLL) {
        PollHolder pollHolder = (PollHolder) holder;
        pollHolder.tv_posted_date_pic_pol3_new_poll.setText(arr_data.get(position).getParticipation());
        pollHolder.tv_uname_pic_pol_new_poll.setText(arr_data.get(position).getCreated_name());
        pollHolder.tv_desc_tex_new_poll.setText(arr_data.get(position).getTitle());
        pollHolder.tv_posted_date_pic_pol2_new_poll.setText(arr_data.get(position).getExp_date());
        Ion.with(pollHolder.imv_poll_image_new_poll).error(R.drawable.dummy2).placeholder(R.drawable.dummy2).load(arr_data.get(position).getPoll_image());
        //Ion.with(pollHolder.imv_poll_image_new_poll).error(R.drawable.dummy2).placeholder(R.drawable.dummy2).load(arr_data.get(position));
        pollHolder.tv_posted_date_pic_pol_new_poll.setText("");
        pollHolder.card_one_pic_poll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Variables.options_list.clear();
                Variables.options_list.addAll(arr_data.get(position).getArr_quest_options());
                Log.i("options array for voting","'kkkkk"+Variables.options_list.size());
                Intent polldetails_intent=null;
                Log.i("get poll type", arr_data.get(position).getItem_type().toString());
                if(arr_data.get(position).getItem_type().toString().compareToIgnoreCase("feedback")==0)
                {
                     polldetails_intent = new Intent(context, FeedBackPage.class);
                }
                else {
                    polldetails_intent = new Intent(context, PollDetails.class);
                }
                polldetails_intent.putExtra("desc", arr_data.get(position).getDesc());
                polldetails_intent.putExtra("poll_type", arr_data.get(position).getItem_type());
                polldetails_intent.putExtra("newsfeed_id", arr_data.get(position).getPoll_id());
                polldetails_intent.putExtra("poll_subtype", arr_data.get(position).getPoll_type());
                polldetails_intent.putExtra("name", arr_data.get(position).getCreated_name());
                polldetails_intent.putExtra("posted_date", "");
                polldetails_intent.putExtra("exp_date", arr_data.get(position).getExp_date());
                polldetails_intent.putExtra("poll_image", arr_data.get(position).getPoll_image());
                polldetails_intent.putExtra("title", arr_data.get(position).getTitle());
                polldetails_intent.putExtra("createdUserId", arr_data.get(position).getCreated_userID());
                polldetails_intent.putExtra("participation", arr_data.get(position).getParticipation());
                polldetails_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


                    context.startActivity(polldetails_intent);

            }
        });
    }

}

@Override
public int getItemCount() {
    return arr_data.size();
}

public class ParentViewHolder extends RecyclerView.ViewHolder {


    public ParentViewHolder(View itemView) {
        super(itemView);

    }
}

public class PollHolder extends ParentViewHolder {

    CardView card_one_pic_poll;
    TextView tv_uname_pic_pol_new_poll, tv_posted_date_pic_pol_new_poll, tv_desc_tex_new_poll, tv_posted_date_pic_pol2_new_poll, tv_posted_date_pic_pol3_new_poll;
    ImageView imv_profile_pic_pic_pol_new_poll, imv_indicator_pic_pol_new_poll, imv_poll_image_new_poll;

    public PollHolder(View itemView) {
        super(itemView);
        card_one_pic_poll = (CardView) itemView.findViewById(R.id.card_one_pic_poll);
        imv_profile_pic_pic_pol_new_poll = (ImageView) itemView.findViewById(R.id.imv_profile_pic_pic_pol_new_poll);
        imv_indicator_pic_pol_new_poll = (ImageView) itemView.findViewById(R.id.imv_indicator_pic_pol_new_poll);
        tv_uname_pic_pol_new_poll = (TextView) itemView.findViewById(R.id.tv_uname_pic_pol_new_poll);
        tv_posted_date_pic_pol_new_poll = (TextView) itemView.findViewById(R.id.tv_posted_date_pic_pol_new_poll);
        tv_desc_tex_new_poll = (TextView) itemView.findViewById(R.id.tv_desc_tex_new_poll);
        imv_poll_image_new_poll = (ImageView) itemView.findViewById(R.id.imv_poll_image_new_p);
        tv_posted_date_pic_pol2_new_poll = (TextView) itemView.findViewById(R.id.tv_posted_date_pic_pol2_new_poll);
        tv_posted_date_pic_pol3_new_poll = (TextView) itemView.findViewById(R.id.tv_posted_date_pic_pol3_new_poll);

    }
}

}

Onefragment(extends Fragment)类

  NewsFeedAdapter newsFeedAdapter;
  linearLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
    recycler_first_tab.setLayoutManager(linearLayoutManager);

    newsFeedAdapter = new NewsFeedAdapter(arr_data, getActivity().getApplicationContext(), linearLayoutManager, recycler_first_tab, getActivity());
    recycler_first_tab.setAdapter(newsFeedAdapter);

1 个答案:

答案 0 :(得分:0)

在gradle文件中添加picasa库。加载此图片后

Picasso.with(context).load(itm.getImage()).into(imageView);

也请参考此链接

http://javatechig.com/android/how-to-use-picasso-library-in-android