This question is similar to Accessing items in CardView after it has been created and put into a RecyclerView. But I didn't understand the answer.
I have a CardViewAdapter ViewHolder
-
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView card_text;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
card_text = (TextView) itemLayoutView.findViewById(R.id.info_text);
}
}
And I access it using OnBindViewHolder
which is in the same, CardViewAdapter
-
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.card_text.setText(mColorData[position]);
viewHolder.card_text.setBackgroundColor(Color.parseColor("#616161"));
}
But I also have a RecyclerView
and now I wish to access the TextView
from this CardViewAdapter
and use it in my RecyclerView
Activity so that I can set an action when it is (long) clicked.
My RecyclerView
-
public class RecyclerViewMainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recycler_view);
String[] colorData = {
//a simple string array
}
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter = new CardViewDataMainAdapter(colorData);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, mRecyclerView, new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
//do stuff here
}
@Override
public void onItemLongClick(View view, int position) {
//I want to do stuff here using TextView from CardViewAdapter.
}
}
P.S. I've implemented OnClickListener
and OnLongClickListener
using https://stackoverflow.com/a/26826692/4458075