由于onClick事件从未在ViewPager
上触发(请参阅this question),因此我将onClickListener
设置为适配器中的项容器。
当单击某个项目时,想法是从适配器调用Fragment中的方法,其中此适配器设置为ViewPager
。
对于Activity
,我会传递上下文并使用此上下文调用方法(类似((myActivity)context).itemContainerOnClick()
)。
但是我怎么能用片段呢?我可以将Fragment作为参数传递给我的适配器的构造函数吗?
由于
片段
public class MyFragment extends Fragment {
ViewPager mViewPager;
int mPosition = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = (RelativeLayout) inflater.inflate(R.layout.my_fragment_container, container, false);
ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewpager);
mViewPager.setAdapter(new MyAdapter(getActivity(), myObjectArray);
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
mPosition = position;
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageScrollStateChanged(int state) {}
});
mViewPager.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// I want to start some activity, passing mPosition
// but this is never called
}
});
return view;
}
// Alternative to mViewPager.setOnClickListener, called from the adapter
public void onViewPagerClick()
{
// start some activity, passing mPosition
}
}
PageAdapter
class MyAdapter extends PagerAdapter {
private MyObject[] mItemArray;
private Context mContext;
public SejoursWidgetAdapter(Context context, MyObject[] itemArray) {
mContext = context;
mItemArray = itemArray;
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.item, null);
// Add click listener here because setting it directly on the ViewPager does not work (onClick not triggered)
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// How can I call MyFragment's onViewPagerClick() ?
}
});
((ViewPager) collection).addView(row);
return row;
}
/* More stuff */
}
答案 0 :(得分:3)
使用ItemClick更改检查我的下面的代码。
class MyAdapter extends PagerAdapter {
private MyObject[] mItemArray;
private Context mContext;
public SejoursWidgetAdapter(Context context, MyObject[] itemArray) {
mContext = context;
mItemArray = itemArray;
}
@Override
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.item, null);
row .setTag(position);
// Add click listener here because setting it directly on the ViewPager does not work (onClick not triggered)
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// How can I call MyFragment's onViewPagerClick() ?
int position=(Integer) v.getTag();
Log.v("Log_tag", "Here image is clicked"+ position);
//now you known which Page is click you can do what you want.
}
});
((ViewPager) collection).addView(row);
return row;
}
/* More stuff */
}
答案 1 :(得分:1)
您还可以通过
从mViewPager获取当前页面的位置int position = mViewPager.getCurrentItem();
而不是
int position=(Integer) v.getTag();