代码之家  ›  专栏  ›  技术社区  ›  M Umer

在“回收视图”中单击按钮设置墙纸

  •  -1
  • M Umer  · 技术社区  · 5 年前

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

    这是我的 活动\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(主活动.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));
    
        }
    }
    

    这是我的 列表适配器 扩展RecyclerView的类。适配器也具有嵌套类 听话者 扩展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);
            }
        }
    }
    

    这是我的 ListIsIt.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 回复  |  直到 5 年前
        1
  •  1
  •   Mr. Patel    5 年前

    在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;
    }
    

    快乐编码!

        2
  •  0
  •   J.D    5 年前

    在图像中加载glide或picasso onBindViewHolder 与滑翔

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

    然后在里面放上一个硅碳棒 OnBindViewHolder 作为例子

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

    里面 onClick 将图像转换为位图并设置墙纸。你可以用滑翔机

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