0

Стоит задача написать почтовый клиент для android. Как реализовать возможность отображение прикрепленный файлов для отправки. Я сделал так, что я выбираю файл и могу его отправить, но если я еще раз выберу файл, то fragment, который я загружаю перезатерает предыдущий fragment, который был загружен. Как сделать это динамически, чтобы когда я выбираю новый файл у меня добавляется значок архива ниже предыдущего архива, который я добавил, при этом не накладываясь друг на друга
Код для прикрепленного файла

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Log.d(TAG, "Name " + filePath);
        ArchiveFragment fragment = new ArchiveFragment(filePath.substring(filePath.lastIndexOf("/") + 1));
        fragmentTransaction.add(R.id.fragment_container, fragment);
        fragmentTransaction.commitAllowingStateLoss();

ArchiveFragment

private class ArchiveFragment extends Fragment {
    public static final String TAG = "TAG";
    public String name;
    public TextView nameTextView;

    public ArchiveFragment(String name) {
        this.name = name;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v =inflater.inflate(R.layout.attach_archive, container,false);
        Log.d(TAG, name);
        nameTextView = (TextView) v.findViewById(R.id.nameTextView);
        nameTextView.setText(name);

        return v;
    }


}

Layout, в который я загружаю изображение архива и его подпись.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MailManager">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/coordinate"
            android:layout_height="wrap_content"
            android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
                android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="16dp">
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/cvTo">
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:textColorHint="@color/colorPrimary">
                <EditText
                    android:id="@+id/toEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text"
                    android:hint="Получатель письма"/>
            </android.support.design.widget.TextInputLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/cvText">
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:textColorHint="@color/colorPrimary">
                <EditText
                    android:id="@+id/subjectEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="text"
                    android:hint="Заголовок письма"/>
            </android.support.design.widget.TextInputLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/cvSubject">
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp"
                android:textColorHint="@color/colorPrimary">
                <EditText
                    android:id="@+id/textEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textMultiLine"
                    android:hint="Тело письма"/>
            </android.support.design.widget.TextInputLayout>
        </android.support.v7.widget.CardView>
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="?android:attr/detailsElementBackground"
            android:layout_marginTop="15dp"/>
        </LinearLayout>
    </LinearLayout>
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>

<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@color/colorPrimary"
android:dividerHeight="1dp"
android:background="@android:color/white"/>

1 ответ 1

1

Вы заменяете один фрагмент другим. Каждый ваш фрагмент содержит только одну строку. Засим вам надо просто обновлять текущий фрагмент добавляя в его разметку элементы или заменять его новым фрагментом, содержащим старые и новые элементы.

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.