scroll lag in Custom ListView - java

My project is a tuition management book in this I list all students with their photos, names, IDs, classes in Activity by using a custom listView it runs well but when adding more student it showing lag in listview scrolling. using the below adapter I am facing scroll lag in listView. I reduce the size of the image(under 500kb)which store in the SQLite database but still it's not working smoothly. **
Adapter code
public class MyListAdapter extends BaseAdapter {
private Context context;
private int layout;
private ArrayList<slmodel> recordlist;
public MyListAdapter(Activity context, int layout, ArrayList<slmodel> recordlist) {
this.context = context;
this.layout= layout;
this.recordlist = recordlist;
}
#Override
public int getCount() {
return recordlist.size();
}
#Override
public slmodel getItem(int position) {
return recordlist.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder{
ImageView imageView;
TextView name,id, sclass;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row=convertView;
ViewHolder holder=new ViewHolder();
if(row==null)
{
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=inflater.inflate(layout,null);
holder.name=row.findViewById(R.id.bname);
holder.id=row.findViewById(R.id.sid);
holder.sclass =row.findViewById(R.id.scourse);
holder.imageView=row.findViewById(R.id.imageView);
row.setTag(holder);
}
else{
holder=(ViewHolder)row.getTag();
}
slmodel model=recordlist.get(position);
holder.name.setText(model.getName());
holder.id.setText(model.getId().toString());
holder.sclass.setText(model.getsclass());
byte[] recordimgae=model.getImgae();
Bitmap bitmap= BitmapFactory.decodeByteArray(recordimgae,0,recordimgae.length);
holder.imageView.setImageBitmap(bitmap);
return row;
}
}

Welcome to SO, and congratulations on posting your first question.
use image loading libraries like Picasso or Glide
with these libraries, you can:
resize your image
use caches to decrease loading time
handle background thread automatically for loading and converting bitmaps.
sample for Glide:
Glide.with(context)
.load(recordImage)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.imageView);

Related

Why my Viewpager is not showing images in recyclerview?

Actually i am new to android development and working on an app and trying to implement viewpager to show the images like carousel in recyclerview but don't know how to set the viewpager in recyclerview onBindViewHolder function to show viewpager the images.
Actually i m trying to implement like this
and this is mine app's screenshot
so please guide me how can i implement it
my Adapter Class
public class RoomsAdapter extends RecyclerView.Adapter<RoomsAdapter.RoomsViewHolder> {
Context context;
ArrayList<Rooms> rooms;
public RoomsAdapter(Context context, ArrayList<Rooms> rooms) {
this.context = context;
this.rooms = rooms;
}
#NonNull
#Override
public RoomsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_product, parent, false);
RoomsViewHolder viewHolder = new RoomsViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RoomsViewHolder holder, int position) {
Rooms room = rooms.get(position);
holder.binding.roomsTitle.setText(room.getTitle());
holder.binding.roomsRent.setText("Rent ₹ " + room.getPrice() + " /month");
holder.binding.roomsLocation.setText(room.getLocation());
}
#Override
public int getItemCount() {
return rooms.size();
}
public class RoomsViewHolder extends RecyclerView.ViewHolder {
ItemProductBinding binding;
public RoomsViewHolder(#NonNull View itemView) {
super(itemView);
binding = ItemProductBinding.bind(itemView);
}
}
}
Actually, i didn't set the viewpager here coz i know how to the set the image view but not any idea how can I set the viewpager in recyclerview to show images from url or drawable.
Please guide me

ViewPager not responding quickly

While using a ViewPager i noticed that it wouldn't respond very quickly, meaning that it slows down when switchting through fragments, kind of in a laggy way.
Does anyone know how this code could be causing the issue?
public class List_adapter : BaseAdapter<Element>
{
public List<Element> _list;
FragmentOne _context;
public List_adapter(FragmentOne context, List<Element> list)
{
super(context, list);
_list = list;
_context = context;
}
#Override
public int getCount()
{
return _list.size();
}
#Override
public Object getItem(int position)
{
return _list.get(position);
}
#Override
public long getItemId(int position)
{
return getItem(position).hashCode();
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = (LayoutInflater)getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.row, parent, false);
Element item = getItem(position);
TextView lbl = (TextView)view.findViewById(R.id.label);
TextView prop = (TextView)view.findViewById(R.id.Prop);
lbl.setText(item.getlabel());
prop.setText(item.getprop());
return view;
}
}
Use ViewHolder pattern for smooth list scrolling
Implement Offscreen page limit for initialising a view before showing it inside ViewPager

