gpt4 book ai didi

android - 在 View Pager android 中调用按钮单击监听器

转载 作者:行者123 更新时间:2023-11-29 00:21:38 26 4
gpt4 key购买 nike

我正在使用 View pager 在 android 中滑动页面,我的所有页面都有按钮,我想在按钮上设置点击监听器。我怎样才能做到这一点?我检查了这个链接。 On Click listener is not working in View Pager但它对我不起作用。

这是我的代码:

主类

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(2);

Button btnDone = (Button) findViewById(R.id.buttonalpha);
btnDone.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "clicked", Toast.LENGTH_SHORT).show();



}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

ViewPager

public class MyPagerAdapter extends PagerAdapter {

public int getCount() {
return 3;
}

public Object instantiateItem(View collection, int position) {



LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

int resId = 0;
switch (position) {
case 0:
resId = R.layout.hello;
break;
case 1:
resId = R.layout.hello2;
break;
case 2:
resId = R.layout.hello3;
break;
/*case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
break;*/
}

View view = inflater.inflate(resId, null);

((ViewPager) collection).addView(view, 0);

return view;
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);

}


@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);

}

@Override
public Parcelable saveState() {
return null;
}
}

如有任何帮助,我们将不胜感激。

最佳答案

我认为使用 FragmentPagerAdapter 会更好。

fragment 示例。

public class YourFragment extends Fragment {

private OnClickListener mListener;


public static YourFragment newInstance(int layoutId){
Bundle args = new Bundle();
args.putInt("id", layoutId);
YourFragment fr = new YourFragment();
fr.setArguments(args);
return fr;
}


@Override
public void onAttach(Activity activity) {
super.onAttach(activity);

mListener = (OnClickListener)activity;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(getArguments().getInt("id"), container, false);
view.findViewById(R.id.action_bar).setOnClickListener(mListener);


return view;
}
}

mainActivity 应该实现 OnClickListener。

观看http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

关于android - 在 View Pager android 中调用按钮单击监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22629083/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com