在Android Studio中编写Android应用程序,我有一个GridView来显示一组图像/文本,而我在查找如何访问列表中的特定项目时遇到了麻烦。
如果点击某个项目,我可以对其进行操作(在下面的代码中我更改显示的图像),但我无法弄清楚如何以编程方式访问特定项目(例如,更改显示的图像)
最初我从一个例子中使用了一个ImageAdapter,但是如果我把它更改为使用TextView,我就可以解决这个问题我可以为每个文本分配文本以及一个图像,它可以很好地与Talkback(Android屏幕阅读器)配合使用
我最初也将OnTouchListener添加到活动中,但我认为我现在不需要它,因为我只需要在适配器中实现它。
最后我对我需要尝试访问的内容感到有点困惑 - 我试图将项目从GridView中取出,还是从ImageAdapter中取出?我认为 GridView设置布局并定义将每个项目放在集合中的位置,ImageAdapter是一个容器,用于定义集合中每个项目属性的项目数组,是吗?
亲切的问候
昆汀。
public class multitap extends Activity implements View.OnTouchListener {
private GridView myGridView;
public int numRows;
public int numCols;
public int currentChoice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multitap);
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
numRows = 1;
numCols = 1;
} else {
numRows = extras.getInt("NUMBER_OF_GAME_ROWS");
numCols = extras.getInt("NUMBER_OF_GAME_COLS");
}
} else {
numRows = (int) savedInstanceState.getSerializable("NUMBER_OF_GAME_ROWS");
numCols = (int) savedInstanceState.getSerializable("NUMBER_OF_GAME_COLS");
}
myGridView = (GridView)
findViewById(R.id.myGridView);
myGridView.setNumColumns(numCols);
myGridView.setAdapter(new ImageAdapter(this));
// now that everything is initialised, choose a random image tile.
Random r = new Random();
choice = r.nextInt(numCols*numRows);
// This is the main thing I can't figure out - what do I need to replace "DoSomethingToFindItem" with?
chosenItem = DoSomethingToFindItem(choice);
// change the image of the chosen tile.
chosenItem.setBackgroundResource(R.drawable.newImage);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// turns out I don't need this as onTouch in adapter collects touch event
return false;
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
//not really sure what mConext is?
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() { return numCols*numRows; }
public Object getItem(int position) {
// Is this right? All of the examples on the net use "return null" claiming they it's not needed (for their example)
return this;
}
public long getItemId(int position) {
// I know I should return something other than position here, but don't know what?
// again every example doesn't feel the need for this function.
return position;
}
// create a new TextView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if (convertView == null) { // if it's not recycled, initialize some attributes
textView = new TextView(mContext);
} else {
textView = (TextView) convertView;
}
textView.setBackgroundResource(R.drawable.blankcell);
textView.setText("" + (position + 1));
textView.setTextColor(getResources().getColor(android.R.color.transparent));
textView.setId(position);
textView.setMaxHeight(parent.getHeight() / numRows);
textView.setMinimumHeight(parent.getHeight() / numRows);
textView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int position;
position = v.getId();
// looking to get the first touch of any finger - this part works.
if ((event.getAction() == MotionEvent.ACTION_DOWN) || (event.getAction() == MotionEvent.ACTION_POINTER_DOWN)) {
TextView textView;
if (v == null) { // if it's not recycled, initialize some attributes
textView = new TextView(mContext);
} else {
textView = (TextView) v;
}
// I can set a specific image by clicking on it.
textView.setBackgroundResource(R.drawable.clickedImage);
return true;
} else return false;
}
;});
return textView;
}
}
}
答案 0 :(得分:0)
好的,就像往常一样,我最初遇到了几个问题。一个是访问GridView中的项目,Rami的答案(来自:How to find element inside a gridview in Android?的答案)是正确的:
// newChoice is an int set to something in the range 0...myGridView.getChildCount()
v = (TextView) myGridView.getChildAt(newChoice);
v.setBackgroundResource(themeImages[ACTIVE1]);
我最初没有意识到的第二个问题是,我试图从视图中获取的信息中的逻辑在绘制视图后会起作用,但是因为此代码设法在之前执行视图都已完成绘制,值不是我所期望的,这意味着标记为可见的项目的高度为0等。我发现此代码有帮助:getWidth() and getHeight() of View returns 0
final View theParent = parent;
final TextView theText = textView;
textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
theText.post(new Runnable() {
public void run() {
// Now height is ready
theText.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, (theParent.getHeight() / numRows)));
}
});
}
});
感谢Rami的帮助和建议! (我也根据你的建议改变了直接引用图像文件到使用数组)。