Search code examples
androidandroid-recyclerviewshared-librariesadapteraar

Android RecyclerView Adapter from Library module


I create a local Android library with common shared code. I successfully use and import it in my Android app project using the .aar file.

Now I create a custom RecyclerView Adapter in my library.

public class MyCustomAdapter extends RecyclerView.Adapter<CustomFolderAdapter.ViewHolder> {


    private Context context;
    private List<CustomObject> array_data;


    public MyCustomAdapter(Context context, List<CustomObject> array_data) {
        this.context = context;
        this.array_data = array_data;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    ...
    }



    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
    ...
    }


    @Override
    public int getItemCount() {
    ...
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView txv1;

        private ViewHolder(View v) {
            super(v);
            txv1 = v.findViewById(R.id.txv1);
        }
    }
}

Can I call it from my app's activity ?

Something like:

import static com.example.mycustomlibrary.MyCustomAdapter;

or

MyCustomLibrary().MyCustomAdapter

Solution

  • You should be able to import it just like any other class - no import static needed, as long as it's a top-level class. Just refer to it by the package name followed by the class name:

    import com.example.mycustomlibrary.MyCustomAdapter;