게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
안드로이드) fragment에 textswitcher 도와주세요
게시물ID : programmer_10802짧은주소 복사하기
작성자 : andgate
추천 : 0
조회수 : 345회
댓글수 : 0개
등록시간 : 2015/06/04 01:41:10
일단 horizonal paging에 탭2개로 각각 activity(Tab1,Tab2) 만들었구요

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);

final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());

mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position); }
});

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);
}

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() {
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; }
}
}

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; }
}

전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호