我正在使用Viewpager在页面之间切换。我在ViewPager中有五个页面从我的自定义游标适配器获取数据。问题是切换页面不顺畅,这是不稳定的。我究竟做错了什么 ?任何帮助都非常感谢。
// 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;
}
}
//自定义适配器
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);
}
}
}
}
答案 0 :(得分:6)
将来,请尝试发布少量代码。如同,请尝试隔离您可能认为发生问题的特定区域。我知道您可能不知道,在这种情况下,转储您的Adapters
都没问题。只要知道,当有较少的代码可供阅读时,任何人都可以帮助您。
至于你可以做什么,你应该尝试使用setOffScreenPageLimit
中的ViewPager
。
设置应保留在页面两侧的页数 处于空闲状态的视图层次结构中的当前页面。页面超出此范围 在需要时,将从适配器重新创建限制。
示例强>
YOURPAGER.setOffscreenPageLimit(LIMIT);
答案 1 :(得分:0)
你可以设置你的清单“android:hardwareAccelerated =”true“”。
非常顺利