Skip to content

Instantly share code, notes, and snippets.

@osamaqarem
Last active August 20, 2023 15:25
Show Gist options
  • Save osamaqarem/d059222e4119c10c2d153f10abea8307 to your computer and use it in GitHub Desktop.
Save osamaqarem/d059222e4119c10c2d153f10abea8307 to your computer and use it in GitHub Desktop.
Android Java Boilerplate

inffrag

    super.onCreateView(inflater, container, savedInstanceState);
    View view = inflater.inflate(R.layout.$LAYOUT$, container, false);
    ButterKnife.bind(this, view);

    return view;

spinneradapt

    private List<String> $DATA$;

    public $CLASSNAME$(List<String> $DATA$) {
        this.$DATA$ = $DATA$;
    }

    @Override
    public int getCount() {
        return $DATA$ == null ? 0 : $DATA$.size();
    }

    @Override
    public Object getItem(int i) {
        return $DATA$.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.$LAYOUT$, viewGroup, false);
        }

        // Example
        FontTextView value = view.findViewById(R.id.value);
        value.setText($DATA$.get(i));

        return view;
    }

    public List<String> getSignatureTypes() {
        return $DATA$;
    }

recycpag

@BindView(R.id.$PROGRESS_BAR_ID$)
ProgressBar loadingBottom;

@BindView(R.id.$RECYCLER_ID$)
RecyclerView recyclerView;
private RecyclerView.Adapter mAdapter;
private LinearLayoutManager layoutManager;
 
// Dialog callback
LinearLayoutPagedLoadScrollListener.LoadCompleteNotifier mLoadCompleteNotifier;

// Call this method in onCreate
private void setupAdapters() {
 mAdapter = new $ADAPTER_CLASS$($ITEMS$);
 layoutManager = new LinearLayoutManager(this);

 recyclerView.setLayoutManager(layoutManager);
 recyclerView.setAdapter(mAdapter);

 recyclerView.addOnScrollListener(new LinearLayoutPagedLoadScrollListener(layoutManager) {
     @Override
     public void onLoadMore(int page, LoadCompleteNotifier loadComplete) {
         mLoadCompleteNotifier = loadComplete;
         // TODO: Load more data
     }
 });
}

public void setBottomLoading(boolean isLoading) {
 loadingBottom.setVisibility(isLoading ? View.VISIBLE : View.GONE);
 if (!isLoading) mLoadCompleteNotifier.notifyLoadComplete();
}

recycadapt

public class $CLASSNAME$ extends RecyclerView.Adapter {
    private List<String> dataList;

    // View holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // Replace TextView with your item layout type
        public TextView itemView;

        // View holder constructor
        public ViewHolder(TextView itemView) {
            super(itemView);
            this.itemView = itemView;
        }
    }

    // Adapter constructor
    public $CLASSNAME$(List<String> dataList) {
        this.dataList = dataList;
        this.notifyDataSetChanged();
    }

    // Setter
    public void setDataList(List<String> dataList) {
        this.dataList = dataList;
    }

    // Create new views
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // Replace with your item layout type and file
        TextView view = (TextView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.$ITEM_VIEW$, parent, false);

        return new ViewHolder(view);
    }

    // Replace contents of a view
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        // Current item
        String currentItem = this.dataList.get(position);

        TextView attachmentView = holder.itemView.findViewById(R.id.$ITEM_ID$);

        attachmentView.setText(currentItem);
    }

    @Override
    public int getItemCount() {
        return this.dataList == null ? 0 : this.dataList.size();
    }

    public void addItem(String attachmentName) {
        this.dataList.add(attachmentName);
        this.notifyDataSetChanged();
    }

    public void setItems(List<String> attachmentNameList) {
        this.dataList = attachmentNameList;
        this.notifyDataSetChanged();
    }

    public void clear() {
        this.dataList.clear();
        this.notifyDataSetChanged();
    }
}

recyc

    @BindView(R.id.$LAYOUT$)
    RecyclerView recyclerView;
    private RecyclerView.Adapter mAdapter;
    private LinearLayoutManager layoutManager;

    // Call this method in onCreate
    private void setupAdapters() {
        mAdapter = new $ADAPTER$();
        layoutManager = new LinearLayoutManager(this);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(mAdapter);
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment