如何在ViewPager中更改特定页面的textView的文本?

时间:2019-07-07 21:29:39

标签: android android-studio

如何在ViewPager中更改特定页面的textView的文本? 我想更改第二页(位置1)的文本。 我尝试在InstantiateItem中进行调整,但它更改了最后一页或第一页。

@NonNull
@Override
public Object instantiateItem(@NonNull final ViewGroup container, final int position) {

    layoutInflater = layoutInflater.from(context);
    View view = layoutInflater.inflate(R.layout.item_menu, container, false);
    reponse1 = view.findViewById(R.id.text1);


    reponse1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                reponse1.setText("FAUX");

        }
    });
    return view;
}

在我的主要内容中:

private void init_adapter(){
    modelList = new ArrayList<>();
    //Inflate
    modelList.add(new Model(R.drawable.photo0, "Commencer", "", "", "","", 0, ""));
    modelList.add(new Model(R.drawable.photo1, "Ose faire ______ ce que tu redoutais hier.", "Ose faire aujourd'hui ce que tu redoutais hier", "Demain", "Aujourd'hui", "Dans un ans", 2, "01"));
    modelList.add(new Model(R.drawable.photo2, "Ton temps est _____, nattends plus.", "Ton temps est limité, n'attends plus.", "Infinie", "Limité", "Gratuit", 2, "02"));


    adapter = new Adapter(modelList, this);
    viewPager = findViewById(R.id.viewPager);
    viewPager.setAdapter(adapter);
}

1 个答案:

答案 0 :(得分:0)

您必须在modelList中的模型内部更改值,然后调用defaults write -g CGFontRenderingFontSmoothingDisabled -bool NO

如果您位于片段内部,则可以使用接口或类似EventBus的事件将事件通知给父活动。 作为实例:

片段

defaults write com.microsoft.VSCode.helper CGFontRenderingFontSmoothingDisabled -bool NO

}

MainActivity:

adapter.notifyDatasetChanged();

片段适配器:

public class YourFragment extends Fragment {

public YourFragment() {
    // Required empty public constructor
} 

Model model;
int position;
public static YourFragment newInstance(Model model , int position) {
    YourFragment fragment = new YourFragment();
    Bundle args = new Bundle();
    args.putSerializable("model" , model);
    args.putInt("position"   , position);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        model  = getArguments().getSerializable("model");
        position  = getArguments().getInt("position");
    }
}

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


    //initViews  
    reponse1.setOnClickListener((View.OnClickListener) v -> {
        String newText = "SomeThing";
        MainActivity.listener.onItemClicked(position , newText);
    }); 
    return  view;
}