首页 > 解决方案 > 在从片段单击按钮时更改 ViewPager2 内 textview 中的文本

问题描述

我有一个包含 viewPager2 和几个按钮的片段... ViewPager2 包含一个 textView,我想在按钮单击时更改其文本(存在于片段内部而不是 viewPager2)。

界面

public interface IUpdateCursor {

    void cursorOn_nextWord();
    void cursorOn_nextLine();
    void cursorOn_nextTextBlock();

    void cursorOn_previousWord();
    void cursorOn_previousLine();
    void cursorOn_previousTextBlock();
}

分段

public class Read_fragment extends Fragment implements View.OnClickListener {

    private ImageButton imgbtn_back,imgbtn_left_top, imgbtn_right_top, imgbtn_left_down, imgbtn_right_down;
    private Button btn_done, btn_center_top, btn_center_down;
    private ViewPager2 viewPager;
    com.example.vision.HighlightedTextView textView;

    public Read_fragment() {
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_read, container, false);
        initViews(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager.setAdapter(new ReadViewPagerAdapter(getContext()));
//        displayOCRText(getAllImages(Application_TEMP_DIR,
//                getFilenameFilter(new String[]{"jpeg"})));
    }

    private void initViews(@NonNull View view) {
        imgbtn_back = view.findViewById(R.id.imgbtn_back);
        imgbtn_left_top = view.findViewById(R.id.imgbtn_left_top);
        imgbtn_right_top = view.findViewById(R.id.imgbtn_right_top);
        imgbtn_left_down = view.findViewById(R.id.imgbtn_left_down);
        imgbtn_right_down = view.findViewById(R.id.imgbtn_right_down);
        btn_done = view.findViewById(R.id.btn_done);
        btn_center_top = view.findViewById(R.id.btn_center_top);
        btn_center_down = view.findViewById(R.id.btn_center_down);

        viewPager = view.findViewById(R.id.viewPager);
        textView = view.findViewById(R.id.textView5);

        imgbtn_back.setOnClickListener(this);
        imgbtn_left_top.setOnClickListener(this);
        imgbtn_right_top.setOnClickListener(this);
        imgbtn_left_down.setOnClickListener(this);
        imgbtn_right_down.setOnClickListener(this);
        btn_done.setOnClickListener(this);
        btn_center_top.setOnClickListener(this);
        btn_center_down.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.imgbtn_back       :{/*go back to preview without saving changes.*/navigateTo(R.id.action_read_fragment_to_preview_fragment);}break;
            case R.id.imgbtn_left_top   :{      /*previous word or alphabet.*/ imgbtn_left_top_CLICK();       }break;
            case R.id.imgbtn_right_top  :{      /*next word or alphabet.*/ imgbtn_right_top_CLICK();       }break;
            case R.id.imgbtn_left_down  :{      /*next sentence or paragraph*/  imgbtn_left_down_CLICK(view);      }break;
            case R.id.imgbtn_right_down :{      /*previous sentence or paragraph*/  imgbtn_right_down_CLICK(view);      }break;
            case R.id.btn_done          :{      /*apply changes and go back to preview.*/        }break;
            case R.id.btn_center_top    :{      /*mark in on first click, mark out on second click, then show cut, copy paste options.*/        }break;
            case R.id.btn_center_down   :{      /*Read from cursor location to end of page.*/        }break;
        }
    }

    private void navigateTo(@IdRes int actionResId){
        final NavController navController = Navigation.findNavController(Objects.requireNonNull(getActivity()), R.id.container);
        navController.navigate(actionResId);
    }

    private void imgbtn_left_top_CLICK(){
//        UpdateCursor.setUpdateCursor();
//        iUpdateCursor.updateCursor(false,true);
        IUpdateCursor iUpdateCursor = (IUpdateCursor) this.getContext();
        iUpdateCursor.cursorOn_previousWord();
    }

    private void imgbtn_right_top_CLICK(){
//        this.iUpdateCursor = (IUpdateCursor) viewPager.getRootView();
//        iUpdateCursor.updateCursor(true,true);

        IUpdateCursor iUpdateCursor = (IUpdateCursor) this.getContext();
        iUpdateCursor.cursorOn_nextWord();
    }

    private void imgbtn_left_down_CLICK(View view){
//        this.iUpdateCursor = (IUpdateCursor) view;
//        iUpdateCursor.updateCursor(false,false);
        IUpdateCursor iUpdateCursor = (IUpdateCursor) this;
        iUpdateCursor.cursorOn_previousLine();
    }

    private void imgbtn_right_down_CLICK(View view){
//        this.iUpdateCursor = (IUpdateCursor) view;
//        iUpdateCursor.updateCursor(true,false);
        IUpdateCursor iUpdateCursor = (IUpdateCursor) this;
        iUpdateCursor.cursorOn_nextLine();
    }
}

