【问题标题】:how to add to layout dynamically with multiple RelativeLayout.LayoutParams如何使用多个 RelativeLayout.LayoutParams 动态添加到布局
【发布时间】:2015-06-02 14:57:08
【问题描述】:

在我的片段中,我希望列表视图中的每个自定义项如下所示:

名字

设置XReps

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

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

文本视图和复选框的数量取决于适配器中的项目,因此我需要动态创建该部分。我无法将这些元素动态添加到我在 xml 中部分定义的相对布局中。这是我的片段类,后跟自定义列表项的 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" />

【问题讨论】:

    标签: android listview android-relativelayout


    【解决方案1】:

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

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      相关资源
      最近更新 更多