在按钮上设置墙纸单击RecyclerView

时间:2019-01-29 11:13:58

标签: java android onclick

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

这是我的 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>

这是每个列表项的设计。

enter image description here

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

2 个答案:

答案 0 :(得分: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;
}

快乐编码!

答案 1 :(得分:0)

onBindViewHolder中使用Glide或Picasso加载图像,并使用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);
        }
    }