更改页面后Android片段活动更改内容

时间:2014-04-12 04:00:57

标签: android android-fragments android-viewpager

我有具有视图寻呼机的片段活动,我希望每个更改页面的内容都以编程方式进行更改。 `

package com.idroid.splashscreen;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class ScreenSlidePagerActivity extends FragmentActivity {

    private static final int NUM_PAGES = 5;
    private ViewPager mPager;
    private PagerAdapter mPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_slide);
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        OnPageChangeListener pageChangeListener=null;

         pageChangeListener = new OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int arg0) { }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) { }

            @Override
            public void onPageSelected(int position) {
                 Toast.makeText(getApplicationContext()
                         .getApplicationContext(), 
                         "page "+position,
                         Toast.LENGTH_LONG).show();

               TextView txt=(TextView) findViewById(R.id.txtcoba);
               txt.setText("page "+position);

            }
        };
        mPager.setOnPageChangeListener(pageChangeListener);

    }

    @Override
    public void onBackPressed() {
        if (mPager.getCurrentItem() == 0) {
           super.onBackPressed();
        } else {
            mPager.setCurrentItem(mPager.getCurrentItem() - 1);
        }
    }
    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int position) {

            return new ScreenSlidePageFragment();
        }

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



}

`

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/txtcoba"
         style="?android:textAppearanceMedium"
        android:padding="16dp"
        android:lineSpacingMultiplier="1.2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="coba" />
</ScrollView>

我在页面选择的方法上更改它并更改文本视图的内容。为了祝酒它的工作,但文本没有改变。但在第4页,当我回到第3页时,第3页显示页面。但另一页没有。

我该怎么办?谢谢你。这是ScreenSlidePageFragment类

包com.idroid.splashscreen;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



public class ScreenSlidePageFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_screen_slide_page, container, false);

        return rootView;
    }
}

这是fragment_screen_slide_page.xml

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/txtcoba"
         style="?android:textAppearanceMedium"
        android:padding="16dp"
        android:lineSpacingMultiplier="1.2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="coba" />
</ScrollView>

这是activity_screen_slide.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

1 个答案:

答案 0 :(得分:0)

问题是您尝试设置TextView布局的Fragment。 根据您的代码,TextView位于ScreenSlidePageFragment布局中。

删除onPageeSelcted

中的'TextView'setText

像这样更改ScreenSlidePageFragment

 private class ScreenSlidePageFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View row =  inflater.inflate(R.layout.activity_screen_slide, container, false);

        TextView txt=(TextView) row.findViewById(R.id.txtcoba);
        txt.setText("page using fragment");
        return row;
    }

 }