我正在开发一个新的应用程序(第一个大项目),我面临着GridView自定义视图适配器的问题,在网络上寻求答案2小时后我仍无法解决它。
我的目标是在创建片段时动态地将Buttons放入GridLayout,在我显示我的代码之前我想说我在Fragments和Android本身非常新,所以我可能会错过一些愚蠢的东西。 / p>
问题是它只显示传递给构造函数的字符串数组中的2个随机按钮,我试图调试,问题似乎是在getView()方法中。
碎片onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tutor_signup_step_one, container, false);
ButterKnife.bind(this, rootView);
// professions
addProfeesions(professionsList);
gridViewButtonAdapter = new GridViewButtonAdapter(getContext(), professions);
gridView.setAdapter(gridViewButtonAdapter);
return rootView;
}
public void addProfeesions(String[] profession_list) {
professions.clear();
for(int i = 0; i < profession_list.length; i++) {
professions.add(profession_list[i]);
}
}
片段XML:
<android.support.constraint.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:id="@+id/tutorSignupLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.SignupStepOneActivity">
<TextView
android:id="@+id/additionalInfoTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:text="@string/signup_step_one_under_welcome"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.123"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/additionalInfoTextView"
app:layout_constraintVertical_bias="0.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="39dp"
android:gravity="center"
android:text="Choose your profession" />
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>
</LinearLayout>
</ScrollView>
我的自定义ViewAdapter:
public class GridViewButtonAdapter extends BaseAdapter {
private List<String> list;
private Context context;
public GridViewButtonAdapter(Context context, List<String> list) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.row_grid, viewGroup, false);
} else {
LinearLayout linearLayout = view.findViewById(R.id.gridLinear);
linearLayout.addView(addButton(list.get(i)));
}
return view;
}
private Button addButton(String text) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Button button = (Button) LayoutInflater.from(context).inflate(R.layout.button, null);
button.setLayoutParams(params);
button.setText(text);
Log.i("INFO BUTTON", text);
return button;
}
row_grid.XML(用于填充GridLayout单元格)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridLinear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
对于任何有关此问题的帮助,我都会非常感激,同时请继续排除故障,谢谢。
答案 0 :(得分:0)
如上所述我已经解决了缺少按钮的问题,但现在我无法弄清楚如何删除gridView中项目之间的空格。
换句话说,我如何制作一个单元格来包装其内容?
GridVew:
<GridView
android:id="@+id/gridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:numColumns="3" >
</GridView>
row_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/buttonTest"
style="@style/My.Button.One"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/custom_button_1"
android:padding="3dp"
android:text="Button"
android:textSize="12sp"
android:adjustViewBounds="true" />
</LinearLayout>
谢谢, 巴特