ViewPager
,以便在每个页面上显示不同的文本,就像您正在浏览书籍中的页面一样。它的设置方式是,它们有两个XML文件(一个在Fragment Activity类中实现ViewPager元素,一个在Fragment类中有一个textview),在每个页面上显示Lorem Ipsum。我正在尝试找到一种方法来更改Fragment类中的textview,这样当我转到第2,3,4页等时,每页上都会有不同的文本。
在ScreenSlideActivity
类中,我尝试在onPageScrollStateChanged(int position)
内部事件处理程序中使用切换,将某些文本视图设置为可见/不可见。我还尝试在onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState)方法的ScreenSlidePageFragment类中使用一个开关。这是正确的方法还是我离开了?我肯定会忽视一些事情,有些人可以指出我正确的方向吗?
这是使用Eclipse定位API 14到17的构建。下面是两个类中的一些代码,以及带有textview的XML文件:
//片段活动类//
public class ScreenSlideActivity extends FragmentActivity {
/**
* The number of pages (wizard steps) to show in this demo.
*/
private static final int NUM_PAGES = 5;
/**
* The pager widget, which handles animation and allows swiping horizontally to access previous
* and next wizard steps.
*/
private ViewPager mPager;
/**
* The pager adapter, which provides the pages to the view pager widget.
*/
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Instantiate a ViewPager and a PagerAdapter.
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When changing pages, reset the action bar actions since they are dependent
// on which page is currently active. An alternative approach is to have each
// fragment expose actions itself (rather than the activity exposing actions),
// but for simplicity, the activity provides the actions in this sample.
invalidateOptionsMenu();
}
});
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
//片段类//
public class ScreenSlidePageFragment extends Fragment {
/**
* The argument key for the page number this fragment represents.
*/
public static final String ARG_PAGE = "page";
/**
* The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
*/
private int mPageNumber;
/**
* Factory method for this fragment class. Constructs a new fragment for the given page number.
*/
public static ScreenSlidePageFragment create(int pageNumber) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public ScreenSlidePageFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_screen_slide_page, container, false);
// Set the title view to show the page number.
((TextView) rootView.findViewById(android.R.id.text1)).setText(
getString(R.string.title_template_step, mPageNumber + 1));
return rootView;
}
/**
* Returns the page number represented by this fragment object.
*/
public int getPageNumber() {
return mPageNumber;
}
}
// XML layout //
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Dummy content. -->
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView android:id="@android:id/text1"
style="?android:textAppearanceLarge"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp" />
<TextView
android:id="@+id/lorem_ipsum1"
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lineSpacingMultiplier="1.2"
android:text="@string/lorem_ipsum" />
</LinearLayout>
答案 0 :(得分:0)
问题已解决,我必须在ScreenSlidePageFragment
内创建一个switch语句,关闭/打开可见性(VISIBILE
或GONE
),具体取决于mPageNumber
变量(即页码确定为该页面设置的字符串值)。这是一项黑客工作,效率低下,还是很好?最重要的是,它的工作原理。所有字符串都保存在strings.xml中。同样,不确定这是否适用于较大的字符串。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_screen_slide_page, container, false);
// Set the title view to show the page number.
((TextView) rootView.findViewById(android.R.id.text1)).setText(
getString(R.string.title_template_step, mPageNumber + 1));
switch(mPageNumber){
case 0:
rootView.findViewById(R.id.lorem_ipsum1).setVisibility(View.VISIBLE);
rootView.findViewById(R.id.lorem_ipsum2).setVisibility(View.GONE);
rootView.findViewById(R.id.lorem_ipsum3).setVisibility(View.GONE);
break;
case 1:
rootView.findViewById(R.id.lorem_ipsum1).setVisibility(View.GONE);
rootView.findViewById(R.id.lorem_ipsum2).setVisibility(View.VISIBLE);
rootView.findViewById(R.id.lorem_ipsum3).setVisibility(View.GONE);
break;
case 2:
rootView.findViewById(R.id.lorem_ipsum1).setVisibility(View.GONE);
rootView.findViewById(R.id.lorem_ipsum2).setVisibility(View.GONE);
rootView.findViewById(R.id.lorem_ipsum3).setVisibility(View.VISIBLE);
break;
}
return rootView;
}