问题:片段onResume()在ViewPager片段实际可见之前被触发.
例如,我有2个片段与ViewPager和FragmentPagerAdapter.第二个片段仅供授权用户使用,我需要让用户在片段可见时登录(使用警告对话框).
但是ViewPager当第一个片段可见时创建第二个片段,以便缓存第二个片段,并在用户开始滑动时使其可见.
因此,onResume()事件在第二个片段变为可见之前就被触发了.这就是为什么我试图找到一个事件,当第二个片段变得可见时,它会在适当的时刻显示一个对话框.
如何才能做到这一点?
我有Activity一个ViewPager.我还有一个Dialog将检索列表,其中包含用户将选择的一些项目.现在,如何更新Fragment项目应显示的位置作为附加到此片段的新视图?
这是我ViewPager的适配器:
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 1;
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment1();
default:
return null;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,这是Fragment:
public class Fragment1 extends Fragment{
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) { …Run Code Online (Sandbox Code Playgroud) 我理解我能给出的最低数字setOffscreenPageLimit(int)是1.但我需要一次加载一页因为内存问题.
我将不得不使用旧式tabhost等?或者有没有办法/黑客我可以让我的viewPager一次加载一页?
我的适配器使用ViewHolder patern扩展BaseAdapter.
我有一个至少有3个片段的viewpager.如果用户将数据输入A,然后输入B,则输入C并返回A,数据从A中丢失.
我想我在某处看到这与内存管理有关,因为片段只连接或存储相邻的片段,当我离得太远时它会杀死A. 但是我希望保留每个片段的生命,即使我有> 3.
除非明确指示,否则如何告诉viewpager永远不会杀死片段?
android android-fragments android-viewpager android-fragmentactivity
我有四个标签和四个片段(每个标签各一个).
每个片段都有一个垂直的回收站视图.由于所有片段视图看起来相似,我重新使用相同的布局文件,相同的回收器视图项和相同的适配器.
问题是在第一个选项卡和第三个选项卡和第四个选项卡下只加载了一个项目,而第二个选项卡成功加载了整个数据.
我希望下面添加的图片可以更好地理解这个问题.
这是我的适配器代码
public class OthersAdapter extends RecyclerView.Adapter<OthersAdapter.OthersViewHolder> {
private final Context context;
private final ArrayList<LocalDealsDataFields> othersDataArray;
private LayoutInflater layoutInflater;
public OthersAdapter(Context context, ArrayList<LocalDealsDataFields> othersDataArray) {
this.context = context;
this.othersDataArray = othersDataArray;
if (this.context != null) {
layoutInflater = LayoutInflater.from(this.context);
}
}
class OthersViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView othersSmallTitleTextView;
ImageView othersImageView;
OthersViewHolder(View itemView) {
super(itemView);
othersSmallTitleTextView = (TextView) itemView.findViewById(R.id.others_small_title);
othersImageView = (ImageView) itemView.findViewById(R.id.others_image);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent couponDetailsItem = new Intent(context, …Run Code Online (Sandbox Code Playgroud) 我用ViewPager用Fragment.
但它一次加载两页.
有没有办法在viewpager中只加载一个页面?
android android-fragments android-viewpager fragmentstatepageradapter
即使我设置了oViewPager.setOffscreenPageLimit(0),我的视图寻呼机仍然会在可见页面的每一侧加载1个屏幕页面.
如何在用户滑动页面时仅加载页面?
我的适配器如果有帮助:
public class MainPagerAdapter extends FragmentPagerAdapter {
protected static final String[] CONTENT = new String[] { "page 1", "page 2", "page 3", "page 4", };
private int mCount = CONTENT.length;
public MainPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//return TestFragment.newInstance(CONTENT[position % CONTENT.length]);
return MainFragment.newInstance(position);
}
@Override
public int getCount() {
return mCount;
}
@Override
public CharSequence getPageTitle(int position) {
return MainPagerAdapter.CONTENT[position % CONTENT.length];
}
public void setCount(int count) {
if (count > 0 && …Run Code Online (Sandbox Code Playgroud) 嗨,
我创建了一个使用ViewPager的应用程序,所有页面都包含WebView.每个Webview都包含服务器图像,视频,mp3 ...
WebView设置是
public Object instantiateItem(View collection, int position) {
.
.
.
final RelativeLayout imageLayout = (RelativeLayout)
inflater.inflate(R.layout.omb_webview_layout, null);
WebView webview = (WebView) imageLayout.findViewById(R.id.video_view);
ProgressBar pbImage = (ProgressBar) imageLayout.findViewById(R.id.progressBar1);
webview.requestFocus(View.FOCUS_DOWN);
webview.getSettings().setBuiltInZoomControls(false);
webview.getSettings().setSupportZoom(true);
webview.getSettings().setUseWideViewPort(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebViewClient(new mWebViewClient());
webview.getSettings().setPluginsEnabled(true);
webview.clearCache(true);
webview.clearHistory();
if(Pbar != null) {
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
Log.i(TAG, "webview loading progress : "+ progress);
if(progress == 100) {
try {
Pbar.setVisibility(ProgressBar.GONE);
} catch (Exception e) {
Log.e(TAG, "timer progress GONE …Run Code Online (Sandbox Code Playgroud) ViewPager.setOffscreenPageLimit(0)无法按预期工作
喜欢这个问题.但我需要一个自定义的Viewpager,它可以按时加载一页.有什么例子吗?
我的应用程序中有标签.每个标签都有不同的片段,并有不同的菜单.下面是我正在使用的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background">
<include
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<com.CustomViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:background="@color/background"
app:layout_anchor="@id/view_pager"
app:layout_anchorGravity="bottom|center_horizontal"
app:layout_behavior="com.widget.ScrollTabBehavior"
app:tabBackground="@color/background"
app:tabIndicatorColor="@color/toolbar"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabPaddingEnd="0dp"
app:tabPaddingStart="0dp" />
</android.support.design.widget.CoordinatorLayout>
<RelativeLayout
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginLeft="-64dp"
android:layout_marginStart="-64dp"
android:background="@color/toolbar"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp">
<ImageButton
android:id="@+id/close_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@android:color/transparent"
android:padding="5dp"
android:src="@drawable/close_icon" />
<view
android:id="@+id/drawerlist"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/close_btn" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
现在,onCreate() …
如何在ViewPager中禁用预加载?
我有一个ViewPager,每页有3页片段.所以我不想加载下一页和上一页.如何禁用此行为?
我试过了
viewPager.setOffscreenPageLimit(1)
但这不起作用.
并尝试过
@Override
public void setMenuVisibility(final boolean visible) {
super.setMenuVisibility(visible);
if (visible) {
// do something nice here
}
}
Run Code Online (Sandbox Code Playgroud)
但是下一个片段中的RecyclerView显示了这个
RecyclerView:没有连接适配器; 跳过布局
为我的英语道歉
android android-fragments android-viewpager android-recyclerview