ViewPager 适配器

public class ReadViewPagerAdapter extends RecyclerView.Adapter<ReadViewPagerAdapter.ViewHolder>  {

    private LayoutInflater mInflater;
    private Vector<Bitmap> images;

    public ReadViewPagerAdapter(Context context) {
        this.mInflater = LayoutInflater.from(context);
        this.images = new Vector<>(5);
        this.images = getAllImages(Application_TEMP_DIR,
                getFilenameFilter(new String[]{"jpeg"}));

    }

    @NonNull
    @Override
    public ReadViewPagerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.list_item_view_pager_read, parent, false);
        return new ReadViewPagerAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ReadViewPagerAdapter.ViewHolder myHolder, int position) {
        //TODO: set image in imageView.
        Bitmap bitmap = images.get(position);
        new OCR(myHolder).displayOCRText(bitmap);
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder implements OCRcallbacks, IUpdateCursor{

        com.example.vision.HighlightedTextView textView;
        FirebaseVisionText OCRText;

        ViewHolder(View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView5);
        }

        @Override
        public void setText(FirebaseVisionText texts) {
            this.OCRText = texts;
            this.textView.setText(this.OCRText.getText());
            this.textView.setVisionText(texts);
        }


        @Override
        public void cursorOn_nextWord() {
            this.textView.cursorOn_nextWord();
        }

        @Override
        public void cursorOn_nextLine() {
            this.textView.cursorOn_nextLine();
        }

        @Override
        public void cursorOn_nextTextBlock() {
            this.textView.cursorOn_nextTextBlock();
        }

        @Override
        public void cursorOn_previousWord() {
            this.textView.cursorOn_previousWord();
        }

        @Override
        public void cursorOn_previousLine() {
            this.textView.cursorOn_previousLine();
        }

        @Override
        public void cursorOn_previousTextBlock() {
            this.textView.cursorOn_previousTextBlock();
        }
    }
}

请帮助我。如果可能的话,请让我深入了解作为侦听器和回调的接口实现以及接口如何充当引用。

标签: javaandroid

解决方案


我有类似的问题。我通过在我的适配器和适配器构造函数中保留一个 parentView 指针来解决它,我添加了一个名为“parentView”(类型:viewGroup)的参数来设置它。

所以每次我需要访问父按钮/我使用的另一个视图时:(您还可以添加一个点击监听器 - 我在下面添加一个示例)

parentView.findViewById(R.id.Parent-View-Name);

在我的适配器中。


这就是它在您的情况下应该如何工作:

适配器

步骤1

 private ViewGroup parentView; // Add new Var
 // change/add new constracotr 
 public ReadViewPagerAdapter(Context context,ViewGroup parent) {
    this.mInflater = LayoutInflater.from(context);
    //******
    this.parentView = parent;
    //******
    this.images = new Vector<>(5);
    this.images = getAllImages(Application_TEMP_DIR,
            getFilenameFilter(new String[]{"jpeg"}));

}

第 2 步 当你想找到你的按钮时,只需使用“parentView.findViewById”

(ImageButton)parentView.findViewById(R.id.fragment_button).setOnClickListener(v1 -> {// Put Here what ever you want to happen when click happen}

我希望我能帮助:) 让我知道


推荐阅读