Viewpager设置文本功能

时间:2017-04-25 07:52:07

标签: android android-viewpager settext

我想在ViewPager页面中添加类别栏。类别标题根据ViewPager页面更改而更改。虽然我试图这样做ViewPager工作正常,但标题没有改变。

这是页面布局:

enter image description here

我在PagerAdapter中添加了以下代码:

private class MyPagerAdapter extends FragmentPagerAdapter {


public MyPagerAdapter(FragmentManager fm) {

    super(fm);
}

@Override
public Fragment getItem(int pos) {

    TextView cate_title = (TextView) getActivity().findViewById(R.id.cate_title);
    String title_name = "";
    switch(pos) {
        case 0:
            title_name = "MACARONS";
            cate_title.setText(title_name);
            return new SP_macarons();
        case 1:
            title_name = "CHOCOLATE";
            cate_title.setText(title_name);
            return new SP_chocolate();
        case 2:
            title_name = "CHEESE MOUSSE";
            cate_title.setText(title_name);
            return new SP_cheese_mousse();
        default:
            break;
    }
return  null;
}

@Override
public int getCount() {
    return 3;
}

这是Fragment代码

public class SpecialCake extends Fragment {
private ViewPager viewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.special_cake, container, false);


    final ViewPager pager = (ViewPager)view.findViewById(R.id.cate_pager);
    pager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));

    ImageButton prebtn = (ImageButton) view.findViewById(R.id.leftarrow);
    prebtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            pager.setCurrentItem(getItem(-1), true); //getItem(-1) for previous
        }
        private int getItem(int i) {
            return pager.getCurrentItem() + i;
        }
    });
    ImageButton nxtbtn = (ImageButton) view.findViewById(R.id.rightarrow);
    nxtbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            pager.setCurrentItem(getItem(+1), true); //getItem(-1) for previous
        }
        private int getItem(int i) {
            return pager.getCurrentItem() + i;
        }
    });

    return view;
}

2 个答案:

答案 0 :(得分:1)

您可以拥有OnPageChangeListener

最初将标题设置为MACARONS,然后在页面更改时更改标题。

textView.setTitle("MACRONS);

然后

pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
     String title_name = "";
     // depending on the position do something
     if(position==0)
     {
         title_name = "MACARONS";
     }
     else if(position==1)
     {
         title_name = "CHOCOLATE";
     }
     else
     {
         title_name = "MACARONS";
     }
     // Now set title to text
     textView.setTitle(title_name);
    }

答案 1 :(得分:0)

您可以在viewpager fragment中使用setUserVisibleHint来确定用户是否可以看到它。

表格文件:

  

为系统设置有关此片段的UI当前是否正确的提示   对用户可见。此提示默认为true且持久   跨片段实例状态保存和恢复。

然后将TextView TextView cate_title = (TextView) getActivity().findViewById(R.id.cate_title);添加到您的片段的布局中。

并添加以下代码:

 public class SP_macarons extends Fragment {

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            title_name = "MACARONS";
            cate_title.setText(title_name);   //card_title should be defined in your fragment 
        }
        else {
        }
    }
}