如何在ViewPager的片段中更改活动视图?

时间:2013-06-05 11:56:41

标签: android android-fragments android-viewpager android-fragmentactivity

我需要更改放置在TextView' FragmentActivityViewPager布局中的ViewPagerAdapter文字。基本上我在我的应用程序中创建了一个操作栏,它显示了当前可见Fragment的名称。

所以我需要在TextView中的可见片段的每次更改时更改ViewPager(使用滑动动作或使用TabHost选择)。从我的搜索中我发现这种方法可以帮助我实现我的目标:

    @Override
public void setUserVisibleHint(boolean isVisibleToUser) 
{
    Log.d(TAG, "setUserVisibleHint invoked!");
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) 
    {
        if (odTab != null && getActivity() != null)
        {
            Log.d(TAG, "Currently visable tab: "+odTab.getTabTitle());
            Toast.makeText(getActivity(), "Currently visable tab: "+odTab.getTabTitle(), Toast.LENGTH_LONG).show();
            if (application.getCurrentDataSource() == DataSource.SSRS)
            {
                if (rsTab.getTabTitle().equals("")  || rsTab.getTabTitle() == null)
                {
                    tabName.setVisibility(View.GONE);
                }
                else
                {
                    tabName.setText(rsTab.getTabTitle());
                }
            }
            else if (application.getCurrentDataSource() == DataSource.SGRDL)
            {
                Log.d(TAG, "found Title textView from fragmentActivity");
                if (odTab.getTabTitle().equals("")  || odTab.getTabTitle() == null)
                {
                    ((TabsViewPagerFragmentActivity)getActivity()).findViewById(R.id.tvTabName).setVisibility(View.GONE);
                }
                else
                {
                    Log.d(TAG, "set Title textView to: "+odTab.getTabTitle());
                    ((TextView) ((TabsViewPagerFragmentActivity)getActivity()).findViewById(R.id.tvTabName)).setText(odTab.getTabTitle());
                }
            }
        }
    }   
}

我试图在我的片段中实现它,但由于某种原因,这一行:

((TextView) ((TabsViewPagerFragmentActivity)getActivity()).findViewById(R.id.tvTabName)).setText(odTab.getTabTitle());

没有做任何事情,虽然这一行确实会在日志中显示viewPager中片段的每次更改:

Log.d(TAG, "set Title textView to: "+odTab.getTabTitle());

更新: 我的适配器:

public class ViewPagerAdapter extends FragmentPagerAdapter 
{
private List<Fragment> fragments;

/**
 * @param fm
 * @param fragments
 */
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

/* (non-Javadoc)
 * @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
 */

@Override
public Fragment getItem(int position) {
    return this.fragments.get(position);
}

/* (non-Javadoc)
 * @see android.support.v4.view.PagerAdapter#getCount()
 */

@Override
public int getCount() {
    return this.fragments.size();
}

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}  
}

我做错了吗?还有另一种方法可以做这个操作吗?

3 个答案:

答案 0 :(得分:3)

在您的活动中,您可以:

viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int pos) {
                // Change your textview
            }


            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

答案 1 :(得分:1)

虽然我无法确定问题所在,但我建议您简化代码:

  1. 避免在演员阵容中施放。您在TabsViewPagerFragmentActivityif中都使用else。将此强制转换分配给参数并使用此参数。处理开销值得。
  2. 而不是使用findViewById,将R.id.tvTabName保存为TabsViewPagerFragmentActivity中的本地参数,并在setTitle(String title)中创建方法TabsViewPagerFragmentActivity。它还可以帮助您在活动本身调试此调用。
  3. 我同意@mromer - 尝试从活动中访问该片段。要实现此目的,请使FragmentPagerAdapter继承以下类:

    public abstract class SmartFragmentPagerAdapter extends FragmentPagerAdapter{
    
    private SparseArray<String> mFragmentTags;
    private FragmentManager mFragmentManager;
    
    public SmartFragmentPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        mFragmentManager = fragmentManager;
        mFragmentTags = new SparseArray<String>();
    }
    
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Object obj = super.instantiateItem(container, position);
        if (obj instanceof Fragment) {
            // record the fragment tag here.
            Fragment f = (Fragment) obj;
            String tag = f.getTag();
            mFragmentTags.put(position, tag);
        }
        return obj;
    }
    

    //在onPageSelected中使用此方法     public Fragment getFragment(int position){         String tag = mFragmentTags.get(position);         if(tag == null)             return null;         return mFragmentManager.findFragmentByTag(tag);     }

    public FragmentManager getFragmentManager(){
        return mFragmentManager;
    }
    

    }

答案 2 :(得分:0)

我最终设法处理这个问题的方式与以下几点相结合:

1。首先我在片段中声明了一个接口:

public interface OnFragmetVisibleListener{
    public void fragmetVisible(boolean visible);
}

2. 接下来我将这个函数添加到片段中以了解它何时附加到ViewPager

@Override 
public void onAttach(Activity activity){ 
    super.onAttach(activity);
    try{
        onFragmetVisibleListener = (OnFragmetVisibleListener) activity;
        isAttached = true; //flag for whether this framgnet is attache to pager
    } catch (ClassCastException e){
        throw new ClassCastException(activity.toString() + " must implement interface OnFragmetVisibleListener");
    }
}

3。我还添加了以下方法,以了解片段何时可​​见并在Application类中设置其数据对象:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) 
{
    Log.d(TAG, "setUserVisibleHint invoked!");
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser) 
    {
        if (odTab != null && getActivity() != null)
        {
            Log.d(TAG, "Currently visable tab: "+odTab.getTabTitle());
            application.currentVisibleTab = odTab;
        }
        if (isAttached)
        { 
            onFragmetVisibleListener.fragmetVisible(true);
        }
    }   
}

4. 在我的ViewPager活动中,我让它在步骤1中实现已定义的界面:

public class TabsViewPagerFragmentActivity extends FragmentActivity implements GridFragment.OnFragmetVisibleListenerr

5. 接下来我添加了未实现的方法:

@Override
public void fragmetVisible(boolean visible) 
{
    if(visible)
    {
        setCurrentTabTitle(application.currentVisibleTab.getTabTitle());
    }
}
setCurrentTabTitle为:

时,

6。

 public void setCurrentTabTitle(String title)
{
    tvTabTitle.setText(title); //my TextView that's needs to be changed.
    Log.d(TAG, "set tab title from activity: "+title);
}

就是这样,这就是我根据ViewPager的可见片段更改活动视图的方式。