如何删除片段内的视图

时间:2014-08-04 15:30:03

标签: android fragment

我刚接触到Android dev和片段一直给我带来了困难。

基本上我正在创建一个应用程序,要求用户每周输入他的时间表(学校)6天。所以我每天都制作一个片段(即6个片段),这些片段中的每一个都有一个添加主题底部的按钮,单击时添加xml布局。我已经想出如何使用xml文件中的布局添加一个edittext字段和两个按钮(一个称为数据,另一个称为删除),现在单击删除按钮,我希望删除该特定行。我该怎么做呢?

public class NewTimeTableMonday extends Fragment {

    Context context;            // context is needed to use inflater outside on create view
    View view;                  // view needs to be passed for (find view by id)
    private int unique_id = 0;  // for dynamic Id allocation when New Fields are created
    private final List<TextView> SUBJECT_NAME = new ArrayList<TextView>();

    public NewTimeTableMonday() {
                                // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
                                // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_new_time_table_monday,
                container, false);
        context = container.getContext();

                                // adding two entries by default
        AddNewSubjectLine(view);
        AddNewSubjectLine(view);

        // for button +Add Another
        Button button_AddAnother = (Button) view.findViewById(R.id.AddAnother);
        button_AddAnother.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                AddNewSubjectLine(view);
                                        // Adds another Subject Entry Line on the button being clicked
            }

        });

        // for button x

        Button button_crossDelete = (Button) view.findViewById(R.id.Delete);
        button_crossDelete.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                DeleteSubjectLine(v);
                // Deletes that particular line when x is clicked
            }

        });

        // For Button Done
        Button button_Done = (Button) view.findViewById(R.id.Done);
        button_Done.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // viewPager.setCurrentItem passes the arg0 value to getItem
                // to move to page 2 when done is clicked
                CreateNewTimetable.viewPager.setCurrentItem(1, true);
            }

        });

        return view;// because OnCreatView is of type View

    }

这是addsubjectline函数

public void AddNewSubjectLine(View view) {

        // To display another SUbject Entry Line
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View subjectLayout = inflater.inflate(R.layout.new_entry_line, null);
        LinearLayout layout = (LinearLayout) view
                .findViewById(R.id.MondayLayout);
        layout.addView(subjectLayout);

        /** Adding to list **/
        // Subject Name
        EditText subName = (EditText) view.findViewById(R.id.SubName);
        subName.setId(unique_id);
        SUBJECT_NAME.add(subName);

        // Time Button
        Button button_EnterTime = (Button) view.findViewById(R.id.Time);
        button_EnterTime.setId(100 + unique_id);

        // Delete button
        Button deleteSub = (Button) view.findViewById(R.id.Delete);
        deleteSub.setId(200 + unique_id);

        final ScrollView scrollView = (ScrollView) view
                .findViewById(R.id.scrollViewMonday);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                scrollView.fullScroll(ScrollView.FOCUS_DOWN);
            }

        }, 400);

        unique_id++;

    }

和使用的两个XML文件

这是fragment_new_time_table_monday.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<TextView
    android:id="@+id/textViewmonday"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/monday"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
    android:id="@+id/linearLayoutmonday"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" >

    <Button
        android:id="@+id/AddAnother"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/add" />

    <Button
        android:id="@+id/Done"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/done" />
</LinearLayout>

<ScrollView
    android:id="@+id/scrollViewMonday"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewmonday"
    android:layout_above="@+id/linearLayoutmonday"
    android:layout_alignParentLeft="true" >

    <LinearLayout
        android:id="@+id/MondayLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="50dp"
        android:orientation="vertical" >

    </LinearLayout>

</ScrollView>

</RelativeLayout>

和xml new_line_entry

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/NewLineEntry"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="17dp"
    android:orientation="horizontal" >

<EditText
    android:id="@+id/SubName"
    android:layout_width="189dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:ems="10"
    android:hint="@string/subjectname"
    android:paddingLeft="20dp" />

<Button
    android:id="@+id/Time"
    style="?android:attr/buttonBarButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="fill_horizontal"
    android:layout_weight="1"
    android:text="@string/time" />

<Button
    android:id="@+id/Delete"
    style="?android:attr/buttonBarButtonStyle"
    android:layout_width="42dp"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:layout_weight="1"
    android:text="@string/crossdelete" />

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

如果您希望视图不显示,可以使其不可见或消失。您也可以从视图中删除它

让它不显示

view.setVisibility(View.GONE);
view.setVIsibility(View.VISIBLE);

真的删除它

parent = view.getParent();
parent.removeView(view);

答案 1 :(得分:1)

首先,您应该使用单个XML布局和Fragment实现一天。只需为每天创建片段的单独实例,然后让ViewPager填充它。

我不会分析整个代码,但我绝对可以建议阅读Android Developers教程。谷歌在Udacity门户网站上也有很好的video tutorial

关于您的代码,作为一种快速解决方案,您可以像这样实现它:

设置布局和subjectLayout final

final View subjectLayout = inflater.inflate(R.layout.new_entry_line, null);
final LinearLayout layout = (LinearLayout) view
        .findViewById(R.id.MondayLayout);

添加点击监听器以删除按钮

deleteSub.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        layout.removeView(subjectLayout);
    }

});