如何使用多个 RelativeLayout.LayoutParams 动态添加到布局
how to add to layout dynamically with multiple RelativeLayout.LayoutParams
在我的片段中,我希望列表视图中的每个自定义项目如下所示:
姓名
SetsXReps
[][][][][] ---> 文本视图数组
[][][][][] ---> 复选框数组
文本视图和复选框的数量取决于适配器中的项目,因此我需要动态创建该部分。我无法将这些元素动态添加到我在 xml 中部分定义的相对布局中。这是我的片段 class,后面是自定义列表项的 xml。布局的 ID 是 list_item_exercise_in_workout。我有添加文本视图行的代码,但是 运行 在添加复选框时遇到了麻烦。我在我尝试添加但不起作用的代码下方发表了评论。问题来自第二个 RelativeLayout.LayoutParams 以及我如何尝试将第二行添加到视图中。提前致谢,非常感谢给我的任何帮助。
public class WorkoutFragment extends Fragment
{
public static final String EXTRA_ALREADYCREATED_ID = "alreadyCreated";
private ExAdapter adapter;
private ListView listView;
private ArrayList<Set> sets;
private Workout w;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID workoutId = (UUID) getArguments().getSerializable(EXTRA_ALREADYCREATED_ID);
w = WorkoutMASTER.get(getActivity()).getWorkout(workoutId);
sets = w.getSets();
}
public View onCreateView(LayoutInflater inflater, ViewGroup parent,Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_workout, parent, false);
listView = (ListView) v.findViewById(R.id.listWorkout);
adapter = new ExAdapter(getActivity(), R.layout.fragment_workout, R.id.listWorkout, sets);
listView.setAdapter(adapter);
return v;
}
public static WorkoutFragment newInstance(UUID workoutId)
{
Bundle args = new Bundle();
args.putSerializable(EXTRA_ALREADYCREATED_ID, workoutId);
WorkoutFragment fragment = new WorkoutFragment();
fragment.setArguments(args);
return fragment;
}
private class ExAdapter extends ArrayAdapter<Set>
{
public ExAdapter(Context c, int resource, int textViewResourceId, ArrayList<Set> sets ) {
super(c, 0, sets);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
//if we werent given a view, inflate one
if(convertView == null)
{
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_exercise_in_workout, null);
}
Set s = getItem(position);
RelativeLayout container = (RelativeLayout) convertView.findViewById(R.id.relativeLayoutEx);
TextView name = (TextView) convertView.findViewById(R.id.exName);
name.setText(s.getName());
TextView setsReps = (TextView) convertView.findViewById(R.id.setsXreps);
setsReps.setText(s.getSets() + "X" + s.getReps());
CheckBox[] checkBoxes = new CheckBox[s.getSetsInt()];
EditText[] weightBoxes = new EditText[s.getSetsInt()];
//initialize weightBoxes's text to the inputted weight
for(int i = 0; i < weightBoxes.length+1; i++)
{
weightBoxes[i] = new EditText(getActivity());
weightBoxes[i].setText(s.getWeight());
weightBoxes[i].setId(i);
checkBoxes[i] = new CheckBox(getActivity());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//statement here to add the edit text's horizontally
if(i==0)
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
else
params.addRule(RelativeLayout.RIGHT_OF, i-1);
params.leftMargin = 107;
//params2.addRule(RelativeLayout.BELOW, i);
//params2.leftMargin = 107;
container.addView(weightBoxes[i], params);
//container.addView(checkBoxes[i], params2);
}
return convertView;
}
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeLayoutEx"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="@+id/exName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SetsXReps"
android:id="@+id/setsXreps"
android:layout_below="@id/exName"
android:layout_alignParentLeft="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_below="@+id/setsXreps"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="112dp" />
您实际上可以给 ListView
一些自定义布局,这样您就不必动态添加它们。
为此,您需要覆盖适配器的 getItemViewType
和 getViewTypeCount
方法。
getViewTypeCount
只需要 return 一个整数 == 您将拥有的不同布局的数量。
getItemViewType
占据一个位置,然后您可以评估该位置的项目,并且 return 不同的 int
取决于对象类型或您需要评估的任何参数。
然后,在getView
中,你可以为那个位置调用getItemViewType
,创建一个开关或if块来加载合适的视图类型,然后瞧,你有不同的xml 布局,而不是尝试单独构建它们。
在我的片段中,我希望列表视图中的每个自定义项目如下所示:
姓名
SetsXReps
[][][][][] ---> 文本视图数组
[][][][][] ---> 复选框数组
文本视图和复选框的数量取决于适配器中的项目,因此我需要动态创建该部分。我无法将这些元素动态添加到我在 xml 中部分定义的相对布局中。这是我的片段 class,后面是自定义列表项的 xml。布局的 ID 是 list_item_exercise_in_workout。我有添加文本视图行的代码,但是 运行 在添加复选框时遇到了麻烦。我在我尝试添加但不起作用的代码下方发表了评论。问题来自第二个 RelativeLayout.LayoutParams 以及我如何尝试将第二行添加到视图中。提前致谢,非常感谢给我的任何帮助。
public class WorkoutFragment extends Fragment
{
public static final String EXTRA_ALREADYCREATED_ID = "alreadyCreated";
private ExAdapter adapter;
private ListView listView;
private ArrayList<Set> sets;
private Workout w;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UUID workoutId = (UUID) getArguments().getSerializable(EXTRA_ALREADYCREATED_ID);
w = WorkoutMASTER.get(getActivity()).getWorkout(workoutId);
sets = w.getSets();
}
public View onCreateView(LayoutInflater inflater, ViewGroup parent,Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_workout, parent, false);
listView = (ListView) v.findViewById(R.id.listWorkout);
adapter = new ExAdapter(getActivity(), R.layout.fragment_workout, R.id.listWorkout, sets);
listView.setAdapter(adapter);
return v;
}
public static WorkoutFragment newInstance(UUID workoutId)
{
Bundle args = new Bundle();
args.putSerializable(EXTRA_ALREADYCREATED_ID, workoutId);
WorkoutFragment fragment = new WorkoutFragment();
fragment.setArguments(args);
return fragment;
}
private class ExAdapter extends ArrayAdapter<Set>
{
public ExAdapter(Context c, int resource, int textViewResourceId, ArrayList<Set> sets ) {
super(c, 0, sets);
}
public View getView(final int position, View convertView, ViewGroup parent)
{
//if we werent given a view, inflate one
if(convertView == null)
{
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_exercise_in_workout, null);
}
Set s = getItem(position);
RelativeLayout container = (RelativeLayout) convertView.findViewById(R.id.relativeLayoutEx);
TextView name = (TextView) convertView.findViewById(R.id.exName);
name.setText(s.getName());
TextView setsReps = (TextView) convertView.findViewById(R.id.setsXreps);
setsReps.setText(s.getSets() + "X" + s.getReps());
CheckBox[] checkBoxes = new CheckBox[s.getSetsInt()];
EditText[] weightBoxes = new EditText[s.getSetsInt()];
//initialize weightBoxes's text to the inputted weight
for(int i = 0; i < weightBoxes.length+1; i++)
{
weightBoxes[i] = new EditText(getActivity());
weightBoxes[i].setText(s.getWeight());
weightBoxes[i].setId(i);
checkBoxes[i] = new CheckBox(getActivity());
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//statement here to add the edit text's horizontally
if(i==0)
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
else
params.addRule(RelativeLayout.RIGHT_OF, i-1);
params.leftMargin = 107;
//params2.addRule(RelativeLayout.BELOW, i);
//params2.leftMargin = 107;
container.addView(weightBoxes[i], params);
//container.addView(checkBoxes[i], params2);
}
return convertView;
}
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeLayoutEx"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="@+id/exName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="SetsXReps"
android:id="@+id/setsXreps"
android:layout_below="@id/exName"
android:layout_alignParentLeft="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_below="@+id/setsXreps"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="112dp" />
您实际上可以给 ListView
一些自定义布局,这样您就不必动态添加它们。
为此,您需要覆盖适配器的 getItemViewType
和 getViewTypeCount
方法。
getViewTypeCount
只需要 return 一个整数 == 您将拥有的不同布局的数量。
getItemViewType
占据一个位置,然后您可以评估该位置的项目,并且 return 不同的 int
取决于对象类型或您需要评估的任何参数。
然后,在getView
中,你可以为那个位置调用getItemViewType
,创建一个开关或if块来加载合适的视图类型,然后瞧,你有不同的xml 布局,而不是尝试单独构建它们。