I am new to Android Material Design
and I am trying to implement a RecyclerView
.
I am loading data (which include images) from the server.I am using ION
library to load the image to Imageview.
I have to inflate several views based on a condition that I am checking. Currently I am inflating the same view for different conditions.
The images and rest of the data is loaded correctly but when I scroll the recyclerview it is not smooth and gives a jerky feeling.
Can anyone tellme how can I make the scrolling smooth and where I am going wrong.
My codes are as follows:
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) class
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);