如何在ViewPager中编写Button.setOnClickListener

时间:2017-10-02 08:32:30

标签: java android android-activity appcompatactivity

我在一个ViewPager中有三个页面,我在Page1上创建了一个按钮。 当我点击该按钮时,我可以进行其他活动。

我想在ViewPager中使用Button setOnClickListener。 我的应用程序可以运行,但该按钮无效。

这是ViewPager活动文件。

public class ScheduleActivity extends AppCompatActivity {

private View view1, view2, view3;
private List<View> viewList;
private ViewPager viewPager;

private List<String> titleList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_schedule);
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    PagerTitleStrip pagerTitleStrip = (PagerTitleStrip) findViewById(R.id.pager_title);

    pagerTitleStrip.setTextSize(TypedValue.COMPLEX_UNIT_PX,70);

    pagerTitleStrip.setTextColor(Color.WHITE);

    pagerTitleStrip.setBackgroundColor(Color.BLUE);

    pagerTitleStrip.setGravity(17);

    pagerTitleStrip.getChildAt(0).setVisibility(View.GONE);
    pagerTitleStrip.getChildAt(2).setVisibility(View.GONE);

    LayoutInflater inflater = getLayoutInflater();
    view1 = inflater.inflate(R.layout.activity_day__schedule, null);
    view2 = inflater.inflate(R.layout.activity_week__schedule, null);
    view3 = inflater.inflate(R.layout.activity_missing__date, null);

    viewList = new ArrayList<View>();
    viewList.add(view1);
    viewList.add(view2);
    viewList.add(view3);

    titleList = new ArrayList<String>();
    titleList.add("Page1");
    titleList.add("Page2");
    titleList.add("Page3");

    PagerAdapter pagerAdapter = new PagerAdapter() {

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == viewList.get((int) 
         Integer.parseInt(arg1.toString()));
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return viewList.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position,
                                Object object) {
            // TODO Auto-generated method stub
            container.removeView(viewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub
            container.addView(viewList.get(position));

            return position;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            // TODO Auto-generated method stub
            return titleList.get(position);
        }

    };

    viewPager.setAdapter(pagerAdapter);

}

这里是按钮onclicklistener文件...

public class Day_Schedule extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_day__schedule);
    Button more_button = (Button) findViewById(R.id.more_button);
    more_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Day_Schedule.this, 
    MaintainActivity.class);
            startActivity(intent);
        }
    });
  }

 }

我可以对这些文件做什么?如何纠正它们?

1 个答案:

答案 0 :(得分:0)

您必须在视图寻呼机适配器视图和类代码中设置按钮侦听器 尝试组织您的代码以使其更具可读性 https://github.com/android10/Android-CleanArchitecture