1

我需要更改TextView'放置在FragmentActivity具有 aViewPager和 a的布局中的 s 文本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;
}  
}

难道我做错了什么?还有其他方法可以执行此操作吗?

4

3 回答 3

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

            }
        });
于 2013-06-05T12:04:54.860 回答
1

虽然我无法查明问题,但我建议简化您的代码:

  1. 避免投中投。您TabsViewPagerFragmentActivity同时使用 inifelse. 将此强制转换分配给参数并使用此参数。处理开销值得。
  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;
    }

    // use this method in 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;
    }
}
于 2013-06-05T13:54:34.980 回答
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());
    }
}

6.什么时候setCurrentTabTitle

 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.

于 2013-06-26T10:13:47.517 回答