如何在片段和适配器之间创建接口?我有碎片ListView,说MyListFragment,以及习俗CursorAdapter..我正在设置onClickListener在此适配器中,用于列表行中的按钮。public class MyListAdapter extends CursorAdapter {
public interface AdapterInterface {
public void buttonPressed();
}
...
@Override
public void bindView(final View view, final Context context, final Cursor cursor) {
ViewHolder holder = (ViewHolder) view.getTag();
...
holder.button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// some action
// need to notify MyListFragment
}
});
}}public MyListFragment extends Fragment implements AdapterInterface {
@Override
public void buttonPressed() {
// some action
}}我需要在按下按钮时通知片段。如何调用这个接口?救命求你了。
3 回答
慕的地10843
TA贡献1785条经验 获得超8个赞
AdapterInterface buttonListener;public MyListAdapter (Context context, Cursor c, int flags, AdapterInterface buttonListener){ super(context,c,flags); this.buttonListener = buttonListener;}
public void onClick(View v) { buttonListener.buttonPressed();}
Adapter
MyListAdapter adapter = new MyListAdapter (getActivity(), myCursor, myFlags, this);
this
AdapterInterface
.
添加回答
举报
0/150
提交
取消