Java 更改文本视图中的文本在ViewPager2上的按钮上单击“从片段”

Java 更改文本视图中的文本在ViewPager2上的按钮上单击“从片段”,java,android,Java,Android,我有一个包含viewPager2和几个按钮的片段。。。ViewPager2包含一个textView,我想在单击按钮时更改其文本(出现在片段中而不是ViewPager2) 接口 public interface IUpdateCursor { void cursorOn_nextWord(); void cursorOn_nextLine(); void cursorOn_nextTextBlock(); void cursorOn_previousWord()

我有一个包含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();
    }
}
可视寻呼机适配器

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();
        }
    }
}
公共类ReadViewPagerAdapter扩展了RecyclerView.Adapter{
私人停车场;
私有向量图像;
公共ReadViewPagerAdapter(上下文){
this.mInflater=LayoutInflater.from(上下文);
this.images=新向量(5);
this.images=getAllImages(应用程序临时目录,
getFilenameFilter(新字符串[]{“jpeg”});
}
@非空
@凌驾
public ReadViewPagerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup父级,int-viewType){
视图视图=最小充气器。充气(R.layout.list\u item\u View\u pager\u read,parent,false);
返回新的ReadViewPagerAdapter.ViewHolder(视图);
}
@凌驾
public void onBindViewHolder(@NonNull ReadViewPagerAdapter.ViewHolder myHolder,int位置){
//TODO:在imageView中设置图像。
位图=图像。获取(位置);
新OCR(myHolder)。显示OCRText(位图);
}
@凌驾
public int getItemCount(){
返回图像。size();
}
类ViewHolder扩展了RecyclerView。ViewHolder实现OCRcallbacks、IUpdateCursor{
com.example.vision.HighlightedTextView textView;
FirebaseVisionText文本;
ViewHolder(视图项视图){
超级(项目视图);
textView=itemView.findViewById(R.id.textView5);
}
@凌驾
public void setText(FirebaseVisionText文本){
this.OCRText=文本;
this.textView.setText(this.OCRText.getText());
this.textView.setVisionText(文本);
}
@凌驾
公共无效cursorOn_nextWord(){
this.textView.cursorOn_nextWord();
}
@凌驾
公共无效光标下一行(){
this.textView.cursorOn_nextLine();
}
@凌驾
公共无效游标n_nextTextBlock(){
this.textView.cursorOn_nextTextBlock();
}
@凌驾
公共无效游标{
this.textView.cursorOn_previousWord();
}
@凌驾
公共无效光标(上一行){
this.textView.cursorOn_previousLine();
}
@凌驾
public void cursorOn_previousTextBlock(){
this.textView.cursorOn_previousTextBlock();
}
}
}
请帮帮我。如果可能的话,请让我深入了解作为侦听器和回调的接口实现,以及接口如何作为引用。

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

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

在我的适配器中


在您的案例中,它应该是这样工作的:

适配器

步骤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"}));

}

我希望我能帮上忙:)让我知道

但我想知道是否可以使用接口来完成。嗨,很高兴我帮了:)关于使用接口。据我所知,接口是抽象类,与UI无关。希望我能帮忙:)是的。我知道。我想知道引用在其中是如何工作的,就像你在视图的引用中输入引用,然后我们输入使用该引用的参数,然后它返回到其实现存在的地方并执行该方法。如果我错了,请纠正我。嘿,本。我能提供一些发帖技巧吗?(1) 在这里,技术写作是首选,因此最好使用完整的文字,而不是“plz”等。(2)我们更希望在问题下的评论中保留对问题质量的反馈-大多数读者对这些问题不感兴趣,因此我们尽量将其排除在答案之外。(3) 我们倾向于删减“希望这有帮助”等——这是一种很好的情绪,但有点多余。(4) 人称代词“I”在英语中总是大写字母。谢谢
 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"}));

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