I am using Viewpager to switch between pages. I have five pages in ViewPager which are getting data from my custom cursor adapter. The problem is switching pages is not smooth, it's choppy. What am i doing wrong ? Any help is really appreciated .
//MYPAGER
private class MyPagerAdapter extends PagerAdapter{
public int getCount()
{
return 5;
}
public Object instantiateItem(View collection, int position)
{
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view;
SharedPreferences myoptions = getSharedPreferences("options", Context.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = myoptions.edit();
view = inflater.inflate(R.layout.all_tasks, null);
((ViewPager) collection).addView(view, 0);
//getting list view
lv= (ListView)findViewById(R.id.list);
MyAdapter adapter1;
switch (position) {
case 0:
prefEditor.putInt("filter", 5);
prefEditor.commit();
if(selectedList!=null)
{
cursor1= managedQuery(uri, PROJECTION, where +" AND "+ taskColumns.LIST+ "= ?"
+" AND "+ taskColumns.DONE+ "= ?"
,new String[] {selectedList,"0"}, sortingOrder);
}
else
cursor1= managedQuery(uri, PROJECTION, where+" AND "+ taskColumns.DONE+ "= ?",new String[] {"0"}, sortingOrder);
if (cursor1 != null)
{
cursor1.moveToFirst();
}
adapter1 = new MyAdapter(tasks.this,cursor1);
lv.setAdapter(adapter1);
break;
case 1:
prefEditor.putInt("filter", 4);
prefEditor.commit();
if(selectedList!=null)
{
cursor1= managedQuery(uri, PROJECTION, taskColumns.LIST+ "= ?"
,new String[] {selectedList}, sortingOrder);
}
else
cursor1= managedQuery(uri, PROJECTION, where,null, sortingOrder);
if (cursor1 != null)
{
cursor1.moveToFirst();
}
adapter1 = new MyAdapter(tasks.this,cursor1);
lv.setAdapter(adapter1);
break;
case 2:
prefEditor.putInt("filter", 1);
prefEditor.commit();
if(selectedList!=null)
{
cursor1= managedQuery(uri, PROJECTION, where +" AND "+ taskColumns.LIST+ "= ?"
,new String[] {selectedList}, sortingOrder);
}
else
cursor1= managedQuery(uri, PROJECTION, where,null, sortingOrder);
if (cursor1 != null)
{
cursor1.moveToFirst();
}
adapter1 = new MyAdapter(tasks.this,cursor1);
lv.setAdapter(adapter1);
break;
case 3:
prefEditor.putInt("filter", 2);
prefEditor.commit();
if(selectedList!=null)
{
cursor1= managedQuery(uri, PROJECTION, where +" AND "+ taskColumns.LIST+ "= ?"
,new String[] {selectedList}, sortingOrder);
}
else
cursor1= managedQuery(uri, PROJECTION, where,null, sortingOrder);
if (cursor1 != null)
{
cursor1.moveToFirst();
}
adapter1 = new MyAdapter(tasks.this,cursor1);
lv.setAdapter(adapter1);
break;
case 4:
prefEditor.putInt("filter", 3);
prefEditor.commit();
if(selectedList!=null)
{
cursor1= managedQuery(uri, PROJECTION, where +" AND "+ taskColumns.LIST+ "= ?"
,new String[] {selectedList}, sortingOrder);
}
else
cursor1= managedQuery(uri, PROJECTION, where,null, sortingOrder);
if (cursor1 != null)
{
cursor1.moveToFirst();
}
adapter1 = new MyAdapter(tasks.this,cursor1);
lv.setAdapter(adapter1);
break;
}
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState()
{
return null;
}
}
//custom adapter
static class ViewHolder {
public TextView task;
public TextView note;
public ImageView note_img;
public TextView priority;
public CheckBox done;
public TextView showingByList;
public TextView space;
public LinearLayout b;
}
public class MyAdapter extends CursorAdapter {
private final int taskIndex;
private final LayoutInflater Inflater;
private final int doneIndex;
private final int priorIndex;
private final int rowIndex;
private final int taskListIndex;
private final int NoteIndex;
boolean innewView;
public MyAdapter(Context context, Cursor c)
{
super(context, c);
this.taskIndex = c.getColumnIndex(taskColumns.TASK);
this.priorIndex = c.getColumnIndex(taskColumns.PRIORITY);
this.doneIndex = c.getColumnIndex(taskColumns.DONE);
this.rowIndex = c.getColumnIndex(taskColumns._ID);
this.taskListIndex = c.getColumnIndex(taskColumns.LIST);
this.NoteIndex = c.getColumnIndex(taskColumns.NOTE);
this.Inflater = LayoutInflater.from(context);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent)
{
View v = Inflater.inflate(R.layout.tasks_row, parent, false);
innewView=true;
return v;
}
@Override
public void bindView(View view, Context context, final Cursor cursor) {
final ViewHolder viewHolder;
if(innewView)
{
viewHolder = new ViewHolder();
viewHolder.task = (TextView) view.findViewById(R.id.task);
viewHolder.note = (TextView) view.findViewById(R.id.note);
viewHolder.note_img = (ImageView) view.findViewById(R.id.note_img);
viewHolder.priority = (TextView) view.findViewById(R.id.priority);
viewHolder.done = (CheckBox) view.findViewById(R.id.check);
viewHolder.b=(LinearLayout)view.findViewById(R.id.task_layout);
viewHolder.showingByList = (TextView) view.findViewById(R.id.tasklist);
view.setTag(viewHolder);
}
else
viewHolder = (ViewHolder)view.getTag();
innewView=false;
final String task = cursor.getString(taskIndex);
String TaskNote = cursor.getString(NoteIndex);
int prior= cursor.getInt(priorIndex);
int taskDone= cursor.getInt(doneIndex);
final long id=cursor.getLong(rowIndex);
int uniqueyindex = cursor.getColumnIndex(taskColumns.UNIQUEY);
if(TaskNote==null)
{
viewHolder.note.setVisibility(View.GONE);
viewHolder.note_img.setVisibility(View.GONE);
}
else
{
if(TaskNote.length()<1)
{
viewHolder.note.setVisibility(View.GONE);
viewHolder.note_img.setVisibility(View.GONE);
}
else{
viewHolder.note.setVisibility(View.VISIBLE);
viewHolder.note_img.setVisibility(View.VISIBLE);
}
}
if(showbyList){
String taskList = cursor.getString(taskListIndex);
if(taskList==null)
{
ContentValues valuey=new ContentValues();
valuey.put(taskColumns.LIST_ID, "0");
valuey.put(taskColumns.LIST, "personal");
Uri mUry = ContentUris.withAppendedId(uri,id);
getContentResolver().update(mUry, valuey,null,null);
taskList="personal";
}
// viewHolder.showingByList.setTypeface(tf);
viewHolder.b.setVisibility(View.VISIBLE);
viewHolder.space=(TextView)view.findViewById(R.id.space);
viewHolder.space.setVisibility(View.VISIBLE);
if(cursor.moveToPrevious()){
int comparedResult= taskList.compareTo(cursor.getString(taskListIndex));
if(comparedResult==0)
{
viewHolder.showingByList.setVisibility(View.GONE);
}
else{
viewHolder.showingByList.setVisibility(View.VISIBLE);
viewHolder.showingByList.setText(taskList.toUpperCase());
}
}
else{
viewHolder.showingByList.setVisibility(View.VISIBLE);
viewHolder.showingByList.setText(taskList.toUpperCase());
}
cursor.moveToNext();
viewHolder.showingByList.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0) {
}
});
}
viewHolder.b.setBackgroundResource(R.drawable.my_drawable1);
if(toModify.contains((int)id))
viewHolder.b.setBackgroundResource(R.color.light_blue);
viewHolder.task.setText(task);
if(TaskNote!=null)
viewHolder.note.setText(TaskNote);
if(taskDone==1){
viewHolder.done.setChecked(true);
viewHolder.task.setTextColor(getResources().getColor(R.color.smooth));
viewHolder.note.setTextColor(getResources().getColor(R.color.smooth));
viewHolder.note_img.setImageResource(R.drawable.note_done2);
viewHolder.priority.setBackgroundResource(R.drawable.priornone);
}
else{
viewHolder.done.setChecked(false);
viewHolder.task.setTextColor(getResources().getColor(R.color.darkthemed_text));
viewHolder.note.setTextColor(getResources().getColor(R.color.darkthemed_note));
viewHolder.note_img.setImageResource(R.drawable.note2);
if(prior==1){
viewHolder.priority.setBackgroundResource(R.drawable.priornone);
}
else{
viewHolder.priority.setBackgroundResource(R.color.priority_2);
}
}
}
}
In the future, try posting a little less code. As in, try to isolate the specific areas that you may think the problem is occurring in. I understand that you may not know, and in this case dumping both your Adapters
is alright. Just know that it's slightly easier for anyone to help you when there's less code to read over.
As far as what you can do, you should try using setOffScreenPageLimit
from ViewPager
.
Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.
Here are the docs for it.
Example
YOURPAGER.setOffscreenPageLimit(LIMIT);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With