问题描述
需要帮助修复一个code我在 http://stackoverflow.com/a/9275732/1938357 。我需要的解决方案是基于我的应用程序viewpager获取当前视图onany页
下面是code,以供参考。
公开查看getCurrentView(ViewPager寻呼机){
的for(int i = 0; I< pager.getChildCount();我++){
查看孩子= pager.getChildAt(I)
如果(child.getX()&下; = pager.getScrollX()+ pager.getWidth()
&功放;&安培; child.getX()+ child.getWidth()&GT = pager.getScrollX()+ pager.getWidth()){
返回子女;
} }
返回getChildAt(0);
我试图访问视图寻呼机当前视图,并使用上述code。当我向前滚动它工作正常。但是,当我向后滚动再经过2向后滚动这个停止工作。
公共类myActivity扩展FragmentActivity
//变量声明
保护无效的onCreate(捆绑savedInstanceState){
ViewPager mPager =(ViewPager)findViewById(R.id.pager);
mPagerAdapter =新ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
查看CurrView;OnPageChangeListener pageChangelistener =新OnPageChangeListener(){
@覆盖
公共无效使用onPageSelected(INT pageSelected){
doTextViewChnges(); //获取文本查看和更新其基于pageSelected
---这是我在哪里停留在试图获得的TextView在我目前的FRAGMWNT / VIEW -------
}mPager.setOnPageChangeListener(pageChangelistener);
}
私有类ScreenSlidePagerAdapter扩展FragmentStatePagerAdapter {
公共ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager FM){
超(FM);
} @覆盖
公共android.support.v4.app.Fragment的getItem(INT位置){
返回ScreenSlidePageFragment.create(位置,<其他参数,我想传递>);
} @覆盖
公众诠释的getCount(){
返回NUM_PAGES;
}
}
和下面是我ScreenSlidePageFragment类
公共类ScreenSlidePageFragment扩展android.support.v4.app.Fragment {公共静态最后弦乐ARG_PAGE =页面编号;
公共静态最后弦乐ARG_PAGEARRAY =页面;私人诠释mPageNumber;
//声明其他变量@燮pressLint(NewApi)
公共静态ScreenSlidePageFragment创建(INT PAGENUMBER,字符串[]页,INT bookmarkPageNumber,字符串storyName,诠释linesInOnePage){
ScreenSlidePageFragment片段=新ScreenSlidePageFragment();
捆绑ARGS =新包();
args.putInt(ARG_PAGE,PAGENUMBER);
args.putStringArray(ARG_PAGEARRAY,页);
fragment.setArguments(参数);
返回片段;
}@燮pressLint(NewApi)
公共ScreenSlidePageFragment(){
}@燮pressLint(NewApi)
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
mPageNumber = getArguments()调用getInt(ARG_PAGE)。
页= getArguments()getStringArray(ARG_PAGEARRAY)。
}@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
rootView =(ViewGroup中)气筒
.inflate(R.layout.activity_story,集装箱,FALSE); //负载布局填补了该页面所有组件
的loadView(rootView);
返回rootView;
}
公众诠释getPageNumber(){
返回mPageNumber;
}
这是很难与例如code的意见进行讨论。
当我看到的grep code <一个href=\"http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android/4.0.1_r1/android/support/v4/app/FragmentStatePagerAdapter.java#FragmentStatePagerAdapter.setPrimaryItem%28android.view.View,int,java.lang.Object%29\"相对=nofollow> FragmentStatePagerAdapter.setPrimaryItem(),收到对象
为 android.support.v4.app.Fragment
。但你收到 ClassCastException异常
尝试投放对象为片段
时。你可以检查你的施法:你真的投对象 android.support.v4.app.Fragment
不是 android.app.Fragment
?如果没事,添加异常消息你的问题,谢谢。
更新:
如果你的 ScreenSlidePagerAdapter
有方法getCurrentView(),那么你的方法'getCurrentView()'能像这样工作:
公开查看getCurrentView(ViewPager寻呼机){
返回((ScreenSlidePagerAdapter)pager.getAdapter())getCurrentView();
}
和方法 getCurrentView()
你的 ScreenSlidePagerAdapter
:
公开查看getCurrentView(){
返回currView;
}
更新2:
我做了一些修改,与code:
您的活动与私人适配器:
公共类myActivity扩展FragmentActivity实现ISelectItemListener {
//你的变量声明和方法 @覆盖
保护无效的onCreate(捆绑savedInstanceState){
最后ViewPager寻呼机=(ViewPager)findViewById(R.id.pager); mPagerAdapter =新ScreenSlidePagerAdapter(getSupportFragmentManager());
pager.setAdapter(mPagerAdapter); pager.setOnPageChangeListener(
新OnPageChangeListener(){
@覆盖
公共无效使用onPageSelected(INT pageSelected){
最后ScreenSlidePageFragment片段= mPagerAdapter.getFragment(pageSelected); 如果(片段!= NULL){
fragment.doTextViewChnges();
}
} @覆盖
公共无效onPageScrollStateChanged(INT为arg0){
// TODO自动生成方法存根
} @覆盖
公共无效onPageScrolled(INT为arg0,ARG1浮球,诠释ARG2){
// TODO自动生成方法存根
}
});
} 私有类ScreenSlidePagerAdapter扩展FragmentStatePagerAdapter {
//变量声明 私人最终ScreenSlidePageFragment [] mFragments; 公共ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager FM){
超(FM); mFragments =新ScreenSlidePageFragment [NUM_PAGES]
} @覆盖
公众诠释的getCount(){
返回NUM_PAGES;
}
//这将通过创建新的位置,如果ScreenSlidePageFragment它不是exsist
@覆盖
公共android.support.v4.app.Fragment的getItem(INT位置){
如果(getFragment(位置)== NULL){
mFragments [位置] = ScreenSlidePageFragment.create(位置,&lt;其他参数,我想传递&GT;);
} 返回getFragment(位置);
} //如果存在的是它会给你ScreenSlidePageFragment,或者为null
公共ScreenSlidePageFragment getFragment(INT位置){
返回mFragments [位置]
}
}
}
您片段:
公共类ScreenSlidePageFragment扩展片段{
//你的变量和方法 公共静态ScreenSlidePageFragment创建(INT PAGENUMBER,字符串[]页,INT bookmarkPageNumber,字符串storyName,诠释linesInOnePage){
最终捆绑ARGS =新包();
args.putInt(ARG_PAGE,PAGENUMBER);
args.putStringArray(ARG_PAGEARRAY,页); 最后ScreenSlidePageFragment片段=新ScreenSlidePageFragment();
fragment.setArguments(参数); 返回片段;
} @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
mPageNumber = getArguments()调用getInt(ARG_PAGE)。
页= getArguments()getStringArray(ARG_PAGEARRAY)。
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
rootView =(ViewGroup中)inflater.inflate(R.layout.activity_story,集装箱,FALSE); 的loadView(rootView);
返回rootView;
} 公众诠释getPageNumber(){
返回mPageNumber;
} //让我们继续修改逻辑片段
公共无效doTextViewChnges(){
最后查看视图= getView();
//你想你的看法和他的孩子什么
}
}
Need help to fix a code I saw in a http://stackoverflow.com/a/9275732/1938357. The solution I need is get the current view onany page in my viewpager based application
Below is the code for reference
public View getCurrentView(ViewPager pager) {
for (int i = 0; i < pager.getChildCount(); i++) {
View child = pager.getChildAt(i);
if (child.getX() <= pager.getScrollX() + pager.getWidth()
&& child.getX() + child.getWidth() >= pager.getScrollX() + pager.getWidth()) {
return child;
}
}
return getChildAt(0);
I am trying to access the current view in the view pager and used the code above. It works fine when I scroll forwards. But when I scroll backwards then after 2 backward scrolls this stops working.
Public class myActivity extends FragmentActivity
//Variable declarations
protected void onCreate(Bundle savedInstanceState) {
ViewPager mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
View CurrView;
OnPageChangeListener pageChangelistener = new OnPageChangeListener() {
@Override
public void onPageSelected(int pageSelected) {
doTextViewChnges();//access the text view and update it based on pageSelected
---THIS IS WHERE I AM STUCK IN TRYING TO GET THE TEXTVIEW IN MY CURRENT FRAGMWNT/VIEW-------
}
mPager.setOnPageChangeListener(pageChangelistener);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position, <other parameters I want to pass>);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
And below is my ScreenSlidePageFragment class
public class ScreenSlidePageFragment extends android.support.v4.app.Fragment {
public static final String ARG_PAGE = "pagenumber";
public static final String ARG_PAGEARRAY = "pages";
private int mPageNumber;
//Declare other variables
@SuppressLint("NewApi")
public static ScreenSlidePageFragment create(int pageNumber, String[] pages, int bookmarkPageNumber, String storyName, int linesInOnePage) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
args.putStringArray(ARG_PAGEARRAY, pages);
fragment.setArguments(args);
return fragment;
}
@SuppressLint("NewApi")
public ScreenSlidePageFragment() {
}
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
pages = getArguments().getStringArray(ARG_PAGEARRAY);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = (ViewGroup) inflater
.inflate(R.layout.activity_story, container, false);
//load layout with all components filled up for that page
loadView(rootView);
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
It is very hard to discuss in comments with example of code.
As I see in grepcode FragmentStatePagerAdapter.setPrimaryItem(), receive object
as android.support.v4.app.Fragment
. But you are receive ClassCastException
when trying to cast object as Fragment
. Can you check your casting: do you really cast object in android.support.v4.app.Fragment
not in android.app.Fragment
? If all right, add your exception message to your question, please.
UPDATE:
If your ScreenSlidePagerAdapter
has method getCurrentView(), then your method 'getCurrentView()' can work like this:
public View getCurrentView(ViewPager pager) {
return ((ScreenSlidePagerAdapter) pager.getAdapter()).getCurrentView();
}
And method getCurrentView()
of your ScreenSlidePagerAdapter
:
public View getCurrentView() {
return currView;
}
UPDATE 2:
I've made some modification with your code:
Your Activity with private Adapter:
public class myActivity extends FragmentActivity implements ISelectItemListener {
// Your variable declarations and methods
@Override
protected void onCreate(Bundle savedInstanceState) {
final ViewPager pager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
pager.setAdapter(mPagerAdapter);
pager.setOnPageChangeListener(
new OnPageChangeListener() {
@Override
public void onPageSelected(int pageSelected) {
final ScreenSlidePageFragment fragment = mPagerAdapter.getFragment(pageSelected);
if (fragment != null) {
fragment.doTextViewChnges();
}
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
});
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
//Your variable declarations
private final ScreenSlidePageFragment[] mFragments;
public ScreenSlidePagerAdapter(android.support.v4.app.FragmentManager fm) {
super(fm);
mFragments = new ScreenSlidePageFragment[NUM_PAGES];
}
@Override
public int getCount() {
return NUM_PAGES;
}
// It will create new ScreenSlidePageFragment by position if it's not exsist
@Override
public android.support.v4.app.Fragment getItem(int position) {
if (getFragment(position) == null) {
mFragments[position] = ScreenSlidePageFragment.create(position, <other parameters I want to pass>);
}
return getFragment(position);
}
// It will give you ScreenSlidePageFragment if it's exists, or null
public ScreenSlidePageFragment getFragment(int position) {
return mFragments[position];
}
}
}
Your Fragment:
public class ScreenSlidePageFragment extends Fragment {
//Your variables and methods
public static ScreenSlidePageFragment create(int pageNumber, String[] pages, int bookmarkPageNumber, String storyName, int linesInOnePage) {
final Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
args.putStringArray(ARG_PAGEARRAY, pages);
final ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
pages = getArguments().getStringArray(ARG_PAGEARRAY);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = (ViewGroup) inflater.inflate(R.layout.activity_story, container, false);
loadView(rootView);
return rootView;
}
public int getPageNumber() {
return mPageNumber;
}
// Let's keep logic of modifications in fragment
public void doTextViewChnges() {
final View view = getView();
// do what you want with your View and his children
}
}
这篇关于需要帮助修复code为获得在viewpager当前视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!