我需要帮助,所以我有一个带有recycleView的片段,在recycleView里面有一个按钮。
点击后的按钮必须打开已在基本片段中声明的对话框,所以我只调用" openDialog(DIALOG_CHECK); "
现在我如何在我的适配器上调用该对话框我已经在片段中创建了一个方法并从适配器调用它并发出错误" Java lang null指针"
这是我的代码:
DeliveryFragment delivFrag = new DeliveryFragment();
holder.editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
delivFrag.doEdit();
}
});
并在片段中
public void doEdit(){
openDialog(DIALOG_EDIT_ITEM);
}
答案 0 :(得分:8)
只是一个更好理解的简单例子。使用界面。
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {
private static OnItemClickListener mOnItemClickLister;
public interface OnItemClickListener {
void onItemClicked(View view, int pos);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mOnItemClickLister = listener;
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
Button mBtnTest;
Context mContext;
//We also create a constructor that accepts the entire item row
//and does the view lookups to find each subview.
public ViewHolder(Context context, View itemView) {
//Stores the itemView in a public final member variable that can be used
//to access the context from any ViewHolder Instance
super(itemView);
mContext = context;
mBtnTest = (Button) itemView.findViewById(R.id.message_button);
itemView.setOnClickListener(this);
}
@Override public void onClick(View v) {
int position = v.getLayoutDirection();
mOnItemClickLister.onItemClicked(v, position);
}
}
}
片段部分
class FragmentTest extends Fragment implements OnItemClickListener {
TestAdapter adapter = new TestAdapter(); //you can initialize according to your logic
//set the fragment as a listener to adapter
this.adapter.setOnItemClickListener(onItemClickListener);
public void onItemClicked(View view, int pos) {
//do whatever you want here...
}
}
答案 1 :(得分:1)
您必须在Adapter类中编写一个接口,并在您调用适配器的片段中实现该功能。
以下是带有项目点击操作的回收者视图的示例应用程序。 检查一下它可能对您有用。 https://www.dropbox.com/s/2q1ywnehz454axw/SamplePro_Recycler.zip?dl=0
答案 2 :(得分:1)
更新适配器构造函数以接受Fragment作为参数。
customAdapter = new CustomAdapter(myContext, android.R.layout.simple_list_item_1, getList, HomeFragment.this);
适配器类:
public CustomAdapter(Context context, int id, HomeFragment fragment) {
this.fragment = fragment;
}
fragment.doSomething();
答案 3 :(得分:1)
创建一个充当适配器和Fragment之间桥梁的接口。
interface FragmentCallback{
public void doSomething(){
}
}
使Fragment实现回调并覆盖方法
class DeliveryFragment extends Fragment implements FragmentCallback{
@Override
public void doSomething(){
doEdit() //<--Call your method here
}
public void doEdit(){
openDialog(DIALOG_EDIT_ITEM);
}
}
Provide an instance of callback in Adapter and call the callback method from Adapter.
MyAdapter类扩展了RecyclerView.Adapter {
public FragmentCallback callback;
public MyAdapter(FragmentCallback callback){
thid.call = callback; //<--This is how you initialise it
}
callback.doSomething(). //<-- This is how you call callback method
}
MyAdapter adapter = new MyAdapter(this)
示例代码:
您可以找到示例代码here。
免责声明:该示例代码基于上述想法,但不是很容易理解。
答案 4 :(得分:0)
您可以将Fragment Instance发送到Adapter的构造函数中,然后您可以使用此实例来调用该片段中的方法。
public MyCartRecycleAdapter(Context context, List<CartData> list, MyFragmentNew myFragmentNew) {
this.list = list;
this.mContext = (Activity) context;
this.myFragmentNew = myFragmentNew;
}
然后
myFragmentNew.MethodName();