如何从Android应用程序中的任何地方获取运行时权限,如Recyclarview Adapter

时间:2018-08-11 06:14:35

标签: android

我知道如何在活动中请求运行时权限,但是我想知道如何为适配器或从我的android应用程序中的任何位置请求运行时权限。

打击是我的recyclarview适配器代码

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> {
private List<Movie> moviesList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView title, year, genre;

    public MyViewHolder(View view) {
        super(view);
        title = (TextView) view.findViewById(R.id.title);
        genre = (TextView) view.findViewById(R.id.genre);
        year = (TextView) view.findViewById(R.id.year);
    }
}


public MoviesAdapter(List<Movie> moviesList) {
    this.moviesList = moviesList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.movie_list_row, parent, false);

    return new MyViewHolder(itemView);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Movie movie = moviesList.get(position);
    holder.title.setText(movie.getTitle());
    holder.genre.setText(movie.getGenre());
    holder.year.setText(movie.getYear());
}

@Override
public int getItemCount() {
    return moviesList.size();
}
}

谢谢

2 个答案:

答案 0 :(得分:0)

在适配器类中实现ActivityCompat.OnRequestPermissionsResultCallback,因此它将要求实现方法onRequestPermissionsResult

public class MoviesAdapter extends RecyclerView.Adapter<MoviesAdapter.MyViewHolder> implements ActivityCompat.OnRequestPermissionsResultCallback{


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

}

答案 1 :(得分:0)

运行时权限在应用程序中的任何位置查找Here

您可以通过使用以下代码轻松地启动该库:

use dependency
maven { url 'https://jitpack.io' }
dependencies {
implementation 'com.github.irshadsparky:PermissionLib:master-SNAPSHOT'}

并调用如下代码:

PermissionHelper.requestCamera(new PermissionHelper.OnPermissionGrantedListener() {
    @Override
    public void onPermissionGranted() {

    }
});

您可以找到更多

Github