ViewPager и его фрагменты - PullRequest
0 голосов
/ 06 марта 2019

Как и многие другие люди в стеке, например: https://ru.stackoverflow.com/questions/571704/%D0%A0%D0%B0%D0%B7%D0%BD%D1%8B%D0%B9-%D0%BA%D0%BE%D0%BD%D1%82%D0%B5%D0%BD%D1%82-%D0%BD%D0%B0-%D1%81%D1%82%D1%80%D0%B0%D0%BD%D0%B8%D1%86%D0%B0%D1%85-viewpager

Я пытаюсь создать ViewPager с разными макетами, но с: https://github.com/ogaclejapan/SmartTabLayout

Ожидание: красивый ViewPager с 3 вкладками, которые могут переключать различные макеты / фрагменты, нажимая кнопки или проведением пальцем.

Реальность: красивый ViewPager с 3 вкладками, которыеничего не делая.

Ищу вашей помощи, спасибо.

Что я сделал в классе MainActivity:

public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setElevation(0); //toolbar shadow

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        ViewGroup tab = findViewById(R.id.linear_test);
        tab.addView(LayoutInflater.from(this).inflate(R.layout.activity_main, tab, false));

        ViewPager viewPager = findViewById(R.id.viewpager);
        SmartTabLayout viewPagerTab = findViewById(R.id.viewpagertab);
        setup(viewPagerTab);

        FragmentPagerItems pages = new FragmentPagerItems(this);

            pages.add(FragmentPagerItem.of("Вкладка 1", DemoFragment.class));
            pages.add(FragmentPagerItem.of("Вкладка 2", DemoFragment.class));
            pages.add(FragmentPagerItem.of("Вкладка 3", DemoFragment.class));

        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(), pages);

        viewPager.setAdapter(adapter);
        viewPagerTab.setViewPager(viewPager);
}
public void setup(final SmartTabLayout layout) {
    //Do nothing.
}
}

Что ясделали в моем DemoFragment:

public class DemoFragment extends Fragment {

  static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";

    private int page;

    static DemoFragment newInstance(int page) {
        DemoFragment pageFragment = new DemoFragment();
        Bundle arguments = new Bundle();
        arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
        pageFragment.setArguments(arguments);
        return pageFragment;
    }

  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState) {
//      View view = inflater.inflate(R.layout.activity_main, container, false);
    page = getArguments().getInt("someInt", 0);
    return inflater.inflate(R.layout.activity_main, container, false);
//      return view;
  }

  @Override
  public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
       int position = FragmentPagerItem.getPosition(getArguments());
       getItem(position);
  }
    public Fragment getItem(int position) {
        switch (position) {
            case 0: // Fragment # 0 - This will show FirstFragment
                return DemoFragment.newInstance(1);
            case 1: // Fragment # 0 - This will show FirstFragment different title
                return DemoFragment.newInstance(2);
            case 2: // Fragment # 1 - This will show SecondFragment
                return DemoFragment.newInstance(3);
            default:
                return null;
        }
    }

}

Вот Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">



    <LinearLayout
        android:id="@+id/linear_test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints"
        android:background="@color/colorPrimary"
        android:orientation="horizontal" >



        <com.ogaclejapan.smarttablayout.SmartTabLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/viewpagertab"
            android:layout_width="match_parent"
            android:layout_height="@dimen/tab_height_large"
            app:stl_customTabTextLayoutId="@layout/test_layout"
            app:stl_customTabTextViewId="@id/custom_tab_text"
            app:stl_distributeEvenly="true"
            app:stl_defaultTabTextAllCaps="false"
            app:stl_indicatorInterpolation="linear"
            app:stl_defaultTabTextColor="@color/white"
            app:stl_indicatorColor="@color/colorBlue"
            app:stl_underlineColor="@color/colorBlue"
            app:stl_defaultTabBackground="@color/colorPrimary"
            app:stl_indicatorThickness="3dp"
            app:stl_underlineThickness="1dp"
            />

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/viewpagertab"
            />


    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/playlist_recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linear_test" />

    <TextView
        android:id="@+id/dl_params"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:visibility="gone"
        android:layout_weight="1"
        tools:ignore="MissingConstraints"/>



</android.support.constraint.ConstraintLayout>

И, наконец, test_layout, который заполняет теSmartTabLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tab"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/selectableItemBackground"
    >

    <android.support.v4.widget.Space
        android:id="@+id/center_anchor"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent="true"
        />

    <aectann.pr1.TintableImageView

        android:id="@+id/custom_tab_icon"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_above="@id/center_anchor"
        android:layout_centerHorizontal="true"
        android:scaleType="center"
        android:src="@drawable/custom_icon"
        />

    <TextView
        android:id="@+id/custom_tab_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/center_anchor"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="2dp"
        android:maxLines="1"
        android:textSize="14sp"
        android:textColor="@color/white"
        />


</RelativeLayout>

Есть идеи, ребята?

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