问题描述
我正在制作一个活动,其中包含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);
}
您还可以在片段中创建此列表,并将其发送到适配器的构造函数中。