Android 每个页面都有一个listview的ViewPager是滞后的

Android 每个页面都有一个listview的ViewPager是滞后的,android,android-listview,android-viewpager,Android,Android Listview,Android Viewpager,我正在尝试制作一个ViewPager,其中每个页面都有一个包含一些项目的ListView。尽管该应用程序编译和运行良好,但也存在一些问题,即有时需要1到2分钟才能让ViewPager在刷卡时转到下一页或上一页。我使用的代码如下: public class LaunchScreen extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) {

我正在尝试制作一个ViewPager,其中每个页面都有一个包含一些项目的ListView。尽管该应用程序编译和运行良好,但也存在一些问题,即有时需要1到2分钟才能让ViewPager在刷卡时转到下一页或上一页。我使用的代码如下:

public class LaunchScreen extends FragmentActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitylaunchscreen);

        int[] size = {2,3,1,4,1,3,3,3,3,1};

        ArrayList<Parent> parents = new ArrayList<Parent>();

        for(int i=0; i<size.length; i++)
        {
            ArrayList<Child> childs = new ArrayList<Child>();

            for(int j=0; j<size[i]; j++)
            {
                childs.add(new Child("" + j));
            }

            Parent parent = new Parent(childs);
            parents.add(parent);
        }

        MyPagerAdapter myPageAdapter = new MyPagerAdapter(getSupportFragmentManager(), this, parents);
        ViewPager viewPager = (ViewPager) findViewById(R.id.activitylaunchscreen_viewpager);
        viewPager.setAdapter(myPageAdapter);
    }
}
public class MyPagerAdapter extends FragmentPagerAdapter
{
    final String parentFragmentTag = "visit";

    Context context;

    ArrayList<Parent> parents;

    public MyPagerAdapter(FragmentManager fragmentManager, Context context, ArrayList<Parent> parents)
    {
        super(fragmentManager);

        this.context = context;

        this.parents = parents;
    }


    @Override
    public Fragment getItem(int position) 
    {
        MyFragment myFragment = new MyFragment();

        myFragment.setParent(parents.get(position));

        return myFragment;

    }



    @Override
    public int getCount() 
    {
        return parents.size();
    }
}
public class MyFragment extends ListFragment
{
    Parent parent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle)
    {       
        View view = inflater.inflate(R.layout.fragmentparentpage, null);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);
        ChildBaseAdapter childBaseAdapter = new ChildBaseAdapter(getActivity(), parent.getChilds());
        setListAdapter(childBaseAdapter);
    }

    public Parent getParent()
    {
        return parent;
    }

    public void setParent(Parent parent) 
    {
        this.parent = parent;
    }
}
public class ChildBaseAdapter extends BaseAdapter
{
    private String childViewTag = "child";

    Context context;

    ArrayList<Child> childs;

    public ChildBaseAdapter(Context context, ArrayList<Child> childs)
    {
        this.context = context;

        this.childs = childs;
    }

    @Override
    public int getCount() 
    {
        return childs.size();
    }

    @Override
    public Object getItem(int position) 
    {
        return childs.get(position);
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) 
    {

        if(view == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.childlayout, null);
        }
        else
        {
            if(((String) view.getTag()).compareTo(childViewTag) != 0)
            {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.childlayout, null);
            }
        }

        TextView childName = (TextView) view.findViewById(R.id.childlayout_textviewChildName);

        childName.setText(childs.get(position).getNumber());
        view.setTag(childViewTag);

        return view;
    }
}
主启动屏幕活动如下所示:

public class LaunchScreen extends FragmentActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitylaunchscreen);

        int[] size = {2,3,1,4,1,3,3,3,3,1};

        ArrayList<Parent> parents = new ArrayList<Parent>();

        for(int i=0; i<size.length; i++)
        {
            ArrayList<Child> childs = new ArrayList<Child>();

            for(int j=0; j<size[i]; j++)
            {
                childs.add(new Child("" + j));
            }

            Parent parent = new Parent(childs);
            parents.add(parent);
        }

        MyPagerAdapter myPageAdapter = new MyPagerAdapter(getSupportFragmentManager(), this, parents);
        ViewPager viewPager = (ViewPager) findViewById(R.id.activitylaunchscreen_viewpager);
        viewPager.setAdapter(myPageAdapter);
    }
}
public class MyPagerAdapter extends FragmentPagerAdapter
{
    final String parentFragmentTag = "visit";

    Context context;

    ArrayList<Parent> parents;

    public MyPagerAdapter(FragmentManager fragmentManager, Context context, ArrayList<Parent> parents)
    {
        super(fragmentManager);

        this.context = context;

        this.parents = parents;
    }


    @Override
    public Fragment getItem(int position) 
    {
        MyFragment myFragment = new MyFragment();

        myFragment.setParent(parents.get(position));

        return myFragment;

    }



    @Override
    public int getCount() 
    {
        return parents.size();
    }
}
public class MyFragment extends ListFragment
{
    Parent parent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle)
    {       
        View view = inflater.inflate(R.layout.fragmentparentpage, null);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);
        ChildBaseAdapter childBaseAdapter = new ChildBaseAdapter(getActivity(), parent.getChilds());
        setListAdapter(childBaseAdapter);
    }

    public Parent getParent()
    {
        return parent;
    }

    public void setParent(Parent parent) 
    {
        this.parent = parent;
    }
}
public class ChildBaseAdapter extends BaseAdapter
{
    private String childViewTag = "child";

