Reputation: 222
Question
How can I access a toolbar and its children from a RecyclerViewHolder? I want to change the textView in my toolbar_main from RecyclerViewHolder with LongClick.
What I know
I know how to access the toolbar from my activty inside onCreate with
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
txtTitle = (TextView) toolbar.findViewById(R.id.txtTitle);
rbSelectAll = (RadioButton)toolbar.findViewById(R.id.rbSelectAll);
rbSelectAll.setVisibility(GONE);
txtTitle.setText("Gallerie");
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {
private List<ItemObject> itemList;
private Context context;
private boolean isDir;
public RecyclerViewAdapter(Context context, List<ItemObject> itemList, boolean isDir) {
this.itemList = itemList;
this.isDir = isDir;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView, itemList, context);
return rcv;
}
@Override
public void onBindViewHolder(final RecyclerViewHolders holder, int position) {
if(isDir){
File[] fileNames = null;
Log.v("Folder: ", itemList.get(position).getName());
File path = new File(itemList.get(position).getName());
if(path.exists()){
fileNames = path.listFiles();
}
int i = 0;
while(fileNames[i].isDirectory()){
i++;
}
Glide.with(context).asBitmap()
.load(fileNames[i])
.thumbnail(0.2f)
.into(holder.countryPhoto);
holder.selected.setVisibility(GONE);
holder.albumName.setText(itemList.get(position).getName().substring(itemList.get(position).getName().lastIndexOf("/")+1));
holder.countImages.setText(""+listFiles(itemList.get(position).getName()).size());
}else {
holder.selected.setVisibility(GONE);
holder.albumName.setVisibility(GONE);
holder.countImages.setVisibility(GONE);
Glide.with(context).asBitmap()
.load(itemList.get(position).getName())
.thumbnail(0.2f)
.into(holder.countryPhoto);
}
}
}
RecyclerViewHolders.java
public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
public TextView albumName, countImages, txtName;
public ImageView countryPhoto;
private List<ItemObject> itemList;
public RadioButton selected, selectAll;
public RecyclerViewHolders(View itemView, List<ItemObject> itemList, Context context) {
super(itemView);
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
this.itemList = itemList;
selected = (RadioButton)itemView.findViewById(R.id.rbSelected);
albumName = (TextView)itemView.findViewById(R.id.albumName);
countImages = (TextView)itemView.findViewById(R.id.countImages);
countryPhoto = (ImageView)itemView.findViewById(R.id.country_photo);
}
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked " + getPosition() + " " + itemList.get(getPosition()).getName(), Toast.LENGTH_SHORT).show();
}
@Override
public boolean onLongClick(View view) {
Toast.makeText(view.getContext(), "Long Clicked " + getPosition() + " " + itemList.get(getPosition()).getName(), Toast.LENGTH_SHORT).show();
if(selected.isChecked()){
selected.setVisibility(View.GONE);
selected.setChecked(false);
}else{
selected.setVisibility(VISIBLE);
selected.setChecked(true);
}
return true;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.spicysoftware.myapplication.MainActivity">
<include
android:id="@+id/toolbarPictures"
layout="@layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</RelativeLayout>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation" />
</LinearLayout>
toolbar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#5fb0c9"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:id="@+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</LinearLayout>
Upvotes: 1
Views: 369
Reputation: 1196
Step 1: Create a class and Interface for RecyclerViewOnItemClick
RecyclerTouchListener.java
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener{
private IOnRecyclerItemClickedListener clicklistener;
private GestureDetector gestureDetector;
public RecyclerTouchListener(Context context, final RecyclerView recycleView, final IOnRecyclerItemClickedListener clicklistener){
this.clicklistener=clicklistener;
gestureDetector=new GestureDetector(context,new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child=recycleView.findChildViewUnder(e.getX(),e.getY());
if(child!=null && clicklistener!=null){
clicklistener.onLongClick(child,recycleView.getChildAdapterPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child=rv.findChildViewUnder(e.getX(),e.getY());
if(child!=null && clicklistener!=null && gestureDetector.onTouchEvent(e)){
clicklistener.onClick(child,rv.getChildAdapterPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
Interface IOnRecyclerItemClickedListener.java
public interface IOnRecyclerItemClickedListener {
public void onRecyclerItemClick(View view, int position);
public void onRecyclerItemLongClick(View view,int position);
}
Step 2: Implement IOnRecyclerItemClickedListener in your activity
YourActivity.java
public class YourActivity extends Fragment implements IOnRecyclerItemClickedListener{
private Toolbar toolbar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
txtTitle = (TextView) toolbar.findViewById(R.id.txtTitle);
rbSelectAll = (RadioButton)toolbar.findViewById(R.id.rbSelectAll);
rbSelectAll.setVisibility(GONE);
txtTitle.setText("Gallerie");
}
@Override
public void onRecyclerItemLongClick(View view, int position) {
toolbar.setTitle("Title that you want to display");
}
@Override
public void onRecyclerItemClick(View v) {
}
}
}
I hope this answer will help you!!
Upvotes: 2
Reputation: 1800
((MainActivity) context).toolbar
Or ((MainActivity) context).findViewById(R.id.toolbar)
Upvotes: 1
Reputation: 1057
You can use interface to achieve your goal as shown below.
Create an interface.
public interface DummyInterface {
public void doSomethingWithToolbar();
}
Then implement this interface on your activity.
public class DummyActivity extends Activity implements DummyInterface {
.
.
.
public void doSomethingWithToolbar() {
//do something with toolbar
}
}
Then pass the reference of this(activity) when creating recycler view in constructor. Like,
new DummyRecyclerView(..., this);
Then add a parameter in RecyclerView's constructor. Like,
DummyInterface dummy = new DummyInterface();
public DummyRecyclerView(..., DummyInterface d) {
dummy = d;
}
Then pass dummy to the RecyclerViewHolder's constructor and add that to parameter in constructor of RecyclerViewHolder same way we have done from Activity to RecyclerView.
And you can call the method doSomethingWithToolbar
from RecyclerViewHolder using dummy.doSomethingWithToolbar()
.
Upvotes: 0
Reputation: 1152
You shouldn't couple two entirely separate Views. You should define an onLongClick interface that your Activity should implement and you set it on each and every itemView inside of the constructor for your ViewHolder then do your Toolbar functionality.
Upvotes: 1