如何从recyclelerView

时间:2018-06-12 13:11:55

标签: android android-fragments android-recyclerview android-adapter android-dialogfragment

基本上,我有一个用户输入数据的对话框。此对话框从具有自定义适配器的recyclerView启动。在用户点击"完成"后,我需要在对话框中调用notifyDataSetChanged

我该怎么做?

代码:

public class FoodListAdapter extends RecyclerView.Adapter<FoodListAdapter.ViewHolder>
    implements View.OnClickListener {
    //blabla

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        String foodItemTitle = mFoodItemList.get(position).getTitle();

        FoodEditDietDialog dialog = new FoodEditDietDialog();
        dialog.show(fm, mFoodItemList.get(position).getTimeStamp());
    }


public class Fragment extends Fragment {
    //blabla

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View fragment = inflater.inflate(R.layout.fragment, container, false);

    foodList = databaseHelper.getList("somethingsomething");
    recyclerView = Fragment.findViewById(R.id.list);
    recyclerView.setHasFixedSize(false);
    layoutManager = new LinearLayoutManager(this.getContext());
    recyclerView.setLayoutManager(layoutManager);
    adapter = new FoodListAdapter(fm, foodList);
    recyclerView.setAdapter(adapter);
}

public class FoodEditDietDialog extends DialogFragment {
    //blabla

    button = dialog.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            databaseHelper.doSomething();
            dismissDialog();
            //TODO: call method in dietFragment to notify adapters
        }
    });
}

1 个答案:

答案 0 :(得分:1)

您需要通过托管Activity执行此操作。

public interface DataChangeListener {
    void onDataChange();
}

public class YourActivity extends Activity implements DataChangeListener {

    private List<DataChangeListener> dataChangeListeners = new ArrayList<>();

    @Override
    public void onDataChange() {
        for(DataChangeListener listener in dataChangeListeners) {
            listener.onDataChange();
        }
    }

    public void addDataChangeListener(DataChangeListener listener) {
        dataChangeListeners.add(listener);
    }

    public void removeDataChangeListener(DataChangeListener listener) {
        dataChangeListeners.remove(listener);
    }
}

public class Fragment extends Fragment implements DataChangeListener {

    @Override
    public void onAttach(Activity activity) {
        if (activity instanceOf YourActivity) {
            activity.addDataChangeListener(this)
        }
    }

    @Override
    public void onDetach() {
        Activity activity = getActivity();
        if (activity instanceOf YourActivity) {
            activity.removeDataChangeListener(this)
        }
    }

    @Override
    public void onDataChange() {
        adapter.notifyDataSetChanged();
    }
}

public class FoodEditDietDialog extends DialogFragment {
    //blabla

    button = dialog.findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            databaseHelper.doSomething();
            dismissDialog();

            //TODO: call method in dietFragment to notify adapters
            Activity activity = getActivity();
            if (activity instanceOf DataChangeListener) {
                activity.onDataChange();
            }
        }
    });
}

所以基本上,对话框会调用activity,activity会通知片段,片段会通知适配器。或者,您可以将片段(作为DataChangeListener)传递给适配器和对话框。并从那里打电话。