【问题标题】:Displaying layouts inside DraggableGridView在 Draggable GridView 中显示布局
【发布时间】:2015-11-04 22:13:58
【问题描述】:

我正在尝试制作一个图像+标题对的网格,用户将能够通过拖放重新排列。为此,我使用 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);

它会显示一个文本和图像网格,但它们不是配对的 - 你可以用图像等切换文本的位置。

当我尝试插入包含 Image+ 文本的布局时:

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 个孩子在场。

我正在使用的布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content">    

    <ImageView
        android:id="@+id/limage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ImageView>

    </TableRow>

    <TableRow android:layout_width="wrap_content"
        android:layout_height="wrap_content">  

    <TextView
        android:id="@+id/ltext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:gravity="center_horizontal"
        android:textColorHighlight="#656565">
    </TextView>
    </TableRow>   

</TableLayout>

【问题讨论】:

  • 我也有同样的问题,我已经在github 上打开了一个问题
  • 在进一步测试中,视图在那里,我可以长按空白屏幕并移动它们并重新排序。只是看不到他们....

标签: java android android-layout


【解决方案1】:

编辑

可以找到更好的版本here。它基于 DraggableGridView 并具有更多功能,正在积极开发中并且相当稳定。

原帖:
我一直在研究这个库,我目前让它按照你希望它使用数据库的方式工作,我正在努力将它移动到一个 ArrayAdapter。你可以找到我的叉子here

【讨论】:

  • 感谢您这样做,但我在实施它时遇到了问题。拖动带有文本的图标后,它会跳回到原来的位置。另外,我希望 GridView 不滚动。还有:设置项目点击监听器的最佳位置是什么?
  • 我实际上仍在处理其中的一些问题,有一些图标无法交换的错误。如果您不希望它滚动,您可以遍历并删除所有滚动方法,并在使用它的任何地方删除滚动变量。至于OnItemClickListener,我一直在 DraggableGrid 视图中使用 setter 来使用您在主活动中创建的侦听器设置局部变量,然后从 DraggableGridView 中的 OnItemClick 方法调用它,但我仍然无法解决它虽然调用了。
  • 一如既往,随时提交拉取请求,我希望还能清理很多东西。
  • 是的.. 我得到了图标交换,但另一个错误:刚刚交换的图标也显示在位置 0。并且,交换任何图标后,位置 0 的图标被突出显示 - 为什么在哪里?
  • 其他人有这些问题——我前段时间解决了这个问题,但它在一个包含我公司专有的其他代码的私人仓库中。我将尝试分离修复并最终将它们与 AdapterView 版本一起推送。在那之前,看看卢克的叉子。
【解决方案2】:

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

希望对您有所帮助。 从另一个帖子链接找到解决方案:Imageview and textview not displaying in view?

【讨论】:

  • 强烈建议不要在 Stack Overflow 上发布仅包含链接(或已发布的一些信息和链接)的答案,尤其是如果您是文章的作者。要么删除此答案并将链接发布为评论,要么编辑答案以总结文章的相关部分,使其在没有链接的情况下独立存在(不过,您可以保留链接)。谢谢。
  • 感谢@Dethariel 的反馈。
  • 哇,我问这个问题已经这么多年了:) 我希望你的回答能帮助到别人。
猜你喜欢
  • 1970-01-01
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 2018-05-31
相关资源
最近更新 更多