微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的片段中的GridView不可见

如何解决我的片段中的GridView不可见

我正在制作一个活动,其中包含2个片段,以显示2种不同的功能。我遇到的问题是我的第一个片段中有一个不可见的网格视图。该应用程序运行时不会崩溃,我只是不知道为什么GridView不可见。

这是有问题的片段:

public class PaletteFragment extends Fragment {

GridView grid;
TextView label;

public PaletteFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View palette =  inflater.inflate(R.layout.fragment_palette,container,false);

    label = palette.findViewById(R.id.label);
    label.setText(getResources().getString(R.string.labelText));

    grid = palette.findViewById(R.id.gridView);
    grid.setAdapter(new CustomAdapter(palette.getContext()));

    return palette;
}

}

这是该片段的XML:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<GridView
    android:id="@+id/gridView"
    android:layout_width="0dp"
    android:layout_height="317dp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0"
    tools:visibility="invisible" />

<TextView
    android:id="@+id/label"
    android:layout_width="0dp"
    android:layout_height="119dp"
    android:gravity="center"
    android:text="@string/labelText"
    android:visibility="visible"
    app:layout_constraintBottom_toTopOf="@+id/gridView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

如果有帮助,这是我在GridView上使用的适配器:

public class CustomAdapter extends BaseAdapter {

Context context;
ArrayList<Integer> colorVal = new ArrayList<Integer>();

public CustomAdapter(Context context) {
    this.context = context;
}

@Override
public int getCount() {
    return colorVal.size();
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public View getView(int i,View View,ViewGroup viewGroup) {
    String[] colorName = context.getResources().getStringArray(R.array.colorName);

    colorVal.add(Color.RED);
    colorVal.add(Color.BLUE);
    colorVal.add(Color.BLACK);
    colorVal.add(Color.GREEN);
    colorVal.add(Color.WHITE);
    colorVal.add(Color.LTGRAY);
    colorVal.add(Color.DKGRAY);
    colorVal.add(Color.magenta);
    colorVal.add(Color.YELLOW);
    colorVal.add(Color.CYAN);

    TextView text = new TextView(context);
    text.setText(colorName[i]);
    text.setBackgroundColor(colorVal.get(i));
    text.setTextSize(22);
    text.setPadding(20,20,20);

    if(text.getText().toString().equals(context.getResources().getString(R.string.black))){
        text.setTextColor(Color.WHITE);
    }

    return text;
}

}

解决方法

调用适配器时,您的colorVal列表为空,因此getCount始终返回0。这就是为什么不会调用getView的原因。

只需移动您的代码以将colorVal列表更新到构造函数,就可以了!

public CustomAdapter(Context context) {
    this.context = context;

    colorVal.add(Color.RED);
    colorVal.add(Color.BLUE);
    colorVal.add(Color.BLACK);
    colorVal.add(Color.GREEN);
    colorVal.add(Color.WHITE);
    colorVal.add(Color.LTGRAY);
    colorVal.add(Color.DKGRAY);
    colorVal.add(Color.MAGENTA);
    colorVal.add(Color.YELLOW);
    colorVal.add(Color.CYAN);
}

您还可以在片段中创建此列表,并将其发送到适配器的构造函数中。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?