如何在viewpager页面之间拆分文本?

时间:2014-01-31 09:44:44

标签: android textview android-viewpager

我在每页上都有带ViewView的ViewPager。

我想使用ViewPager实现类似书籍的东西。如何确定每页上可以容纳多少行文本?每个页面都必须没有滚动。

LogsPagerAdapter pagerAdapter = new LogsPagerAdapter(charSequence);
            mViewPagerFLog.setAdapter(pagerAdapter);





private class LogsPagerAdapter extends PagerAdapter {
    CharSequence charSequence;
    int pageNum;
    public LogsPagerAdapter(CharSequence charSequence) {
        super();
        this.charSequence = charSequence;
        pageNum = charSequence.length()/1000;
    }

    @Override
    public Object instantiateItem(View collection, int position) {
        LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.log_text, null);          
        TextView logText = (TextView) view.findViewById(R.id.logs_text);
        logText.setText(charSequence);
        ((ViewPager) collection).addView(view, 0);
        return view;
    }

    @Override
    public void destroyItem(View collection, int position, Object view) {
        ((ViewPager) collection).removeView((View) view);
    }

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}

R.lauyout.logs_text:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/logs_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

0 个答案:

没有答案