8

我正在使用 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); 
            }
         }
         }


}
4

2 回答 2

6

将来,尝试发布更少的代码。例如,尝试隔离您可能认为问题正在发生的特定区域。我知道您可能不知道,在这种情况下,倾倒您Adapters的两个都可以。只要知道当需要阅读的代码较少时,任何人都可以更轻松地帮助您。

至于你能做什么,你应该尝试使用setOffScreenPageLimitfrom ViewPager

设置应保留到处于空闲状态的视图层次结构中当前页面的任一侧的页面数。超出此限制的页面将在需要时从适配器重新创建。

这是它的文档。

例子

    YOURPAGER.setOffscreenPageLimit(LIMIT);
于 2012-06-22T07:46:51.527 回答
0

您可以在清单中设置“android:hardwareAccelerated="true"”。这很顺利

于 2012-08-07T20:12:03.400 回答