I am programming a VR Android application and using a custom listview to display images to the user. The listview, for whatever reason, is incredibly laggy when scrolling. I've looked over many tutorials on how to speed up the listview but I can't seem to figure out what I'm doing wrong. I've tried decreasing the size of the imagebuttons, disabled the scroll cache, and many other things. Note: I don't think the problem is the picasso call, because when I comment it out, it is still slow when scrolling.
My XML for the custom layout is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="170dp"
android:padding="10dp">
<ImageButton
android:layout_width="fill_parent"
android:layout_height="150dp"
android:id="@+id/redirect_btn"
android:longClickable="false"
/>
</LinearLayout>
The code for the custom listview is here:
public class MyCustomAdapter extends BaseAdapter
{
private Activity activity;
private String[] listOfVideoNames;
private String[] listOfVideoTileImageURLs;
private VrVideoInfo[] listOfVrVideoInfo;
private Context context;
private static LayoutInflater inflater = null;
public MyCustomAdapter(String[] listOfVideoNames, String[] listOfVideoTileImages, VrVideoInfo[] vrList, Activity activity, Context context) {
this.listOfVideoNames = listOfVideoNames;
listOfVideoTileImageURLs = listOfVideoTileImages;
this.context = context;
this.activity = activity;
listOfVrVideoInfo = vrList;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return listOfVideoNames.length;
}
@Override
public Object getItem (int pos) {
return listOfVideoNames[pos];
}
@Override
public long getItemId(int pos) {
return 0;
// return 0 if your list items do not have an Id variable
}
public class Holder {
ImageButton imageButton;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder;
if(convertView == null)
{
holder = new Holder();
convertView = inflater.inflate(R.layout.custom_list_layout, parent, false);
holder.imageButton = (ImageButton) convertView.findViewById(R.id.redirect_btn);
convertView.setTag(holder);
}
else
{
holder = (Holder)convertView.getTag();
}
Picasso.with(context).load(listOfVideoTileImageURLs[position])
.into(holder.imageButton);
holder.imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(activity, VideoViewer.class);
intent.putExtra("video_title", listOfVideoNames[position]);
activity.startActivity(intent);
}
});
return convertView;
}
and just in case it is relevant, here is what calls the custom listview:
public class MenuActivity extends Activity {
// string adapter that will handle the data of the list view
MyCustomAdapter adapter;
String[] vrVideoTitles = null;
String[] videoTileImageListURLs = null;
ArrayList<VrVideoInfo> videoInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_activity);
videoInfo = getIntent().getParcelableArrayListExtra("rData");
vrVideoTitles = new String[videoInfo.size()];
videoTileImageListURLs = new String[videoInfo.size()];
for (int i = 0; i < videoInfo.size(); i++) {
vrVideoTitles[i] = videoInfo.get(i).GetTitle();
}
for(int i = 0; i < videoInfo.size(); i++)
{
videoTileImageListURLs[i] = videoInfo.get(i).GetImageURl();
}
VrVideoInfo[] info = videoInfo.toArray(new VrVideoInfo[videoInfo.size()]);
//instantiate custom adapter
adapter = new MyCustomAdapter(vrVideoTitles,
videoTileImageListURLs,info, this, this);
//handle listview and assign adapter
ListView lView = (ListView) findViewById(R.id.list_container);
lView.setOverScrollMode(View.OVER_SCROLL_NEVER);
lView.setScrollingCacheEnabled(false);
lView.setDrawingCacheEnabled(false);
lView.setAdapter(adapter);
}
}
Any information or tips would be very helpful and appreciated.