我正在尝试从ViewPager动态添加和删除片段,添加工作没有任何问题,但删除不能按预期工作.
每次我想删除当前项目时,最后一项将被删除.
我还尝试使用FragmentStatePagerAdapter或在适配器的getItemPosition方法中返回POSITION_NONE.
我究竟做错了什么?
这是一个基本的例子:
MainActivity.java
public class MainActivity extends FragmentActivity implements TextProvider {
private Button mAdd;
private Button mRemove;
private ViewPager mPager;
private MyPagerAdapter mAdapter;
private ArrayList<String> mEntries = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mEntries.add("pos 1");
mEntries.add("pos 2");
mEntries.add("pos 3");
mEntries.add("pos 4");
mEntries.add("pos 5");
mAdd = (Button) findViewById(R.id.add);
mRemove = (Button) findViewById(R.id.remove);
mPager = (ViewPager) findViewById(R.id.pager);
mAdd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
addNewItem();
}
});
mRemove.setOnClickListener(new OnClickListener() {
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我在ViewPager中有三个页面(片段),但我只想显示其中两个页面的菜单项.
之前的SO答案中给出的代码似乎不起作用:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser == true) { }
else if (isVisibleToUser == false) { }
}
Run Code Online (Sandbox Code Playgroud)
Eclipse说不需要@Override,也不能设置super.它从未被系统调用,即使它是如何确定当前显示哪个页面?我可以帮忙吗?
我有一个ViewPager实例化a View.当搜索结果返回到视图时,我想暂时禁用viewpager和子按钮的滚动.我打电话viewPager.setEnabled(false)但这不会禁用它.
有人有任何想法吗?
我有一个ViewPager连接到一个显示三个片段的FragmentPagerAdapter.当ViewPager从当前位置多次滑动时,它似乎会破坏托管片段的视图.
这些视图都是简单的列表,这种优化是完全没必要的,所以我想禁用它.它导致一些视觉问题,因为列表具有应用于它们的布局动画,并且这些动画在被销毁和重新创建后正在重放.它还会每次显示滚动条简介动画(滚动条可以短暂显示以指示可以滚动),这可能会分散注意力,并且用户当前的滚动位置会在此过程中丢失.
它也不会加载第三个片段,直到第一次滑动发生,这是有问题的,因为每个片段处理自己的服务调用,我宁愿在活动加载时同时关闭所有三个片段.延迟第三次服务呼叫不太理想.
有没有办法说服ViewPager停止这种行为并将我的所有片段保存在内存中?
我ViewPager用于刷卡之间Fragments,但我可以使用ViewPager在Views简单的XML布局之间滑动吗?
这是AdapterViewPager的页面,用于在片段之间滑动:
import java.util.List;
import com.app.name.fragments.TipsFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.view.ViewGroup;
public class PageAdapter extends FragmentPagerAdapter {
/**
*
*/
List<Fragment> fragments;
public PageAdapter(FragmentManager fm,List<Fragment> frags) {
super(fm);
fragments = frags;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return TipsFragment.newInstance(0, 0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public void destroyItem(ViewGroup container, …Run Code Online (Sandbox Code Playgroud) 我ViewPager用来允许用户在其视图之间滑动.有没有办法如何强制它ViewPager重新加载/重新实例化它们的视图,以防它们不再有效或需要刷新?我试图调用notifyDataSetChanged()它的适配器,但这不会instantiateItem()再次调用方法.
这是从ViewPager及其适配器定义扩展的类.Bellow是refresh()我想要强制刷新项目时调用的方法.
public class DayFlipper extends ViewPager {
public class FlipperAdapter extends PagerAdapter {
@Override
public int getCount() {
return DayFlipper.DAY_HISTORY;
}
@Override
public void startUpdate(View container) {
}
@Override
public Object instantiateItem(View container, int position) {
Log.d(TAG, "instantiateItem(): " + position);
Date d = DateHelper.getBot();
for (int i = 0; i < position; i++) {
d = DateHelper.getTomorrow(d);
}
d = DateHelper.normalize(d);
CubbiesView cv = new CubbiesView(mContext);
cv.setLifeDate(d); …Run Code Online (Sandbox Code Playgroud) 有没有办法让它ViewPager不是水平滚动,而是垂直滚动?!
我有一个应用程序,包括在选项卡模式下使用ActionBarSherlock.我有5个选项卡,每个选项卡的内容使用片段处理.但是对于tab2,我有一个片段,其xml文件包含一个ViewPager元素,该元素又包含一些片段页面.当我最初启动应用程序的应用程序时,我能够在选项卡之间切换没有问题,但是当我第二次按下tab2时,我得到上面提到的错误.主要活动如下:
public class MainActivity extends SherlockFragmentActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
ActionBar.Tab tab1 = actionBar.newTab().setText("Tab1");
ActionBar.Tab tab3 = actionBar.newTab().setText("Tab3");
ActionBar.Tab tab2 = actionBar.newTab().setText("Tab2");
ActionBar.Tab tab4 = actionBar.newTab().setText("Tab4");
ActionBar.Tab tab5 = actionBar.newTab().setText("Tab5");
Fragment fragment1 = new Tab1();
Fragment fragment3 = new Tab3();
Fragment fragment2 = new Tab2();
Fragment fragment5 = new Tab5();
Fragment fragment4 = new Tab4();
tab1.setTabListener(new MyTabListener(fragment1));
tab3.setTabListener(new MyTabListener(fragment3));
tab2.setTabListener(new MyTabListener(fragment2));
tab5.setTabListener(new MyTabListener(fragment5));
tab4.setTabListener(new MyTabListener(fragment4));
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
actionBar.addTab(tab4);
actionBar.addTab(tab5);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); …Run Code Online (Sandbox Code Playgroud) android android-fragments android-viewpager actionbarsherlock android-nested-fragment
所以我有一个引人入胜的问题.尽管我不是手动或以编程方式滚动我的视图,但我的WebView会在其内部数据加载后自动滚动到.
我在viewpager中有一个片段.当我第一次加载寻呼机时,它按预期工作,并显示所有内容.但是一旦我"翻页",数据就会加载,WebView会弹出到页面顶部,将视图隐藏在它上面,这是不可取的.
有谁知道如何防止这种情况发生?
我的布局看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/article_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="2dp"
android:text="Some Title"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/article_title"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/LL_Seperator"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@color/text"
android:orientation="horizontal" >
</LinearLayout>
<WebView
android:id="@+id/article_content"
android:layout_width="match_parent"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/article_link"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:text="View Full Article"
android:textColor="@color/article_title"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
我也没注重任何事情.默认情况下,它似乎在加载后自动滚动到WebView.我该如何防止这种情况?
android android-layout android-webview android-scrollview android-viewpager
我创建了一个ViewPager,一切都运行正常,但是我希望在ViewPager之外有一个前一个下一个按钮,可用于在ViewPager中导航.如何在不手动滑动的情况下转到ViewPager上的下一个项目?