因为在 ViewPager
上永远不会触发 onClick 事件(请参阅 这个问题),我将把onClickListener
设置为适配器中的item容器.
Since the onClick event is never triggered on a ViewPager
(see this question), I'm going to set the onClickListener
to the item container in the adapter.
这个想法是,当点击一个项目时,从适配器调用一个方法,该方法来自该适配器设置为 ViewPager
的 Fragment.
The idea is, when an item is clicked, to call from the adapter a method from the Fragment in which this adapter is set to the ViewPager
.
对于 Activity
,我将传递上下文并使用此上下文调用方法(类似于 ((myActivity)context).itemContainerOnClick()
).
For an Activity
I would pass the context and call the method using this context (something like ((myActivity)context).itemContainerOnClick()
).
但是我该如何处理 Fragment?将 Fragment 作为参数传递给适配器的构造函数时,我会遇到任何问题吗?
But how can I do with a Fragment? Could I face any issue passing the Fragment as an argument to my adapter's constructor?
谢谢
片段
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
}
}
页面适配器
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 */
}
用 ItemClick 的变化检查我下面的代码.
Check my below code with Change of 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 */
}
这篇关于在适配器的构造函数中传递片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!