Last active
September 7, 2018 14:10
-
-
Save csabafarkas/223246af2770e8ac86128082342bd135 to your computer and use it in GitHub Desktop.
RecyclerView with Adapter and OnClickListener implemented
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<android.support.v7.widget.RecyclerView | |
android:id="@+id/rv_numbers" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
</FrameLayout> |
This file contains hidden or 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
public interface ListItemClickListener { | |
void onListItemClick(int clickedItemIndex); | |
} |
This file contains hidden or 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
// Implement Adapter.ListItemClickListener | |
public class MainActivity extends AppCompatActivity implements MyRecyclerViewAdapter.ListItemClickListener { | |
private static final int NUM_LIST_ITEMS = 100; | |
public MyRecyclerViewAdapter mAdapter; | |
public RecyclerView mList; | |
// Toast is used to create a reaction to the item-click | |
private Toast mToast; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
mList = (RecyclerView) findViewById(R.id.rv_numbers); | |
LinearLayoutManager layoutManager = new LinearLayoutManager(this); | |
mList.setLayoutManager(layoutManager); | |
mList.setHasFixedSize(true); | |
mAdapter = new MyRecyclerViewAdapter(NUM_LIST_ITEMS, this); | |
mList.setAdapter(mAdapter); | |
} | |
public void onListItemClick(int clickedItemIndex) { | |
if (mToast != null) { | |
mToast.cancel(); | |
} | |
mToast = Toast.makeText(this, "Item #" + clickedItemIndex, Toast.LENGTH_LONG); | |
mToast.show(); | |
} | |
} |
This file contains hidden or 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
public class MyRecyclerViewAdapter extends RecyclerView.Apadter<MyRecyclerViewAdapter.NumberViewHolder> { | |
final private ListItemClickListener mOnClickListener; | |
private static int viewHolderCount; | |
private int mNumberItems; | |
public MyRecyclerViewAdapter(int numberOfItems, ListItemClickListener listener) { | |
mNumberItems = numberOfItems; | |
mOnClickListener = listener; | |
viewHolderCount = 0; | |
} | |
@Override | |
public NumberViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { | |
Context context = viewGroup.getContext(); | |
LayoutInflater inflater = LayoutInflater.from(context); | |
View view = inflater.inflate(R.layout.number_list_item, viewGroup, false); | |
NumberViewHolder viewHolder = new NumberViewHolder(view); | |
return viewHolder; | |
} | |
@Override | |
public void onBindViewHolder(NumberViewHolder holder, int position) { | |
holder.bind(position); | |
} | |
@Override | |
public int getItemCount() { | |
return mNumberItems; | |
} | |
class NumberViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { | |
TextView listItemNumberView; | |
public NumberViewHolder(View itemView) { | |
super(itemView); | |
listItemNumberView = (TextView) itemView.findViewById(R.id.tv_item_number); | |
itemView.setOnClickListener(this); | |
} | |
void bind(int listIndex) { | |
listItemNumberView.setText(String.valueOf(listIndex)); | |
} | |
// Override onClick, passing the clicked item's position (getAdapterPosition()) to mOnClickListener via its onListItemClick method | |
@Override | |
public void onClick(View v) { | |
int clickedPosition = getAdapterPosition(); | |
mOnClickListener.onListItemClick(clickedPosition); | |
} | |
} | |
} |
This file contains hidden or 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
<?xml version="1.0" encoding="utf-8"?> | |
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="16dp"> | |
<TextView | |
android:id="@+id/tv_item_number" | |
style="@style/TextAppearance.AppCompat.Title" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_vertical|start" | |
android:fontFamily="monospace" | |
android:textSize="42sp" /> | |
</FrameLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment