Android:如何从RecyClerViewHolder访问工具栏



问题

如何从回收界持有人那里访问工具栏及其孩子?我想通过LongClick从RecyClerviewHolder中更改工具Bar_main中的文本视图。

我知道

我知道如何使用

内部的激活访问工具栏
    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>

步骤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:在您的活动中实现Ionrecycleclecleclickedlistener

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) {
        }
    }
}

我希望这个答案对您有帮助!

((MainActivity) context).toolbar

或 ((MainActivity)上下文).findViewById(r.id.toolbar)

您不应该将两个完全独立的视图求发。您应该定义一个活动应实现的onLongClick界面,并将其设置在构造函数内部的每个项目视图上供您查看持有人。然后执行工具栏功能。

您可以使用接口实现您的目标,如下所示。

创建一个接口。

public interface DummyInterface {
    public void doSomethingWithToolbar();
}

然后在您的活动上实现此接口。

public class DummyActivity extends Activity implements DummyInterface {
    .
    .
    .
    public void doSomethingWithToolbar() {
        //do something with toolbar
    }
}

然后在构造函数中创建回收器视图时传递此(活动)的参考。喜欢,

new DummyRecyclerView(..., this);

然后在Recyclerview的构造函数中添加一个参数。喜欢,

DummyInterface dummy = new DummyInterface();
public DummyRecyclerView(..., DummyInterface d) {
    dummy = d;
}

然后将虚拟传递到RecyClerviewHolder的构造函数,然后将其添加到RecyClerviewHolder的构造函数中,我们从活动到RecyClerview进行了相同的方式。您可以使用dummy.doSomethingWithToolbar()从RecyClerviewHolder调用方法doSomethingWithToolbar

相关内容

  • 没有找到相关文章