问题
如何从RecyclerViewHolder访问工具栏及其子代? 我想使用LongClick从RecyclerViewHolder更改toolbar_main中的textView。
我所知道的
我知道如何使用
从onCreate内部的activty访问工具栏 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>
答案 0 :(得分:2)
步骤1:为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) {
}
}
接口IOnRecyclerItemClickedListener.java
public interface IOnRecyclerItemClickedListener {
public void onRecyclerItemClick(View view, int position);
public void onRecyclerItemLongClick(View view,int position);
}
第2步:在您的活动中实施IOnRecyclerItemClickedListener
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) {
}
}
}
我希望这个答案可以帮到你!!
答案 1 :(得分:1)
你不应该把两个完全独立的视图结合起来。您应该定义Activity应该实现的onLongClick接口,并在ViewHolder的构造函数内的每个itemView上设置它,然后执行工具栏功能。
答案 2 :(得分:1)
((MainActivity) context).toolbar
或者 ((MainActivity)context).findViewById(R.id.toolbar)
答案 3 :(得分:0)
您可以使用界面来实现您的目标,如下所示。
创建一个界面。
public interface DummyInterface {
public void doSomethingWithToolbar();
}
然后在您的活动上实施此界面。
public class DummyActivity extends Activity implements DummyInterface {
.
.
.
public void doSomethingWithToolbar() {
//do something with toolbar
}
}
然后在构造函数中创建recycleler视图时传递this(activity)的引用。像,
new DummyRecyclerView(..., this);
然后在RecyclerView的构造函数中添加一个参数。像,
DummyInterface dummy = new DummyInterface();
public DummyRecyclerView(..., DummyInterface d) {
dummy = d;
}
然后将dummy传递给RecyclerViewHolder的构造函数,并将它添加到RecyclerViewHolder的构造函数中的参数,就像我们从Activity到RecyclerView一样。
您可以使用doSomethingWithToolbar
从RecyclerViewHolder调用方法dummy.doSomethingWithToolbar()
。