命令ViewPager按钮OnClick from a Activity

问题描述 投票:0回答:2

我在我的Android应用程序中创建了一个ViewPager,这是代码

public class TestPagerAdapter extends PagerAdapter {

    Context context;
    List<PagerModel> pagerArr;
    LayoutInflater inflater;

    public TestPagerAdapter(Context context, List<PagerModel> pagerArr) {
        this.context = context;
        this.pagerArr = pagerArr;
        inflater = ((Activity)context).getLayoutInflater();
    }

    @Override
    public int getCount() {
        return pagerArr.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view= inflater.inflate(R.layout.pop_up,container,false);

        TextView text_task=view.findViewById(R.id.textView8);
        TextView text_child_parent=view.findViewById(R.id.textView9);
        TextView edit_child_parent=view.findViewById(R.id.textView10);
        TextView text_deadline=view.findViewById(R.id.textView12);
        TextView text_avancement=view.findViewById(R.id.textView14);
        Button button_update=view.findViewById(R.id.button3);
        Spinner s = view.findViewById(R.id.spinner2);
        ImageView rl=view.findViewById(R.id.imm11);

        view.setTag(position);
        ((ViewPager)container).addView(view);
        PagerModel model=pagerArr.get(position);

        text_task.setText(model.getText_task());
        text_avancement.setText(model.getText_avancement());
        text_deadline.setText(model.getText_deadline());
        text_child_parent.setText(model.getText_child_parent());
        edit_child_parent.setText(model.getEdit_child_parent());
        button_update.setText(model.get_button_text());

        return view;
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==((View)object);
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager)container).removeView((View)object);
    }

}

当我点击Retrofit时,我想使用button_update使用网络服务,如何在我的MainActivity中访问此按钮?或者我可以从TestPagerAdapter消费我的网络服务?

这是MainActivity

 final CaldroidListener listener = new CaldroidListener() {
            @Override
            public void onSelectDate(Date date, View view) {


                final Dialog dialog=new Dialog(MainActivity.this);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.pager_layout);

                final LayoutInflater factory = getLayoutInflater();
                final View textEntryView = factory.inflate(R.layout.pop_up, null);
                final Button button_update = textEntryView.findViewById(R.id.button3);

                final LayoutInflater factory2 = getLayoutInflater();
                final View textEntryView2 = factory.inflate(R.layout.pop_up, null);
                final Spinner s = textEntryView.findViewById(R.id.spinner2);


                String d= DateFormat.format("dd-MM-yyyy", date.getTime()).toString();

                Retrofit retrofit = new Retrofit.Builder()
                            .baseUrl(getString(R.string.BaseUrl))
                            .addConverterFactory(GsonConverterFactory.create())
                            .build();

                    final MyInterface myInterface=retrofit.create(MyInterface.class);

                    Call<List<Task>> call = myInterface.getTask(d);

                    call.enqueue(new Callback<List<Task>>() {
                        @Override
                        public void onResponse(Call<List<Task>> call, Response<List<Task>> response) {
                            final List<Task> TaskList = response.body();
                            if(!TaskList.isEmpty()){

                                List<PagerModel> pageArr = new ArrayList<>();
                                for(int i=0;i<TaskList.size();i++){
                                    pageArr.add(new PagerModel(TaskList.get(0).getDescription(),"Parent",TaskList.get(0).getOwner(),TaskList.get(0).getDeadline(),TaskList.get(0).getAvancement(),"Update Task"));
                                    }


                                TestPagerAdapter adapter44=new TestPagerAdapter(MainActivity.this,pageArr);
                                AutoScrollViewPager pager= dialog.findViewById(R.id.pager);

                                pager.setAdapter(adapter44);

                                CirclePageIndicator indicator=dialog.findViewById(R.id.page_indicator);
                                indicator.setViewPager(pager);
                                indicator.setCurrentItem(0);
                                dialog.show();}
                            else Toast.makeText(getApplicationContext(),"il y'a pas de task pour cette date",Toast.LENGTH_SHORT).show();

                            ////////////update

                            button_update.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {

                                    Toast.makeText(getApplicationContext(),"woooow",Toast.LENGTH_SHORT).show();
  
                                    
                                    Call<Success> call22= myInterface.UpdateTask(TaskList.get(0).getId(),s.getSelectedItem().toString());
                                    call22.enqueue(new Callback<Success>() {
                                        @Override
                                        public void onResponse(Call<Success> call, Response<Success> response) {
                                            Success success = response.body();
                                            int s=success.getCode();

                                            if(s==0)  Toasty.error(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();

                                            else {
                                                Toasty.success(getApplicationContext(),"Success update",Toast.LENGTH_SHORT).show();
                                                dialog.dismiss(); }
                                        }

                                        @Override
                                        public void onFailure(Call<Success> call, Throwable t) {
                                            Toast.makeText(getApplicationContext(),"failure",Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            });


                        }



                        @Override
                        public void onFailure(Call<List<Task>> call, Throwable t) {
                            Toasty.error(getApplicationContext(),"failure",Toast.LENGTH_SHORT).show();
                        }
                    });

但我无法访问button_update

android android-viewpager retrofit android-pageradapter
2个回答
0
投票

AdapterActivity沟通的最佳方法是与Listener,你Activity必须implements,类似的东西:

public TestPagerAdapter(Context context, List<PagerModel> pagerArr, InteractionListener listener) {
        this.context = context;
        this.pagerArr = pagerArr;
        this.listener = listener
        inflater = ((Activity)context).getLayoutInflater();
    }

public interface InteractionListener{

    void onUpdateClick();

} 

点击按钮后,您可以通知Activity

button_update.setOnClickListener(v -> {
    if(listener != null){
        listener.onUpdateClick();
    }
});

比在Activity你可以打电话给你的WebService,但请不要直接在你的Activity,但在你的Repository内移动这个逻辑

如果您有一些问题,请问


0
投票

当寻呼机适配器动态创建控件时,您无法直接控制按钮。但要控制按钮单击事件,您可以在pagerAdapter中创建某种自定义事件,然后在主活动中订阅该事件。

在适配器中,您可以处理单击按钮并引发您在适配器中创建的事件。事件会将句柄发送到mainActivity中的方法。

© www.soinside.com 2019 - 2024. All rights reserved.