在DraggableGridView中显示布局
我正在尝试制作一个Grid of Image +字幕对,用户可以通过拖放重新排列。 要做到这一点,我使用DraggableGridView – https://github.com/thquinn/DraggableGridView 。
虽然我可以显示TextViews和ImageViews并重新排列它们,但是当我尝试在一个布局中连接它们时,一切都停止显示。
因此,在使用图像和标题解析XML之后,这可以工作:
for(ImagePair pair : pairs){ tv = new TextView(this); tv.setText(pair.getText()); URL imageURL = new URL(pair.getURL()); Bitmap bmp = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream()); imv = new ImageView(this); imv.setImageBitmap(bmp); dgv.addView(tv); dgv.addView(imv);
它显示文本和图像的网格,但它们没有配对 – 您可以用图像等切换文本的位置。
当我尝试插入包含图像+文本的布局时:
for(ImagePair pair : pairs){ Bitmap bmp = BitmapFactory.decodeStream(imageURL.openConnection().getInputStream()); LayoutInflater li = getLayoutInflater(); View v = li.inflate(R.layout.image, null); TextView txtv = (TextView)v.findViewById(R.id.ltext); txtv.setText(pair.getText()); ImageView iv = (ImageView)v.findViewById(R.id.limage); iv.setImageBitmap(bmp); dgv.addView(v);
什么都没有显示。 我可以通过执行任何单一视图
this.setContentView(v);
并且它将正确显示图像+标题对,但尝试将它们插入网格会导致黑屏,尽管网格报告有9个孩子。
我正在使用的布局:
在DraggableGridView.java中添加一行代码可以解决问题。 打开DraggableGridView.java并添加以下行
getChildAt(i).measure(MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY));
之前
getChildAt(i).layout(xy.x, xy.y, xy.x + childSize, xy.y + childSize);
希望有所帮助。 从另一个post链接找到解决方案: Imageview和textview不在视图中显示?
编辑
可以在这里找到更好的版本。 它基于DraggableGridView,具有更多function,在主动开发中相当稳定。
原帖:
我一直在使用这个库,我目前按照你希望它使用数据库的方式工作,我正在努力将它移动到ArrayAdapter。 你可以在这里找到我的叉子