How to add a fast-scrolling-scrollbar to Recyclerview without 3rd-Libraries

I want to add the Fast-Scroll-Feature (like in the Android contacts app or in Whatsapp contacts) to my RecyclerView?
I would also use a listview but I need Cards in my app on the same layout!
If implementing fast-scroll to Recyclerview is too difficult, using a ViewStub is also an option, or not?
My RecyclerViewAdapter:
public class SongRecyclerViewAdapter extends RecyclerView.Adapter<SongRecyclerViewAdapter.Holder> {
private Song[] sSongs;
public SongRecyclerViewAdapter(Song[] songs) {
sSongs = songs;
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_songview, parent, false);
Holder holder = new Holder(view);
return holder;
}
#Override
public void onBindViewHolder(Holder holder, int position) {
//holder.imvSong.setImageResource(R.drawable.standardartwork);
holder.txvSongTitle.setText(sSongs[position].getTitle());
holder.txvSongInfo.setText(sSongs[position].getArtists());
}
#Override
public int getItemCount() {
return sSongs != null ? sSongs.length : 0;
}
public class Holder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
ImageView imvSong;
TextView txvSongTitle;
TextView txvSongInfo;
public Holder(View layout) {
super(layout);
linearLayout = (LinearLayout) layout;
imvSong = (ImageView) layout.findViewById(R.id.imvSong);
txvSongTitle = (TextView) layout.findViewById(R.id.adap_txvSongtitle);
txvSongInfo = (TextView) layout.findViewById(R.id.txvSongInfo);
}
}
}
Thanks!
the problem in your code would be the commented line :
//holder.imvSong.setImageResource(R.drawable.standardartwork);
to handle this you have 2 solutions:
1) if u have limited small amount of known images, you can to create bitmaps/drawables once when initializing then you will only need to reference those.
2) create a bitmap cache, smart way to decode bitmaps along with reusable bitmap pool and pooling mechanism. This is not trivial and this what 3rd party libraries do for you. I would not suggest to implemented it yourself.

Error when set an ImageAdapter in Inflate Layout?

I am trying to make a gridview on LayoutInflater, when i test my app, it always crashes.
here is my code :
public class Level1 extends Fragment {
public static Fragment newInstance(Context context) {
Level1 f = new Level1();
return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.grid_layout, null);
GridView gridView = (GridView) root.findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(root.getContext()));
}
I think, my problem is in "setAdapter". I can't use context "xxx.this". I've try to change setAdapter context with "getContext" and "getApplicationContext" but it still crashes.
when i delete "setAdapter" my app working but without gridView.
My ImageAdapter is look like this :
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9, R.drawable.pic_10,
R.drawable.pic_11, R.drawable.pic_12,
R.drawable.pic_13, R.drawable.pic_14,
R.drawable.pic_15
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
Please help me..
Please set a breakpoint on this line GridView gridView = (GridView) root.findViewById(R.id.grid_view); and start debugging your app (in eclipse it's the button on the left side of the button you usually use to start your application). When the breakpoint is reached please step over one step and see if gridView is null. For me this looks like the most reasonable source of your error. But without a detailled error message it's hard to say, so please update your post.
if you are getting OUTOFMEMORY exception its generally because you are using large size images. you will have to sample it down. and in fragment you have to use getactivity() for context.

LazyLoading images into a Gallery

If you check out this answer.
Lazy load of images in ListView
Fedor has provided a tutorial on how to lazy load with image view.
But he said it can be used with Gallery with minor modifications.
How do i go about doing this with minor modifications?
Ive Tried this so far.
This is the BaseAdapter
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, String[] d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item, null);
TextView text=(TextView)vi.findViewById(R.id.text);;
ImageView image=new ImageView(this.activity);
text.setText("item "+position);
imageLoader.DisplayImage(data[position], activity, image);
return vi;
}
}
And then in my activity i do this..
LazyAdapter adapter=new LazyAdapter(MainMenu.this, myRemoteImages);
((Gallery) findViewById(R.id.gallery))
.setAdapter(adapter);
It doesnt seem to be working though.
Nothing is showing up
Here is something that I managed to work out. After implementing Fedor's code in a GridView, you must be aware that in the ImageLoader class file there is a class named decodeFile(). In that class Fedor's code automatically resizez the image so that it can become a small thumbnail. You have just to comment the "rescaling" part and your image will appear in full in the GalleryView.

Categories

Resources