java - 为什么我不能动态更新 fragment 中的文本?

标签 java android android-layout android-fragments android-adapter

这是我的 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代码 fragment

 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>

我如何动态更改文本和图像,我还有三个使用相同布局(pager_layout)的 fragment ,我是否可以添加不同的 Intent ?

最佳答案

您必须在 onViewCreated() 方法内的 TextView 中设置文本,而不是在 fragment 类的 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");
    ...
}

关于java - 为什么我不能动态更新 fragment 中的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603565/

相关文章:

android - 在 Android 中的同一 Activity 中使用两个 Volley 请求时如何修复空指针异常?

android - 使用 GridLayout 弄乱计算器

android - 为什么应用程序崩溃,当我尝试通过自定义样式设置文本大小属性时

java - Cassandra Datastax 驱动程序 - 连接池

java - 在 Java 中生成 Get 查询字符串

android - 使用 Facebook SDK 4.1 在 Facebook 上共享带有文本故事的图像

android - 在xml文件中添加标签栏

Java Swing GridBagLayout - 添加没有空格的按钮

java - JPA CascadeType.ALL 不删除孤儿

java - 删除水平进度条中的填充