我在活动中使用视图寻呼机,顺序是 - 当我点击第一页的下一个按钮时,它应该转到第二页。如果我单击第二页中的按钮,它应该转到第三页,依此类推。但我当前的应用程序表现得很奇怪,即当我点击第二页的下一个按钮时,它会进入第四页。再一次当我点击第一页的第一个按钮时,它会跳转到第三页,依此类推......
这是代码。
public class ViewPagerActivity extends AppCompatActivity {
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_act);
setViewPager();
/*pager.setPageTransformer(true, new ZoomPageTransformer());
pager.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager()));
pager.post(new Runnable() {
@Override
public void run() {
pager.setCurrentItem(position); //don't know how to use this from fragment
}
});*/
}
private void setViewPager() {
pager = (ViewPager) findViewById(R.id.viewPager);
FragmentManager fm = getSupportFragmentManager();
ViewPagerAdapter adapter = new ViewPagerAdapter(fm);
pager.setAdapter(adapter);
}
public void setCurrentItem(int selectedPosition) {
pager.setCurrentItem(selectedPosition, true);
}
private class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FirstPager.newInstance();
case 1:
return SecondPager.newInstance();
case 2:
return ThirdPager.newInstance();
case 3:
return FourthPager.newInstance();
case 4:
return FifthPager.newInstance();
case 5:
return SixthPager.newInstance();
default:
return null;
}
}
@Override
public int getCount() {
return 6;
}
}
FirstPager:
public class FirstPager extends Fragment {
private ImageView slideArrow;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_pager, container, false);
return v;
}
public static FirstPager newInstance() {
FirstPager fragment = new FirstPager ();
return fragment;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initUI();
}
private void initUI() {
slideArrow = (ImageView) getActivity().findViewById(R.id.next_arrow);
slideArrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((ViewPagerActivity)getActivity()).setCurrentItem (1);
}
});
}
}
同样在其他片段中,我将currentItem设为2,3,4,5和6.但它的位置行为不正确。
我提到了一些post,但在我的情况下,我将如何申请一种方法从片段传递以使位置正常工作?
答案 0 :(得分:4)
我认为最好的方法是:
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
答案 1 :(得分:1)
function f1() {
if [[ $1 =~ $IP_REGEX ]]
then
return 0
else
echo 'Invalid IP'
exit 1
end
}
function f2() {
# read ip from json file using jq
local config=<read config>
# validate ip by calling f1 if second argument passed to f2 is 'ip'
[[ $2 == "ip" ]] && f1 $config
echo $config
}
function read_all_configs() {
pxe-ip=$(f2 'pxe-ip' 'ip')
}
你确定,你正确地说这个吗?你没有。
您正在调用活动中箭头的findViewById。 ViewPager的工作原理是加载此页面,并预加载下一页和上一页以便顺利滑动。当您单击第一页上的按钮时,最后加载的页面(在您的情况下是第二页的)按钮单击事件将被调用。
您必须使用每个片段中的箭头按钮
您可以这样做:
slideArrow = (ImageView) getActivity().findViewById(R.id.next_arrow);
答案 2 :(得分:0)
你可以在这里做两件事:
((ViewPagerActivity)getActivity()).setCurrentItem (next_frag_pos);
答案 3 :(得分:0)
此代码尝试完美。
public class ViewPagerActivity extends FragmentActivity {
private ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_act);
setViewPager();
/*pager.setPageTransformer(true, new ZoomPageTransformer());
pager.setAdapter(new MyViewPagerAdapter(getSupportFragmentManager()));
pager.post(new Runnable() {
@Override
public void run() {
pager.setCurrentItem(position); //don't know how to use this from fragment
}
});*/
}
private void setViewPager() {
pager = (ViewPager) findViewById(R.id.viewPager);
FragmentManager fm = getSupportFragmentManager();
ViewPagerAdapter adapter = new ViewPagerAdapter(fm);
pager.setAdapter(adapter);
}
public void setCurrentItem(int selectedPosition) {
pager.setCurrentItem(selectedPosition, true);
}
private class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(FragmentManager supportFragmentManager) {
super(supportFragmentManager);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FirstPager();
case 1:
return new SecondPager();
case 2:
return new ThirdPager();
case 3:
return new FourthPager();
case 4:
return new FifthPager();
case 5:
return new SixthPager();
default:
return null;
}
}
@Override
public int getCount() {
return 6;
}
}
}