接口片段android的接口适配器

时间:2016-07-30 12:54:29

标签: java android interface

提供接口的适配器类:

public class Adaptor{

    private ItemCLickCallback itemCLickCallback;

        public interface ItemCLickCallback {
            void onItemClick(int p);
            void onSecItemClick(int p);
        }
        public void setItemCLickCallback(final ItemCLickCallback itemCLickCallback){
            this.itemCLickCallback = itemCLickCallback;
        }}


public class Overview extends Fragment implements Adaptor.ItemCLickCallback{

         @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_overview, container, false);
        Adaptor adaptor = new Adaptor(mijnwinkels, this.getActivity());
adaptor.setItemCLickCallback(this);
        return view;
    }

@Override
    public void onItemClick(int p) {
}
    @Override
    public void onSecItemClick(int p) {
      // On click: send data to database, remove data from database
      // Needs methods that should be implemented in my activity, cant be linked to the activity
    }}

因此。在我的片段中覆盖的方法需要应该位于我的活动中的方法,这些方法需要来自片段的数据。通常我会使用一个接口来执行此操作,但这些方法已经是另一个类的接口的一部分,所以我如何将它们放在一个接口中,以便我可以从我的活动中做所有事情?谢谢

2 个答案:

答案 0 :(得分:1)

您的问题似乎主要涉及从界面中删除重复的代码,所以这里有一些建议。

在onSuccess中获取数据时,无需添加全新的适配器。

private ArrayList<Winkel> mijnwinkels;
private Adaptor mAdaptor;

onCreateView() { 
    //... Other code 
    mijnwinkels = new  ArrayList<Winkel>();
    // mRecyclerView =... 
    mAdaptor = new Adaptor(mijnwinkels, this.getActivity());
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    mRecyclerView.addItemDecoration(new VerticalSpace(30));
    mRecyclerView.setAdapter(adaptor);
    adaptor.setItemCLickCallback(this);
   // etc... 
} 

@Override
public void onSuccess(ArrayList<Winkel> winkels) {
    mijnwinkels.clear();
    mijnwinkels.addAll(winkels);
    mAdaptor.notifyDatasetChanged(); // something like this... Not sure how RecyclerView does it 
 }

如果你制作Winkel implements Parcelable并为其实现必要的代码,则删除那里的一些代码。

@Override
public void onItemClick(int p) {
    Winkel winkel = (Winkel) mijnwinkels.get(p);

     Bundle detailsBundle = new Bundle();
    detailsBundle.putExtra(EXTRA_WINKEL,winkel);

    Details detail= new Details();
    detail.setArguments(detailsBundle);
    this.getFragmentManager().beginTransaction()
            .replace(R.id.mycontainer,detail,null)
            .addToBackStack(null)
            .commit();
  }

虽然,实际上,所有这些都可以作为自己的方法编写。

public static void showWinkel(Winkel winkel, FragmentManager fm) {
    Bundle detailsBundle = new Bundle();
    detailsBundle.putExtra(EXTRA_WINKEL,winkel);

    Details detail= new Details();
    detail.setArguments(detailsBundle);
    fm.beginTransaction()
        .replace(R.id.mycontainer,detail)
        .addToBackStack(null)
        .commit();
} 

从任何地方打电话

@Override
public void onItemClick(int p) {
    showWinkel(mijnwinkels.get(p), this.getFragmentManager());
} 

答案 1 :(得分:0)

而不是

adaptor.setItemCLickCallback(this);

使用下面的代码片段:

   adaptor.setItemCLickCallback(new Adaptor.ItemCLickCallback () {
                @Override
                public void onItemClick(int position) {

    }

});