    Context context;

    ArrayList<Child> childs;

    public ChildBaseAdapter(Context context, ArrayList<Child> childs)
    {
        this.context = context;

        this.childs = childs;
    }

    @Override
    public int getCount() 
    {
        return childs.size();
    }

    @Override
    public Object getItem(int position) 
    {
        return childs.get(position);
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) 
    {

        if(view == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.childlayout, null);
        }
        else
        {
            if(((String) view.getTag()).compareTo(childViewTag) != 0)
            {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.childlayout, null);
            }
        }

        TextView childName = (TextView) view.findViewById(R.id.childlayout_textviewChildName);

        childName.setText(childs.get(position).getNumber());
        view.setTag(childViewTag);

        return view;
    }
}
最后,每个页面中ListView的适配器如下所示:

public class LaunchScreen extends FragmentActivity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitylaunchscreen);

        int[] size = {2,3,1,4,1,3,3,3,3,1};

        ArrayList<Parent> parents = new ArrayList<Parent>();

        for(int i=0; i<size.length; i++)
        {
            ArrayList<Child> childs = new ArrayList<Child>();

            for(int j=0; j<size[i]; j++)
            {
                childs.add(new Child("" + j));
            }

            Parent parent = new Parent(childs);
            parents.add(parent);
        }

        MyPagerAdapter myPageAdapter = new MyPagerAdapter(getSupportFragmentManager(), this, parents);
        ViewPager viewPager = (ViewPager) findViewById(R.id.activitylaunchscreen_viewpager);
        viewPager.setAdapter(myPageAdapter);
    }
}
public class MyPagerAdapter extends FragmentPagerAdapter
{
    final String parentFragmentTag = "visit";

    Context context;

    ArrayList<Parent> parents;

    public MyPagerAdapter(FragmentManager fragmentManager, Context context, ArrayList<Parent> parents)
    {
        super(fragmentManager);

        this.context = context;

        this.parents = parents;
    }


    @Override
    public Fragment getItem(int position) 
    {
        MyFragment myFragment = new MyFragment();

        myFragment.setParent(parents.get(position));

        return myFragment;

    }



    @Override
    public int getCount() 
    {
        return parents.size();
    }
}
public class MyFragment extends ListFragment
{
    Parent parent;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle)
    {       
        View view = inflater.inflate(R.layout.fragmentparentpage, null);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) 
    {
        super.onActivityCreated(savedInstanceState);
        ChildBaseAdapter childBaseAdapter = new ChildBaseAdapter(getActivity(), parent.getChilds());
        setListAdapter(childBaseAdapter);
    }

    public Parent getParent()
    {
        return parent;
    }

    public void setParent(Parent parent) 
    {
        this.parent = parent;
    }
}
public class ChildBaseAdapter extends BaseAdapter
{
    private String childViewTag = "child";

    Context context;

    ArrayList<Child> childs;

    public ChildBaseAdapter(Context context, ArrayList<Child> childs)
    {
        this.context = context;

        this.childs = childs;
    }

    @Override
    public int getCount() 
    {
        return childs.size();
    }

    @Override
    public Object getItem(int position) 
    {
        return childs.get(position);
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) 
    {

        if(view == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.childlayout, null);
        }
        else
        {
            if(((String) view.getTag()).compareTo(childViewTag) != 0)
            {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.childlayout, null);
            }
        }

        TextView childName = (TextView) view.findViewById(R.id.childlayout_textviewChildName);

        childName.setText(childs.get(position).getNumber());
        view.setTag(childViewTag);

        return view;
    }
}
公共类ChildBaseAdapter扩展BaseAdapter
{
私有字符串childViewTag=“child”;
语境;
ArrayList childs;
公共ChildBaseAdapter(上下文,ArrayList childs)
{
this.context=上下文;
this.childs=childs;
}
@凌驾
public int getCount()
{
返回childs.size();
}
@凌驾
公共对象getItem(int位置)
{
返回childs.get(位置);
}
@凌驾
公共长getItemId(int位置)
{
返回位置;
}
@凌驾
公共视图getView(内部位置、视图视图、视图组视图组)
{
如果(视图==null)
{
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图=充气机。充气(R.layout.childlayout,null);
}
其他的
{
if(((字符串)view.getTag()).compareTo(childViewTag)!=0)
{
LayoutFlater充气器=(LayoutFlater)context.getSystemService(context.LAYOUT\u充气器\u服务);
视图=充气机。充气(R.layout.childlayout,null);
}
}
TextView childName=(TextView)view.findViewById(R.id.childlayout\u textviewChildName);
setText(childs.get(position.getNumber());
view.setTag(childViewTag);
返回视图;
}
}

提前感谢您的帮助。

nvm这是安卓的填充功能。默认情况下,当您创建一个新的android项目时,android会使主活动的xml文件的父布局具有填充,这似乎会扰乱viewpager侦听器的滚动