Created
March 21, 2017 05:00
-
-
Save dron247/8fc32ccc14a01c13c8649fc6cc694b68 to your computer and use it in GitHub Desktop.
Example implementation of an recycler adapter without xml markup used. No inflate - no problemo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this is an internal class in example activity | |
// check file name and access modifiers before use | |
static class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ViewHolder> { | |
List<TodoListItem> items; | |
ClickListener clickListener; | |
Resources resources; | |
int DP16; | |
ExampleAdapter(Context context, List<TodoListItem> items) { | |
this.items = items; | |
resources = context.getResources(); | |
setHasStableIds(false); | |
DP16 = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, resources.getDisplayMetrics()); | |
} | |
public void setOnClickListener(ClickListener listener) { | |
clickListener = listener; | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
/*View layout = LayoutInflater.from(parent.getContext()) | |
.inflate(R.layout.row_todo, parent, false);*/ | |
FrameLayout layout = new FrameLayout(parent.getContext()); | |
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); | |
layout.setPadding(0, DP16, 0, DP16); | |
layout.setLayoutParams(lp); | |
layout.setId(R.id.row_todo_container); | |
TextView textView = new TextView(parent.getContext()); | |
LinearLayout.LayoutParams tlp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); | |
textView.setLayoutParams(tlp); | |
textView.setId(R.id.row_todo_label); | |
layout.addView(textView); | |
return new ViewHolder(layout); | |
} | |
@Override | |
public void onBindViewHolder(final ViewHolder holder, int position) { | |
TodoListItem item = getItem(position); | |
holder.id = item.getId(); | |
holder.text.setText(item.getText()); | |
holder.container.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (clickListener != null) { | |
clickListener.onItemClick(getItem(holder.id)); | |
} | |
} | |
}); | |
} | |
@Override | |
public int getItemCount() { | |
return items.size(); | |
} | |
private TodoListItem getItem(int position) { | |
return items.get(position); | |
} | |
interface ClickListener { | |
void onItemClick(TodoListItem item); | |
} | |
class ViewHolder extends RecyclerView.ViewHolder { | |
int id; | |
TextView text; | |
View container; | |
ViewHolder(ViewGroup itemView) { | |
super(itemView); | |
container = itemView; | |
text = (TextView) itemView.findViewById(R.id.row_todo_label); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment