Отображение фрагментов в Android динамически - PullRequest
0 голосов
/ 08 ноября 2019

Я пытаюсь создать простой фрагмент, и он ничего не отображает. Что случилось? Этот фрагмент должен отображаться динамически.

Этот фрагмент будет отображать одно изображение из папки для рисования.

Класс PanelFragment представляет этот фрагмент. ChooseColor выполняет транзакцию фрагмента.

ChooseColor_activtity.xml представляет макет с одним контейнером для 1 фрагмента.

ChooseColor выполняет транзакцию фрагмента.

public class ChooseColor extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);
    setContentView(R.layout.choosecolor_activity);

    PanelFragment firstFragment = new PanelFragment();
    firstFragment.setmImagesIds(AndroidImageAssets.getPictures());
    firstFragment.setmListIndex(0);

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .add(R.id.first_container,firstFragment)
            .commit();

Класс PanelFragment представляетэтот фрагмент.

public class PanelFragment extends Fragment {

public static final String TAG = "PanelFragment";

public PanelFragment() {
}

private List<Integer> mImagesIds;
private int mListIndex;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.panel, container, false);
    final ImageView imageView = (ImageView) rootView.findViewById(R.id.image);

    if (mImagesIds != null) {
        // Set the image resource to the list item at the stored index
        imageView.setImageResource(mImagesIds.get(mListIndex));
    }

    return rootView;

}

public List<Integer> getmImagesIds() {
    return mImagesIds;
}

public void setmImagesIds(List<Integer> mImagesIds) {
    this.mImagesIds = mImagesIds;
}

public void setmListIndex(int mListIndex) {
    this.mListIndex = mListIndex;
}}

ChooseColor_Activity.xml

 <?xml version="1.0" encoding="utf-8"?>
 ScrollView
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:orientation="vertical"
 android:layout_height="match_parent"
 tools:context="com.example.modernartui.ChooseColor">


<LinearLayout
    android:id="@+id/android_me_linear_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="vertical"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp">

    <FrameLayout
        android:id="@+id/first_container"
        android:layout_width="match_parent"
        android:layout_height="100dp"/>



</LinearLayout>

А также несколько файлов XML для представления изображений

файл image.xml

 <?xml version="1.0" encoding="utf-8"?>
 <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/image">

</ImageView>


 Class AndroidImageAssets responds for getting access for images.

public class AndroidImageAssets {


private static final List<Integer> pictures = new ArrayList<Integer>() {{

    add(R.drawable.body1);
    add(R.drawable.picture1);
    add(R.drawable.picture2);
    add(R.drawable.picture3);
    add(R.drawable.picture4);
    add(R.drawable.picture5);
    add(R.drawable.picture6);
    add(R.drawable.picture7);
    add(R.drawable.picture8);
    add(R.drawable.picture9);
    add(R.drawable.picture10);
    add(R.drawable.picture11);
    add(R.drawable.picture12);
    add(R.drawable.picture13);
    add(R.drawable.picture14);
    add(R.drawable.picture15);
    add(R.drawable.picture16);
    add(R.drawable.picture17);
    add(R.drawable.picture18);
    add(R.drawable.picture19);

}};

public static List<Integer> getPictures() {
    return pictures;
}

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...