- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在向 cwac-pager
的 ArrayPagerAdapter
(v4
) 添加另一个页面时遇到问题。我不得不使用该库,因为我无法使用系统 PagerAdapter
动态添加新选项卡。MainActivity.java
:
public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {
private ViewPager viewPager;
private ArrayPagerAdapter pagerAdapter;
private TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar((Toolbar) findViewById(R.id.app_toolbar));
tabLayout = findViewById(R.id.tab_layout);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = findViewById(R.id.pager);
pagerAdapter = new CustomPagerAdapter(getSupportFragmentManager(), new ArrayList<PageDescriptor>());
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(this);
}
@Override
public void onStart() {
super.onStart();
pagerAdapter.add(new TestPageDescriptor());
tabLayout.addTab(tabLayout.newTab().setText("Hello"));
// Uncomment the following lines to make the app crash
pagerAdapter.add(new TestPageDescriptor()); // CRASH
tabLayout.addTab(tabLayout.newTab().setText("Hello2"));
}
@Override
public void onStop() {
super.onStop();
// Remove all the tabs (required in my main application, not in this test)
tabLayout.removeAllTabs();
for (int i = 0; i < pagerAdapter.getCount(); i++) {
pagerAdapter.remove(i);
}
}
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
private class CustomPagerAdapter extends ArrayPagerAdapter<Fragment> {
CustomPagerAdapter(FragmentManager fragmentManager, List<PageDescriptor> descriptors) {
super(fragmentManager, descriptors);
}
@Override
protected Fragment createFragment(PageDescriptor desc) {
return new TestFragment();
}
}
private class TestPageDescriptor extends SimplePageDescriptor {
TestPageDescriptor() {
super("Test","TestHey");
}
}
}
TestFragment.java
:
public class TestFragment extends Fragment {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.test_frag, container, false);
}
}
activity_main.xml
:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="me.test.myapplication.MainActivity"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/app_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:title="@string/app_name" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent" />
</LinearLayout>
test_frag.xml
:只是一个带有 View
的 LinearLayout
,无论是哪个。
在 MainActivity.java
中查看我在 onStart()
中添加的注释:如果您仅使用一个选项卡运行应用程序(方法的最后两行注释), Activity 将正确开始。但是,如果您通过取消注释这些行来添加另一个选项卡,应用程序将立即崩溃。我做错了什么?
谢谢
编辑:logcat
I/Process: Sending signal. PID: 3877 SIG: 9
Application terminated.
没有异常,没有错误。
最佳答案
fragment 标签必须是唯一的,如 the library documentation 中所述.因此,正如 Matt Clark 指出的那样,您需要为不同的页面使用不同的标签。
请注意,您不需要创建自己的 SimplePageDescriptor
子类,至少在您问题的代码中是这样。您可以直接使用 SimplePageDescriptor
。
关于android - 添加另一个项目后应用程序崩溃 - cwac-pager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48609986/
我正在尝试一下 jqGrid 设置,有些事情我不清楚,比如 pager 的正确用法。这个想法是向顶部和底部栏添加一些自定义按钮。 这是我一直在使用的代码: $("#order_logs").jqGri
我正在尝试在我的应用程序中实现视差效果。我有一个 FragmentActivity 实现了 OnPageChangeListener 接口(interface)并监听了我的 ViewPager 滚动事
我是 Android 新手。我创建了一个 View 寻呼机,它从 firebase 获取图像。并且运行良好。但该 View 寻呼机有一些漏洞我未能解决。 1) 我的 View 寻呼机没有实时更新。意味
我通过在 Eclipse 中使用此模板,使用 viewpager 实现了一个轻扫选项卡应用程序 现在我得到一个像这样扩展 FragmentActivity 的 Activity pac
默认情况下寻呼机居中于中心位置。但是,当我为我的列提供固定宽度大小时,寻呼机位置会向左移动。 但我希望 ti 居中对齐。我怎样才能做到这一点 ? 最佳答案 我认为可以通过从寻呼机左侧删除 width
我正在使用带有tablesorter 和tablesorter.pager 的表。表格和寻呼机都位于具有背景颜色的 div 中。 我遇到的问题是寻呼机。它没有包含在 div 内,而是漂浮在其下方。我认
我已经创建了一个带有上一个和下一个链接的 ajax 页面自定义控件,现在当我向控件添加下拉列表时,它无法按预期工作.. 我使用了代码形式 http://www.flixon.com/Articles/
每当我从 ViewPager 中删除图像时。然后有时下一张图片会被删除。或者有时没有图像删除。 我不知道哪里错了。 我的画廊 Activity public class GalleryActivity
我在将值传递给我的类时遇到了问题: public class FragmentViewPager extends Fragment { public static int p; pri
我在向 cwac-pager 的 ArrayPagerAdapter (v4) 添加另一个页面时遇到问题。我不得不使用该库,因为我无法使用系统 PagerAdapter 动态添加新选项卡。 MainA
我在pager.setAdapter(tabpager); 这是代码,请检查一下,让我知道错误 主要 Activity 的 Java 代码 package com.example.protot
我有一个包含回收 View 的 fragment ,在每个项目上按下都会显示另一个 fragment ,该 fragment 持有一个在每次滑动时显示 fragment 的 View 寻呼机。当我在按
我通读了有关此问题 Stack 的其他几篇文章,但未能找到解决方案。这是我的 html 的链接。我是否遗漏了任何 CSS 或其他内容? 我正在测试的代码只是来自 jqGrid 站点的示例
我在 android view-pager(ACL 的 r4)中发现了一个有趣的错误。 在较旧或较慢的设备上,当您的手指滚动得太快/离开屏幕边缘时,速度跟踪器会以错误的方式抛出 View 寻呼机。 我
能否将 View Pager 设置为自动滑动或自动翻页。我将我的 viewpager 设置为使用如下所示的适配器,它工作正常:- @Override public void onCreate(Bund
我正在 Fragment 中实现 Pager Adapter。当我第一次加载屏幕时,它工作正常。如果我切换到其他 fragment 并再次转到上一个 fragment ,它会显示空屏幕。如果我在不同的
我已经使用 android view pager 来显示图像和文本,现在我想要的是,如果用户没有更改图像,那么它应该在几秒钟后以及当用户再次开始使用他的手指时自动开始更改图像那么图像应该不会自动改变?
我有一个带有 View 寻呼机和要显示的三个页面的 Activity 。当 Activity 加载时,只有前两个页面被实例化,即 pageradapter 类中的 instantiateitem 仅针
我已经设置了 core.pager=cat,但是它没有在输出的末尾插入换行符,所以新的提示出现在与最后一行相同的行上git 的输出。 我该如何解决? 例子: Lohoris@Pelociccio /c
我知道你可以做到 git --no-pager 以防止输出通过寻呼机运行。我也意识到你可以使用,例如,git config --global core.pager cat . 但是有些命令我确实想自动
我是一名优秀的程序员,十分优秀!