如何使用多个RelativeLayout.LayoutParams动态添加到布局

时间:2015-06-02 14:57:08

标签: android listview android-relativelayout

在我的片段中,我希望listview中的每个自定义项看起来如下:

名称

SetsXReps

[] [] [] [] [] --->文本视图数组

[] [] [] [] [] --->复选框数组

textview和复选框的数量取决于适配器中的项目,因此我需要动态创建该部分。我无法动态地将这些元素添加到我在xml中部分定义的relativelayout。这是我的片段类,后跟我的自定义列表项的xml。布局的id是list_item_exercise_in_workout。我有添加textview行的代码,但是添加复选框时遇到了麻烦。我在下面评论了我尝试添加但不起作用的代码。问题来自第二个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" />

1 个答案:

答案 0 :(得分:0)

您实际上可以为ListView提供一些自定义布局,这样您就不必动态添加它们。

为此,您需要覆盖适配器的getItemViewTypegetViewTypeCount方法。

getViewTypeCount只需返回一个整数==您将拥有的不同布局的数量。

getItemViewType占据一个位置,然后您可以评估该位置的项目并返回不同的int,具体取决于对象类型或您需要评估的任何参数。

然后,在getView中,您可以为该位置调用getItemViewType,创建一个开关或if块以加载相应的视图类型,然后瞧,您有不同的xml布局,而不是尝试单独构建它们。