为什么不能我动态地在碎片中更新文本?

问题描述:

这里是我的XML代码为什么不能我动态地在碎片中更新文本?

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

> 

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 


     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" 
      android:text="Well heres the secret !" 
      android:textAlignment="center" 
      android:textColor="@color/bluishblack" 
      android:textSize="25sp" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="8dp" 
      android:text="1. Secret is simple,the cards we showed you contained key numbers(very first number of the list)" 
      android:textAlignment="center" 
      android:textSize="23sp" /> 


     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="' 2 , 4 , 1 , 16 , 32 , 8 , 64 ' " 
      android:textAlignment="center" 
      android:textColor="@color/dgreen" 
      android:textSize="23sp" /> 


     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="16dp" 
      android:text="2. Above are the key numbers. For every card that contains spectator's number you add key number(First number of card) of respective card. " 
      android:textAlignment="center" 
      android:textSize="23sp" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" 
      android:text="3. And thats how you Read People's mind ! As easy as that ! " 
      android:textAlignment="center" 
      android:textSize="23sp" /> 

     <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"> 

      <Button 
       android:id="@+id/play_again" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="8dp" 
       android:onClick="playAgain" 
       android:text="Play again !" 
       android:textColor="@color/dgreen" 

       /> 
     </RelativeLayout> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="16dp"> 

       <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:orientation="vertical"> 

        <ImageView 
         android:id="@+id/imageView" 
         android:layout_width="match_parent" 
         android:layout_height="150dp" 
         android:layout_marginBottom="8dp" 
         android:background="@drawable/googleg_standard_color_18" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_margin="8dp" 
         android:text="Your rating helps us to put more efforts i future devlopment,also we get to know more aboout your experiences and Views ! " 
         android:textAlignment="center" 
         android:textSize="20sp" /> 

        <Button 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="99dp" 
         android:text="Rate now !" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:text="Still Here? Do you want to amaze your friends and family.There are many awesome tricks which will blow away peoples mind yet so simple to do See below recommendations now " 
         android:textAlignment="center" 
         android:textSize="20sp" 

         /> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginBottom="8dp" 
         android:text="Do Want to amaze your Freinds !!" 
         android:textAlignment="center" 
         android:textColor="@android:color/black" 
         android:textSize="26sp" /> 


        <RelativeLayout 
         android:layout_width="match_parent" 
         android:layout_height="2dp" 
         android:layout_marginLeft="12dp" 
         android:layout_marginRight="12dp" 
         android:background="@android:color/darker_gray" /> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginStart="6dp" 
         android:layout_marginTop="8dp" 
         android:text="Here are some of our Suggestions :- " 
         android:textColor="@color/colorPrimaryDark" 
         android:textSize="16sp" /> 


        <RelativeLayout 
         android:layout_width="match_parent" 
         android:layout_height="2dp" 
         android:layout_margin="8dp" 
         android:layout_marginLeft="12dp" 
         android:layout_marginRight="12dp" 
         android:background="@android:color/darker_gray" /> 

        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_gravity="center" 
         android:layout_margin="8dp" 
         android:text="Swipe to see more --->" 
         android:textAlignment="center" 
         android:textSize="22sp" /> 


       </LinearLayout> 


      </RelativeLayout> 


     </ScrollView> 

     <android.support.v4.view.ViewPager 
      android:id="@+id/view_pager" 
      android:layout_width="match_parent" 
      android:layout_height="500dp"> 

     </android.support.v4.view.ViewPager> 

    </LinearLayout> 


</ScrollView> 

这里的片段我的Java代码

public class Fragment1 extends Fragment { 

    private TextView tv; 



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


     Intent n = new  Intent(Intent.CATEGORY_BROWSABLE,Uri.parse("http://amzn.to/2rpR35P")); 



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

     tv = (TextView)rootView.findViewById(R.id.textDes); 

     tv.setText("text to set dynamically"); 



     return inflater.inflate(R.layout.pager_layout, container, false); 
    } 


    } 




Here's code for pager Adapter 




     public class PagerAdapter extends FragmentPagerAdapter { 

     public PagerAdapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 

      if (position == 0) { 
       return new Fragment1(); 
      } 
      if (position == 1) { 
       return new Fragment2(); 
      } 
      if (position == 2) { 
       return new Fragment3(); 
      } else { 
       return new Fragment4(); 
      } 


     } 


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

     } 

这里的寻呼机

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="24dp" 
    > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_icon" 
     android:id="@+id/image_book" 
     /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="16dp" 
    > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAlignment="center" 
     android:text="This is description" 
     android:textSize="20sp" 
     android:id="@+id/textDes" 
     /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    > 

    <Button 
     android:id="@+id/getItNow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Start amazing your friends !" 
     android:textAlignment="center" 

     /> 

</LinearLayout> 


</LinearLayout> 

如何更改文本和图像的布局代码动态地,我还有三个片段使用相同的布局t(pager_layout)也可以添加不同的意图或不?

你必须设置文本里面onViewCreated()方法的文本视图,而不是在onCreateView()在你的片段类:

@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.pager_layout, container, false); 
    return view; 
} 

TextView tv; 

@Override 
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    tv = (TextView) view.findViewById(R.id.textDes); 
    tv.setText("text to set dynamically"); 
    ... 
}