마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

fragment에 textswitcher

0 추천
MainActivity.java

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {


   
    SectionsPagerAdapter mSectionsPagerAdapter;

  
    ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sample_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
         
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
                              FragmentTransaction fragmentTransaction) {
      
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
                                FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
                                FragmentTransaction fragmentTransaction) {
    }

  
    public class SectionsPagerAdapter extends FragmentPagerAdapter {
        Context mContext;

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
          
            switch(position) {
                case 0:
                    return new Tab1(mContext);
                case 1:
                    return new Tab2(mContext);

            }
            return null;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
                case 0:
                    return getString(R.string.title_section1).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);

            }
            return null;
        }
    }

  
    public static class DummySectionFragment extends Fragment {
      
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main_dummy,
                    container, false);
            TextView dummyTextView = (TextView) rootView
                    .findViewById(R.id.section_label);
            dummyTextView.setText(Integer.toString(getArguments().getInt(
                    ARG_SECTION_NUMBER)));
            return rootView;
        }
    }

}

 

Tab1

public class Tab1 extends Fragment {
    Context mContext;

    public Tab1(Context context) {
        mContext = context;
    }
    private TextSwitcher mSwitcher;
    Button button1;

   
    String textToShow[]={"Main HeadLine","Your Message","New In Technology","New Articles","Business News","What IS New"};
    int messageCount=textToShow.length;
    int currentIndex=-1;

    @Override
   public View onCreateView(LayoutInflater inflater,
                            ViewGroup container,Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.activity_tab1, null);
        super.onCreate(savedInstanceState);


        button1=(Button)view.findViewById(R.id.button1);
        mSwitcher = (TextSwitcher) view.findViewById(R.id.textSwitcher);
mSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
    @Override
    public View makeView() {
        TextView myText = new TextView(Tab1.this);
        myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        myText.setTextSize(36);
        return myText;
    }
});
      
       
        button1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                currentIndex++;
                // If index reaches maximum reset it
                if (currentIndex == messageCount)
                    currentIndex = 0;
                mSwitcher.setText(textToShow[currentIndex]);
            }
        });
        return view;

    }
}

Horizontal pazing 에서 tab1 activity에 textswitcher를 사용하려합니다.

근데 저 위에 빨간글씨처럼 에러가 뜨는데 어찌해야하나요

익명사용자 님이 2015년 6월 3일 질문

1개의 답변

0 추천

저위에 빨간글씨처럼 뜨는게 어디를 말하는거죠? 

TextView myText = new TextView(<span style="color:#ff0000;">Tab1.this</span>);

여기 말하는건가요? Tab1.this 를 mContext로 바꿔보세여

그리고 위 내용이 맞다면 TextSwitcher랑은 아무런 상관이 없습니다.

 

Gradler (109,780 포인트) 님이 2015년 6월 4일 답변
...