【问题标题】:Set Wallpaper on Button Click in RecyclerView在 RecyclerView 中单击按钮设置墙纸
【发布时间】:2019-01-29 20:00:58
【问题描述】:

我正在创建一个壁纸应用程序。该应用程序在每个列表项中都有一个带有图像和按钮的回收器视图。按钮点击用于将相应图像的壁纸设置到主屏幕。我已成功设置回收站视图,但在点击按钮时设置壁纸时遇到问题。

这是我的 activity_main.xml 代码

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycleView"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">


</android.support.v7.widget.RecyclerView>

这是我的 MainActivity.java 文件

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    int images[] = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5,
                    R.drawable.pic6, R.drawable.pic7, R.drawable.pic8, R.drawable.pic9, R.drawable.pic10};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recycleView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new ListAdapter(images));

    }
}

这是我的 ListAdapter 类,它扩展了 RecyclerView.Adapter,这个类也有嵌套类 ListViewHolder,它扩展了 RecyclerView.ViewHolder

public class ListAdapter extends
RecyclerView.Adapter<ListAdapter.ListViewHolder> {

    private int[] images;
    public ListAdapter(int[] images){
        this.images = images;
    }

    @NonNull
    @Override
    public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View view = inflater.inflate(R.layout.list_item, viewGroup, false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ListViewHolder listViewHolder, int i) {
        int index = images[i];
        listViewHolder.imageView.setImageResource(index);
    }

    @Override
    public int getItemCount() {
        return images.length;
    }

    public class ListViewHolder extends RecyclerView.ViewHolder{

        ImageView imageView;
        Button setWallpaper;

        public ListViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.my_images);
            setWallpaper = itemView.findViewById(R.id.setWallpaper);
        }
    }
}

这是我的 list_item.xml 文件

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/my_images"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/pic1"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>

    <Button
        android:id="@+id/setWallpaper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="20dp"
        android:layout_marginBottom="20dp"/>

</RelativeLayout>

这是每个列表项的设计。

现在我想设置点击按钮设置相应的壁纸到主屏幕。我在哪里放置 onClick() 方法以及如何设置墙纸时遇到了麻烦。

【问题讨论】:

  • onClick 到您放入的按钮 ListViewHolder 。使用getAdapterPosition() 获取ListViewHolder 内的点击项目位置。
  • 不,我的问题有点不同,我在提问之前已经检查了很多解决方案。
  • 好的,让我试试这个
  • 我现在遇到了 WallpaperManager 的问题,我不知道我必须在 WallpaperManager.getInstance() 方法中传递哪个上下文。

标签: java android onclick


【解决方案1】:

在 bindviewholder 里面做这样的事情:

holder.setwallpaper.setOnClickListener(v -> {
        try {
       WallpaperManager wallpaperManager = WallpaperManager.getInstance(mcontext); 
       Drawable drawable = imageview.getDrawable(position);
//or if the above line of code doesn't work try fetching the image from your array list
imagelist.get(position).image
                Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                wallpaperManager.setBitmap(bitmap);
        } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }


            });

我已经给了你逻辑,如果你遇到任何问题,请尝试联系我。

要传递上下文,请执行以下操作: 在你的 mainactivity 类中:

public void initializeAdapter()
    {
        absadapter localabsadapter = new absadapter(exlist,abs.this);
        recyclerView.setAdapter(localabsadapter);
    }

在您的回收站视图中:

Context mContext;
absadapter(List exList,Context context) {
        this.exList= exList;
        this.mContext = context;
}

编码愉快!

【讨论】:

  • 问题出在WallpaperManager.getInstance(mcontext),你说的mcontext是什么意思?
  • @M Umer 你需要传递你的活动上下文,同时在活动类中设置适配器在构造函数中传递你的活动上下文
  • @M Umer 检查我的更新答案我已经给出了一个演示代码希望它有所帮助;)
  • 完美,非常感谢!
  • 不客气 ;)
【解决方案2】:

使用 Glide 或 Picasso 在 onBindViewHolder 中加载图像,使用 Glide

Glide.with(this).load("image_url").into(imageView);

然后在onBindViewHolder中设置OnClickListener作为例子

holder.setWallpaper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

onClick 内将图像转换为位图并设置壁纸。您可以为此使用 Glide

Bitmap bitmapImage;
Glide.with(this)
                .asBitmap()
                .load("image_url")
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        bitmapImage = resource;
                    }
                });

然后执行SetWallpaperTask

new SetWallpaperTask().execute();

SetWallpaperTask() 这样的类

private class SetWallpaperTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Long doInBackground(Void... voids) {
            try {
                WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
                wallpaperManager.setBitmap(bitmapImage);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Long aLong) {
            super.onPostExecute(aLong);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多