允许用户 Select GridView 中的多个项目
Allowing User to Select multiple items in a GridView
通过 Android 进行编码的新手,对 Java 什么都一无所知。我一直在整理发布的其他问题,但我很挣扎。
我在 OnCreate 方法的顶部创建了一个名为 'images2' 的数组,并将我的可绘制对象中的一堆颜色一个一个地放入其中。那部分效果很好,但我一直在尝试创建一个 onClickListner ,它有一个 OnClick 方法,当你点击它时,它会将可绘制对象更改为另一种颜色。我希望它们都变成相同的颜色。我知道我可以通过编程方式设置选择器:gridview2.setSelector(new ColorDrawable(Color.BLACK));但是在后端或 XMl 代码中更改选择器并不能帮助我获得新的可绘制对象。所以我在适配器中创建了另一个 listArray,我可以在 main 中调用它。我在 onClick 中有一个索引,它将遍历所有 25 个可能的项目位置,我想创建它以便它将 drawable 更改为数组中的每个位置。它还将当前点击的位置添加到数组 myarray.add(position);但我刚刚意识到价值可能不会被保存。 arrayList 声明为 public 但它在适配器中声明为 public 那么 OnCreate 中的更改是否无效?对不起。这绝对是一个菜鸟问题。
/**
* Created by Jordan on 3/25/2017.
*/
public class Welcome extends AppCompatActivity implements AdapterView.OnItemClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final List<Integer> images2 = new ArrayList<Integer>();
images2.add(R.drawable.grid1);images2.add(R.drawable.grid6);
images2.add(R.drawable.grid2);images2.add(R.drawable.grid2);
images2.add(R.drawable.grid3);images2.add(R.drawable.grid1);
images2.add(R.drawable.grid4);images2.add(R.drawable.grid3);
images2.add(R.drawable.grid5);images2.add(R.drawable.grid2);
images2.add(R.drawable.grid6);images2.add(R.drawable.grid6);
images2.add(R.drawable.open);images2.add(R.drawable.grid4);
images2.add(R.drawable.grid1);images2.add(R.drawable.grid1);
images2.add(R.drawable.grid6);images2.add(R.drawable.grid4);
images2.add(R.drawable.grid4);images2.add(R.drawable.grid2);
images2.add(R.drawable.grid3);images2.add(R.drawable.grid3);
images2.add(R.drawable.grid6);images2.add(R.drawable.grid1);
images2.add(R.drawable.grid5);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
final GridView gridview2 = (GridView) findViewById(R.id.welcomeGrid);
final Welcome.ImageAdapter mAdapter = new Welcome.ImageAdapter(this, images2);
gridview2.setAdapter(mAdapter);
gridview2.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);
gridview2.setItemChecked(2, true);
gridview2.setOnItemClickListener(this);
gridview2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// ArrayList<Integer> clickedStatus = new ArrayList<Integer>();
ArrayList<Integer> clickedStatus = new ArrayList<>(25);
clickedStatus.add(position);
if (position == 12){
setContentView(R.layout.login);
}
else if(position != 12) {
// no matter, whether the item has already been clicked or not I want this toast to pop up to promp the user to actually enter the app and sign in
Toast.makeText(Welcome.this, "You selected " + position+ ".. Please select middle tile to enter!",
Toast.LENGTH_SHORT).show();
if(clickedStatus.contains(position)){
// as soon as it's noticed that the an integer matching the current position is currently stored in the
// array list (clickedStatus), I want it removed right below
// was passing item in place of position into the remove(). Don't know why Integer item = (Integer) parent.getItemAtPosition(position);
//I want to go through 25 numbers and
for(int i=0; i<25;i++ ){
// compare those ints to what is in contained in the clickedstatus
if(clickedStatus.contains(i)){
// Drawable Marker = getResources().getDrawable(R.drawable.celeb1);
images2.set(i, R.drawable.celeb1);
//for every int that is stored in click status I want
// to call the item and change the background of that item to a specified drawable file.
}
}
}
else {
clickedStatus.add(position);
// images2.set(position, );
for(int i=1; i<25;i++ ){
// compare those ints to what is in contained in the clickedstatus
if(clickedStatus.contains(i)){
// Drawable Marker = getResources().getDrawable(R.drawable.celeb1);
images2.set(i, R.drawable.celeb1);
//for every int that is stored in click status I want
// to call the item and change the background of that item to a specified drawable file.
}
}
}
// gridview2.setSelector(new ColorDrawable(Color.BLACK));
// mAdapter.notifyDataSetChanged();
}
}
}
);
}
// I think this got generated automatically at some point somehow. Might of been with the multichoicemode line above ^
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
//IMAGES ARE REFERENCED HERE V---
private List<Integer> mImages2;
public ArrayList<Integer> clickedStatus = new ArrayList<Integer>();
//create array to store whether an item has been clicked or not. needs to be public!!
//check to determine the case in the below method
// IGNORE THIS
// private int selectedPosition = -1;
//public void setSelectedPosition(int position) {
// selectedPosition = position;
//}
public ImageAdapter( final Context c, final List<Integer> images2){
mContext = c;
mImages2 = images2;
}
public int getCount() {
return mImages2.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return 1;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(140, 120));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
if(clickedStatus.contains(position)){
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.GREEN);
}
imageView.setImageResource(mImages2.get(position));
return imageView;
}
;}
}
我创建并完成了一组不同的数组,用于保存可能导致获胜的五种选择组合。标题为对角线(1、2、3、4、5)、水平(1、2、3、4、5)、垂直(1、2、3、4、5)等。单击后,一个值将存储在ListArray 的适当位置,我有相关检查(if 语句),每次单击按钮时都会循环。不会显着降低应用程序的速度,在纸面上看起来很丑陋,但在不使用复选框的情况下非常实用。如果有人有兴趣查看代码,他们可以发表评论。
通过 Android 进行编码的新手,对 Java 什么都一无所知。我一直在整理发布的其他问题,但我很挣扎。 我在 OnCreate 方法的顶部创建了一个名为 'images2' 的数组,并将我的可绘制对象中的一堆颜色一个一个地放入其中。那部分效果很好,但我一直在尝试创建一个 onClickListner ,它有一个 OnClick 方法,当你点击它时,它会将可绘制对象更改为另一种颜色。我希望它们都变成相同的颜色。我知道我可以通过编程方式设置选择器:gridview2.setSelector(new ColorDrawable(Color.BLACK));但是在后端或 XMl 代码中更改选择器并不能帮助我获得新的可绘制对象。所以我在适配器中创建了另一个 listArray,我可以在 main 中调用它。我在 onClick 中有一个索引,它将遍历所有 25 个可能的项目位置,我想创建它以便它将 drawable 更改为数组中的每个位置。它还将当前点击的位置添加到数组 myarray.add(position);但我刚刚意识到价值可能不会被保存。 arrayList 声明为 public 但它在适配器中声明为 public 那么 OnCreate 中的更改是否无效?对不起。这绝对是一个菜鸟问题。
/**
* Created by Jordan on 3/25/2017.
*/
public class Welcome extends AppCompatActivity implements AdapterView.OnItemClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final List<Integer> images2 = new ArrayList<Integer>();
images2.add(R.drawable.grid1);images2.add(R.drawable.grid6);
images2.add(R.drawable.grid2);images2.add(R.drawable.grid2);
images2.add(R.drawable.grid3);images2.add(R.drawable.grid1);
images2.add(R.drawable.grid4);images2.add(R.drawable.grid3);
images2.add(R.drawable.grid5);images2.add(R.drawable.grid2);
images2.add(R.drawable.grid6);images2.add(R.drawable.grid6);
images2.add(R.drawable.open);images2.add(R.drawable.grid4);
images2.add(R.drawable.grid1);images2.add(R.drawable.grid1);
images2.add(R.drawable.grid6);images2.add(R.drawable.grid4);
images2.add(R.drawable.grid4);images2.add(R.drawable.grid2);
images2.add(R.drawable.grid3);images2.add(R.drawable.grid3);
images2.add(R.drawable.grid6);images2.add(R.drawable.grid1);
images2.add(R.drawable.grid5);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
final GridView gridview2 = (GridView) findViewById(R.id.welcomeGrid);
final Welcome.ImageAdapter mAdapter = new Welcome.ImageAdapter(this, images2);
gridview2.setAdapter(mAdapter);
gridview2.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE);
gridview2.setItemChecked(2, true);
gridview2.setOnItemClickListener(this);
gridview2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// ArrayList<Integer> clickedStatus = new ArrayList<Integer>();
ArrayList<Integer> clickedStatus = new ArrayList<>(25);
clickedStatus.add(position);
if (position == 12){
setContentView(R.layout.login);
}
else if(position != 12) {
// no matter, whether the item has already been clicked or not I want this toast to pop up to promp the user to actually enter the app and sign in
Toast.makeText(Welcome.this, "You selected " + position+ ".. Please select middle tile to enter!",
Toast.LENGTH_SHORT).show();
if(clickedStatus.contains(position)){
// as soon as it's noticed that the an integer matching the current position is currently stored in the
// array list (clickedStatus), I want it removed right below
// was passing item in place of position into the remove(). Don't know why Integer item = (Integer) parent.getItemAtPosition(position);
//I want to go through 25 numbers and
for(int i=0; i<25;i++ ){
// compare those ints to what is in contained in the clickedstatus
if(clickedStatus.contains(i)){
// Drawable Marker = getResources().getDrawable(R.drawable.celeb1);
images2.set(i, R.drawable.celeb1);
//for every int that is stored in click status I want
// to call the item and change the background of that item to a specified drawable file.
}
}
}
else {
clickedStatus.add(position);
// images2.set(position, );
for(int i=1; i<25;i++ ){
// compare those ints to what is in contained in the clickedstatus
if(clickedStatus.contains(i)){
// Drawable Marker = getResources().getDrawable(R.drawable.celeb1);
images2.set(i, R.drawable.celeb1);
//for every int that is stored in click status I want
// to call the item and change the background of that item to a specified drawable file.
}
}
}
// gridview2.setSelector(new ColorDrawable(Color.BLACK));
// mAdapter.notifyDataSetChanged();
}
}
}
);
}
// I think this got generated automatically at some point somehow. Might of been with the multichoicemode line above ^
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
//IMAGES ARE REFERENCED HERE V---
private List<Integer> mImages2;
public ArrayList<Integer> clickedStatus = new ArrayList<Integer>();
//create array to store whether an item has been clicked or not. needs to be public!!
//check to determine the case in the below method
// IGNORE THIS
// private int selectedPosition = -1;
//public void setSelectedPosition(int position) {
// selectedPosition = position;
//}
public ImageAdapter( final Context c, final List<Integer> images2){
mContext = c;
mImages2 = images2;
}
public int getCount() {
return mImages2.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return 1;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(140, 120));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
if(clickedStatus.contains(position)){
convertView.setSelected(true);
convertView.setPressed(true);
convertView.setBackgroundColor(Color.GREEN);
}
imageView.setImageResource(mImages2.get(position));
return imageView;
}
;}
}
我创建并完成了一组不同的数组,用于保存可能导致获胜的五种选择组合。标题为对角线(1、2、3、4、5)、水平(1、2、3、4、5)、垂直(1、2、3、4、5)等。单击后,一个值将存储在ListArray 的适当位置,我有相关检查(if 语句),每次单击按钮时都会循环。不会显着降低应用程序的速度,在纸面上看起来很丑陋,但在不使用复选框的情况下非常实用。如果有人有兴趣查看代码,他们可以发表评论。