如何制作可变的可绘制对象数组?
How to make a variable Array of drawables?
我必须使用什么类型的列表才能将 ImageView 的背景设置为可变可绘制对象?
例如:
有 6 个可绘制对象:image1
、image2
、image3
、image4
、image5
、image6
如果您选择 image1
和 image4
,我需要这两个图像的列表。
我需要从该列表中随机将 ImageView 设置为其中一张图片。
这怎么可能?
这里也许有帮助
layouts = new int[]{
R.drawable.welcome_slide1,
R.drawable.welcome_slide2,
R.drawable.welcome_slide3,
R.drawable.welcome_slide4};
更准确
ArrayList<Integer> myImageList = new ArrayList<>();
myImageList.add(R.drawable.thingOne);
myImageList.add(R.drawable.thingtwo);
myImageList.add(R.drawable.thingthree);
myImageView.setImageResource(myImageList.get(i));
您首先需要创建一个选择列表,然后创建一个按钮,单击该按钮仅选择所选项目的列表,然后从所选项目列表中设置随机图像。
所以对于布局:
主要布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/selection_list"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/get_selection"
android:text="Show randomly selected"/>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/random_img"/>
</LinearLayout>
然后选择列表的适配器:
public class test_adapter extends RecyclerView.Adapter<test_adapter.view> {
Context cntxt;
public ArrayList<selection_items> selection_items_list;
AdapterView.OnItemClickListener listener;
public test_adapter(Context cntxt, ArrayList<selection_items> selection_items_list)
{
this.cntxt=cntxt;
this.selection_items_list = selection_items_list;
}
public ArrayList<selection_items> selected_items()
{
ArrayList<selection_items> selected_=new ArrayList<>();
for(int i = 0; i< selection_items_list.size(); i++)
{
if(selection_items_list.get(i).selected){
selected_.add(selection_items_list.get(i));
}
}
return selected_;
}
@NonNull
@Override
public view onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(cntxt).inflate(R.layout.img_in_list_test, parent, false);
return new view(view);
}
@Override
public void onBindViewHolder(@NonNull view holder, int position) {
selection_items obj= selection_items_list.get(position);
holder.name.setText(obj.name);
holder.img.setImageDrawable(cntxt.getDrawable(obj.img));
holder.name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
obj.selected=b;
}
});
}
@Override
public int getItemCount() {
return selection_items_list.size();
}
public class view extends RecyclerView.ViewHolder implements View.OnClickListener {
public CheckBox name;
public ImageView img;
public void set_printed()
{
itemView.setBackgroundColor(cntxt.getColor(R.color.primary_semi_transparent));
}
view(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.check);
img = itemView.findViewById(R.id.img);
}
@Override
public void onClick(View view) {
}
}
}
您选择的项目布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="52dp">
<ImageView
android:layout_width="50dp"
android:id="@+id/img"
android:layout_height="50dp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check"/>
</LinearLayout>
终于是你的activity
public class TestActivity extends AppCompatActivity {
RecyclerView selection_view;
Button get_selection;
ImageView random_selected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
selection_view = findViewById(R.id.selection_list);
get_selection = findViewById(R.id.get_selection);
random_selected = findViewById(R.id.random_img);
selection_view.setLayoutManager(new LinearLayoutManager(this));
ArrayList<selection_items> selection_items=new ArrayList<>();
selection_items.add(new selection_items("Image 1",R.drawable.backup_icon));
selection_items.add(new selection_items("Image 2",R.drawable.ic_add_photo));
selection_items.add(new selection_items("Image 3",R.drawable.ic_barcode_scan));
selection_view.setAdapter(new test_adapter(this,selection_items));
get_selection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayList<selection_items> selected_items=((test_adapter)selection_view.getAdapter()).selected_items();
if(selected_items.size()>0){
random_selected.setImageDrawable(getDrawable(selected_items.get(new Random().nextInt(selected_items.size())).img));
}else{
random_selected.setImageDrawable(null);
}
}
});
}
}
我必须使用什么类型的列表才能将 ImageView 的背景设置为可变可绘制对象?
例如:
有 6 个可绘制对象:image1
、image2
、image3
、image4
、image5
、image6
如果您选择 image1
和 image4
,我需要这两个图像的列表。
我需要从该列表中随机将 ImageView 设置为其中一张图片。
这怎么可能?
这里也许有帮助
layouts = new int[]{
R.drawable.welcome_slide1,
R.drawable.welcome_slide2,
R.drawable.welcome_slide3,
R.drawable.welcome_slide4};
更准确
ArrayList<Integer> myImageList = new ArrayList<>();
myImageList.add(R.drawable.thingOne);
myImageList.add(R.drawable.thingtwo);
myImageList.add(R.drawable.thingthree);
myImageView.setImageResource(myImageList.get(i));
您首先需要创建一个选择列表,然后创建一个按钮,单击该按钮仅选择所选项目的列表,然后从所选项目列表中设置随机图像。
所以对于布局: 主要布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/selection_list"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/get_selection"
android:text="Show randomly selected"/>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/random_img"/>
</LinearLayout>
然后选择列表的适配器:
public class test_adapter extends RecyclerView.Adapter<test_adapter.view> {
Context cntxt;
public ArrayList<selection_items> selection_items_list;
AdapterView.OnItemClickListener listener;
public test_adapter(Context cntxt, ArrayList<selection_items> selection_items_list)
{
this.cntxt=cntxt;
this.selection_items_list = selection_items_list;
}
public ArrayList<selection_items> selected_items()
{
ArrayList<selection_items> selected_=new ArrayList<>();
for(int i = 0; i< selection_items_list.size(); i++)
{
if(selection_items_list.get(i).selected){
selected_.add(selection_items_list.get(i));
}
}
return selected_;
}
@NonNull
@Override
public view onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(cntxt).inflate(R.layout.img_in_list_test, parent, false);
return new view(view);
}
@Override
public void onBindViewHolder(@NonNull view holder, int position) {
selection_items obj= selection_items_list.get(position);
holder.name.setText(obj.name);
holder.img.setImageDrawable(cntxt.getDrawable(obj.img));
holder.name.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
obj.selected=b;
}
});
}
@Override
public int getItemCount() {
return selection_items_list.size();
}
public class view extends RecyclerView.ViewHolder implements View.OnClickListener {
public CheckBox name;
public ImageView img;
public void set_printed()
{
itemView.setBackgroundColor(cntxt.getColor(R.color.primary_semi_transparent));
}
view(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.check);
img = itemView.findViewById(R.id.img);
}
@Override
public void onClick(View view) {
}
}
}
您选择的项目布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="52dp">
<ImageView
android:layout_width="50dp"
android:id="@+id/img"
android:layout_height="50dp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/check"/>
</LinearLayout>
终于是你的activity
public class TestActivity extends AppCompatActivity {
RecyclerView selection_view;
Button get_selection;
ImageView random_selected;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
selection_view = findViewById(R.id.selection_list);
get_selection = findViewById(R.id.get_selection);
random_selected = findViewById(R.id.random_img);
selection_view.setLayoutManager(new LinearLayoutManager(this));
ArrayList<selection_items> selection_items=new ArrayList<>();
selection_items.add(new selection_items("Image 1",R.drawable.backup_icon));
selection_items.add(new selection_items("Image 2",R.drawable.ic_add_photo));
selection_items.add(new selection_items("Image 3",R.drawable.ic_barcode_scan));
selection_view.setAdapter(new test_adapter(this,selection_items));
get_selection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ArrayList<selection_items> selected_items=((test_adapter)selection_view.getAdapter()).selected_items();
if(selected_items.size()>0){
random_selected.setImageDrawable(getDrawable(selected_items.get(new Random().nextInt(selected_items.size())).img));
}else{
random_selected.setImageDrawable(null);
}
}
});
}
}