2013-08-23 8 views
1

응용 프로그램에 문제가 있습니다. 다음과 같은 응용 프로그램을 재현하려고합니다 : http://developer.android.com/training/animation/screen-slide.html 조각에 TextView가 있고, 시도하고 있습니다. 모든 조각에 대해 동일한 레이아웃을 사용하고 TextView에서 텍스트를 변경합니다. (1,2,3,4,5)하지만 setText 때 나는 호감을 얻었습니다.다른 프래그먼트에서 사용되는 레이아웃에서 TextView를 어떻게 변경할 수 있습니까?

package com.example.msixthapp; 

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.widget.TextView; 


public class ScreenSlidePagerActivity extends FragmentActivity { 
private Fragment a[] = new Fragment [10]; 
private static final int NUM_PAGES=5; 
public static TextView txt; 
private ViewPager viewPager; 
private PagerAdapter pagerAdapter; 

@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_screen_slide_pager); 

    viewPager = (ViewPager)findViewById(R.id.pager); 
    pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); 
    viewPager.setAdapter(pagerAdapter); 

    for(int i=0;i<NUM_PAGES;i++){ 
    a[i]= new ScreenSlidePageFragmen();  

    } 
} 

@Override 
public void onBackPressed(){ 
     if (viewPager.getCurrentItem() == 0) { 
      // If the user is currently looking at the first step, allow the system to handle the 
      // Back button. This calls finish() on this activity and pops the back stack. 
      super.onBackPressed(); 
     } else { 
      // Otherwise, select the previous step. 
      viewPager.setCurrentItem(viewPager.getCurrentItem() - 1); 
     } 

} 

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{ 
    public ScreenSlidePagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int position) {  
     return ScreenSlidePageFragmen.create(position); 
    } 

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

} 

}

package com.example.msixthapp; 

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

/** 
* A simple {@link android.support.v4.app.Fragment} subclass. 
* 
*/ 
public class ScreenSlidePageFragmen extends Fragment { 
public static String ARG_PAGE="page"; 
private int mPageNumber; 
public ScreenSlidePageFragmen() { 
    // Required empty public constructor 

} 
    public static ScreenSlidePageFragmen create(int pageNumber) { 
    ScreenSlidePageFragmen fragmen = new ScreenSlidePageFragmen(); 
    Bundle args = new Bundle(); 
    args.putInt(ARG_PAGE, pageNumber); 
    fragmen.setArguments(args); 
     return fragmen; 
    } 
    @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); 
    TextView txt = (TextView)rootView.findViewById(R.id.cont); 
// txt.setText(mPageNumber+1); 
// ((TextView)rootView.findViewById(R.id.content)).setText(mPageNumber+1); 
    TextView txte = new TextView(null); 
    txte.setText(mPageNumber); 
    rootView.addView(txte); 

    return rootView; 
} 

} ` 그리고 레이아웃

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".ScreenSlidePagerActivity" > 

<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" /> 

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/container" 
tools:context=".ScreenSlidePageFragmen" > 

<TextView 
    android:id="@+id/cont" 
    android:gravity="center" 
    android:textSize="30sp" 
    android:text="tst" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

답변

0

여기

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

ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_screen_slide_page, container,false); 
    TextView txt = (TextView)rootView.findViewById(R.id.cont); 
// txt.setText(mPageNumber+1); 
// ((TextView)rootView.findViewById(R.id.content)).setText(mPageNumber+1); 
    TextView txte = new TextView(getActivity()); 
    txte.setText(mPageNumber); 
    rootView.addView(txte); 

    return rootView; 
} 
+0

업데이트 여전히 작동하지 :(같은 충돌을 얻었다. –

관련 문제