活动中的命令ViewPager按钮OnClick

时间:2018-09-11 08:50:53

标签: android android-viewpager retrofit android-pageradapter

我已经在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使用Web服务,如何在MainActivity中访问此按钮?或者我可以使用TestPagerAdapter的Web服务吗?

这是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

2 个答案:

答案 0 :(得分: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

如果您有任何疑问,请问

答案 1 :(得分:0)

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

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