Java ViewPager作为ListView的行
我试图实现一个ListView,每一行都作为自己的ViewPager。然而,我在片段中有ListView,它是选项卡式ViewPager的一部分。因此,总体上有3个可滑动的选项卡,每个选项卡都有一个ViewPagers列表视图。奇怪的是,它可以在我的模拟器上运行,但不能在我的手机上运行。有人知道为什么或如何修复它吗?下面是一些代码: Main.javaJava ViewPager作为ListView的行,java,android,android-listview,android-viewpager,Java,Android,Android Listview,Android Viewpager,我试图实现一个ListView,每一行都作为自己的ViewPager。然而,我在片段中有ListView,它是选项卡式ViewPager的一部分。因此,总体上有3个可滑动的选项卡,每个选项卡都有一个ViewPagers列表视图。奇怪的是,它可以在我的模拟器上运行,但不能在我的手机上运行。有人知道为什么或如何修复它吗?下面是一些代码: Main.java // Create the adapter that will return a fragment for each of the three
// Create the adapter that will return a fragment for each of the three primary sections
// of the app.
mAppSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the action bar.
final ActionBar actionBar = getActionBar();
// Specify that the Home/Up button should not be enabled, since there is no hierarchical
// parent.
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if(currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
actionBar.setHomeButtonEnabled(false);
}
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager, attaching the adapter and setting up a listener for when the
// user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between different app sections, select the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if we have a reference to the
// Tab.
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by the adapter.
// Also specify this Activity object, which implements the TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(
actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
fragment_viewpager.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="15dp"
android:background="@drawable/layer_card_background"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/layout_main"
android:gravity="center" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".5" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="25dp"
android:text="20"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".5" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="25dp"
android:text="15"
android:textSize="18sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="2dp"
android:background="#58232323" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<de.passsy.holocircularprogressbar.HoloCircularProgressBar
android:id="@+id/hcpb_ingredient"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
事实证明,这段代码确实有效!我只是用一个片段填充了我的ViewPager,所以没有什么可以滚动到的 你想做的是个坏主意。ViewPager
是一个非常昂贵的视图
,将它们放在列表视图中只会导致延迟、结巴和细微的错误。在旧设备上,它甚至可能导致OutOfMemoryExceptions
。我在三个选项卡中的每个选项卡上测试了300多个viewpagers,总共900个viewpagers,没有延迟。但是我不打算每页显示超过10个
if(getItem(position).isResponded()) {
items.add(new ViewPagerItem(getItem(position), colors[position % 5]));
items.add(new ViewPagerItem(getItem(position), colors[position % 5]));
} else {
items.add(new ViewPagerItem(getItem(position), colors[position % 5]));
}
ViewPager viewPager = (ViewPager) row.findViewById(R.id.layout_main);
SentViewPagerAdapterWithView tempMyFriendPagerAdapter = new SentViewPagerAdapterWithView(activity, items);
viewPager.setAdapter(tempMyFriendPagerAdapter);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="15dp"
android:background="@drawable/layer_card_background"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/layout_main"
android:gravity="center" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".5" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="25dp"
android:text="20"
android:textSize="18sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".5" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:padding="25dp"
android:text="15"
android:textSize="18sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="1dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="2dp"
android:background="#58232323" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<de.passsy.holocircularprogressbar.HoloCircularProgressBar
android:id="@+id/hcpb_ingredient"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>