本文介绍了如何在Android的ViewPager内部添加ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试获取可滑动以更改其项目的列表视图,以显示下一周或上一周的详细信息.这是我的布局文件的样子'

I've been trying to get a list view that can be swipe to change the its items to show the following or preceding week details. Here is what my layout file looks like'

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/home_pannels_pager">

    <ListView
        android:id="@+id/week_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</android.support.v4.view.ViewPager>

但是我没有得到我想要的.请你帮助我好吗?预先感谢.

But I didn't get what i want. Could you please help me? Thanks in advance.

推荐答案

这不是ViewPager的工作方式.您使用 ViewPager ="http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html" rel ="nofollow"> PagerAdapter .您的ListView将包含在PagerAdapter创建的Fragment中.

This is not how ViewPager works. You feed the pages to ViewPager with a PagerAdapter. Your ListView will be contained within a Fragment created by the PagerAdapter.

在布局中:

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/home_pannels_pager" />

在具有以下布局的FragmentActivity中:

In the FragmentActivity with this layout:

ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));

类似PagerAdapter的示例:

Example of simle PagerAdapter:

public class PagerAdapter extends FragmentPagerAdapter {

    public FrontPageAdapter(FragmentManager Fm) {
        super(Fm);
    }

    @Override
    public Fragment getItem(int position) {
        Bundle arguments = new Bundle();
        arguments.putInt("position", position);
        FragmentPage fragment = new FragmentPage();
        fragment.setArguments(arguments);
        return fragment;
    }

    @Override
    public int getCount() {
        // return count of pages
        return 3;
    }
}

FragmentPage示例:

Example of FragmentPage:

public class FragmentPage extends Fragment {

    public FragmentPage() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_frontpage, container, false);
        // probably cast to ViewGroup and find your ListView

        Bundle arguments = getArguments();
        int position = b.getInt("position");

        return view;
    }
}

这篇关于如何在Android的ViewPager内部添加ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-13 20:08