gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:51:15 25 4
gpt4 key购买 nike

这是我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com