[英]How to find the “last page” in a view pager. Or the total 'number' of views. Android Development
Thank you for your help in advanced.感谢您在高级方面的帮助。 And sorry if this is a very silly question, but I cannot find it elsewhere.
对不起,如果这是一个非常愚蠢的问题,但我在其他地方找不到。 What I want, is basically, to determine the total amount of pages I have in my ViewPager when it is all set up, like in my code.
我想要的是,基本上,确定我的 ViewPager 中的页面总数,就像在我的代码中一样。 I have tried (on a later version) adding the "size variable" to f as it's argument (see, FirstFragment).
我尝试过(在更高版本中)将“大小变量”添加到 f 作为参数(参见 FirstFragment)。 It's not a known number, as the user input's the number of forms (view's) I will create, and passes it to FirstFragment as an intent.
它不是一个已知数字,因为用户输入是我将创建的表单(视图)的数量,并将其作为意图传递给 FirstFragment。 So I have tried :
所以我试过:
Fragment f = new UserFragment();
Bundle bundl = new Bundle();
bundl.putInt("pagenumbers", size);
f.setArguments(bundl);
lf.add(f);
Inside FirstFragment.在 FirstFragment 中。 But when UserFragment extracts its arguments under "pagenumbers" it is returning 0. I then tried to gain access to the getCount() method that lives inside FragmentAdapter, however, I'm not sure how to go about this, given that I'm inside UserFragment when I need it.
但是,当 UserFragment 在“pagenumbers”下提取其参数时,它返回 0。然后我尝试访问位于 FragmentAdapter 内的 getCount() 方法,但是,鉴于我是当我需要它时在 UserFragment 里面。
What I'm trying to do maybe to help clarify, is I'm extracting "current page info" which I now have working thanks to an earlier post of mine and those whom helped.我试图做的可能是为了帮助澄清,我正在提取“当前页面信息”,这要归功于我和那些帮助过的人的早期帖子。 Now I want to see if "currentpage == size (//which is number of pages. Total views in the viewpager)".
现在我想看看“currentpage == size(//这是页数。viewpager 中的总视图)”。 So in the onClick method you can see in UserFragment, if it's the last page, and the user hits the button to go to the next page.
所以在onClick方法中你可以在UserFragment中看到,如果是最后一页,用户点击按钮进入下一页。 I want it to jump to another activity, where it manipulates all the SparseArray data that was collected from all the forms.
我希望它跳转到另一个活动,在那里它操作从所有表单收集的所有 SparseArray 数据。 (Which I need help on how to put this SparseArray data into an intent, I will probably make a new question with this ... as it's not very related).
(我需要关于如何将这个 SparseArray 数据放入意图的帮助,我可能会提出一个新问题......因为它不是很相关)。 So if you could either assist me in getting my current attempts to work (how to do it properly) or just suggest an entirely new method I'm happy.
因此,如果您可以帮助我让我目前的尝试工作(如何正确地进行)或者只是建议一种全新的方法,我很高兴。 As long as I can get it working!
只要我能让它工作!
Thank you very much for all your assistance, in advanced.非常感谢您的所有帮助,提前。 Please help!
请帮忙!
My UserFragment.java :我的 UserFragment.java :
package com.example.thinice;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class UserFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_avgcalcform,
container, false);
final EditText markc = (EditText)rootView.findViewById(R.id.editTextASSMARK);
final EditText marktotc = (EditText)rootView.findViewById(R.id.editTextASSTOT);
final EditText assvalc = (EditText)rootView.findViewById(R.id.editTextASSWORTH);
int currentviewnum = ((FirstFragment) getActivity()).getPager().getCurrentItem();
String pagenum = Integer.toString(currentviewnum);
TextView tv = (TextView) rootView.findViewById(R.id.avg_testtv);
tv.setText(pagenum);
Button b = (Button) rootView.findViewById(R.id.button1);
b.setOnClickListener( new View.OnClickListener(){
public void onClick(View v){
int currentviewnum = ((FirstFragment) getActivity()).getPager().getCurrentItem();
((FirstFragment) getActivity()).saveData(new Question(markc.getText().toString(),marktotc.getText().toString(),assvalc.getText().toString()));
((FirstFragment) getActivity()).getPager().setCurrentItem(currentviewnum+1);
}
});
return rootView;
}
}
If you need to see any more of my code, please let me know.如果您需要查看我的更多代码,请告诉我。 But this is really annoying me.
但这真的让我很烦。 So if anybody out there could help me, it would be fantastic!
所以如果有人能帮助我,那就太棒了! Thank you very much in advanced.
非常感谢您的先进。
FragmentAdapter.java : FragmentAdapter.java :
package com.example.thinice;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;
public class FragmentAdapter extends FragmentPagerAdapter{
private List<Fragment> fragments;
public FragmentAdapter(FragmentManager fragmentManager, ArrayList<Fragment> fragments) {
super(fragmentManager);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
//Bundle args = new Bundle();
//args.putInt("page_position", position+1);
//((Fragment) fragments).setArguments(args);
return this.fragments.get(position);
}
@Override
public void destroyItem (ViewGroup container, int position, Object object)
{
super.destroyItem(container, position, object);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
FirstFragment.java (this is my activity) : FirstFragment.java(这是我的活动) :
package com.example.thinice;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.widget.Button;
public class FirstFragment extends FragmentActivity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_viewpager);
// Check whether the activity is using the layout version with
// the fragment_container FrameLayout. If so, we must add the first fragment
int size = getIntent().getExtras().getInt("numass1");
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
if (savedInstanceState != null) {
return;
}
ArrayList<Fragment> lf = new ArrayList<Fragment>();
for(int count=0; count<size; count ++){
Fragment f = new UserFragment();
lf.add(f);
}
FragmentAdapter hello = new FragmentAdapter(getSupportFragmentManager() , lf);
vp.setAdapter(hello);
// vp.setOffscreenPageLimit(size);
}
SparseArray<Question> questions = new SparseArray<Question>();
public void saveData(Question quest){
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
questions.put(vp.getCurrentItem(), quest);
}
public ViewPager getPager() {
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
return vp;
}
}
EDIT:LOGCAT FILE FOR SUGGESTED SOLUTION :编辑:LOGCAT 文件以获取建议的解决方案:
07-06 20:51:50.241: D/AndroidRuntime(8151): Shutting down VM
07-06 20:51:50.241: W/dalvikvm(8151): threadid=1: thread exiting with uncaught exception (group=0x415a4ba8)
07-06 20:51:50.251: E/AndroidRuntime(8151): FATAL EXCEPTION: main
07-06 20:51:50.251: E/AndroidRuntime(8151): Process: com.example.thinice, PID: 8151
07-06 20:51:50.251: E/AndroidRuntime(8151): java.lang.NullPointerException
07-06 20:51:50.251: E/AndroidRuntime(8151): at com.example.thinice.UserFragment.onCreateView(UserFragment.java:34)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.View.measure(View.java:16497)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.View.measure(View.java:16497)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-06 20:51:50.251: E/AndroidRuntime(8151): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.View.measure(View.java:16497)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-06 20:51:50.251: E/AndroidRuntime(8151): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.View.measure(View.java:16497)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.Choreographer.doFrame(Choreographer.java:544)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.os.Handler.handleCallback(Handler.java:733)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.os.Handler.dispatchMessage(Handler.java:95)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.os.Looper.loop(Looper.java:136)
07-06 20:51:50.251: E/AndroidRuntime(8151): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-06 20:51:50.251: E/AndroidRuntime(8151): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 20:51:50.251: E/AndroidRuntime(8151): at java.lang.reflect.Method.invoke(Method.java:515)
07-06 20:51:50.251: E/AndroidRuntime(8151): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-06 20:51:50.251: E/AndroidRuntime(8151): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-06 20:51:50.251: E/AndroidRuntime(8151): at dalvik.system.NativeStart.main(Native Method)
您应该使用 ViewPager.getAdapter().getCount() getAdapter() 方法返回为 ViewPager 提供页面的对象。
in OnCreate():在 OnCreate() 中:
ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
System.out.println(vp.getChildCount());
The following code is working fine for me:以下代码对我来说工作正常:
public class ViewFlipperMainActivity extends Activity {
private ViewFlipper viewFlipper;
private float lastX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.help_flipper);
viewFlipper = (ViewFlipper) findViewById(R.id.view_flipper);
System.out.println(viewFlipper.getChildCount());
}
}
You set the setOffscreenPageLimit when creates a ViewPager.在创建ViewPager时设置 setOffscreenPageLimit。
int cont = list.size()
mViewPager.setOffscreenPageLimit(cont);
Then you can access by然后你可以访问
mViewPager.getOffscreenPageLimit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.