GithubHelp home page GithubHelp logo

getPage error about smarttablayout HOT 8 CLOSED

ogaclejapan avatar ogaclejapan commented on September 21, 2024
getPage error

from smarttablayout.

Comments (8)

ogaclejapan avatar ogaclejapan commented on September 21, 2024

Hi @crysan

I tried a simple sample, but the error did not occur.
https://gist.github.com/ogaclejapan/57173ab483e176da078b

However, I think that we should take advantage of the Fragment-based ViewPager in cases such as you want to do.

from smarttablayout.

crysan avatar crysan commented on September 21, 2024

page.xml should be:

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/cardList" />

from smarttablayout.

ogaclejapan avatar ogaclejapan commented on September 21, 2024

Hi @crysan

The sample was modified to work with RecyclerView.
https://gist.github.com/ogaclejapan/57173ab483e176da078b

RecyclerView error occurs when not initialized at the generation of view.

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference

So, you need to'll initialized at the time of view generation by inheriting the ViewPagerItem.

        @Override
        public View initiate(LayoutInflater inflater, ViewGroup container) {
            View page = super.initiate(inflater, container);

            RecyclerView cardList = (RecyclerView) page.findViewById(R.id.cardList);
            cardList.setHasFixedSize(true);
            GridLayoutManager llm = new GridLayoutManager(page.getContext(), 3);
            cardList.setLayoutManager(llm);

            return page;
        }

http://stackoverflow.com/questions/27416834/app-crashing-when-trying-to-use-recyclerview-on-android-5-0

from smarttablayout.

crysan avatar crysan commented on September 21, 2024

Give an example for
public class MainFragment extends Fragment

from smarttablayout.

ogaclejapan avatar ogaclejapan commented on September 21, 2024

See demo πŸ‘
Demo app uses the Fragment-based PagerAdapter :)

from smarttablayout.

crysan avatar crysan commented on September 21, 2024

Your example of the simplest case, and unfortunately, my case is not applicable.
See, I have a basic Activity, which is replaced by a fragment of View. Further, in this passage is set SmartTabLayout, in which each tab I also call fragment containing RecyclerView. Each time you select a tab element SmartTabLayout I have to fill RecyclerView data from the database.
Here's an my example:

public class AuthorFragment extends Fragment {

MyFunctions func;
Realm realm;
DBfunctions db;

Authors author;

ViewPagerItemAdapter adaptertab;
String author_url;

@InjectView(R.id.cardList) RecyclerView cardList;
@InjectView(R.id.viewpager) ViewPager viewPager;
@InjectView(R.id.viewpagertab) SmartTabLayout viewPagerTab;

public AuthorFragment() { }

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    func = new MyFunctions(getActivity());
    realm = Realm.getInstance(getActivity());
    db = new DBfunctions(realm);

    author_url = getArguments().getString("author_url", "");
}


@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_author, container, false);
    page = inflater.inflate(R.layout.books_list, container, false);

    author = realm.where(Authors.class).equalTo("url_base", author_url).findFirst();
    final ArrayList<String> book_type = new <String>ArrayList();
    for (Books book : author.getBooks()) {
        book_type.add(book.getBook_type());
    }
    HashSet hs = new HashSet();
    hs.addAll(book_type);
    book_type.clear();
    book_type.addAll(hs);
    Collections.sort(book_type);

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

    ViewPagerItems.Creator items = ViewPagerItems.with(getActivity());
    for (String type : book_type) {
        items.add(ViewPagerItem.of(type, R.layout.books_list));
    }
    adaptertab = new ViewPagerItemAdapter(items.create());

    viewPager.setAdapter(adaptertab);
    viewPagerTab.setViewPager(viewPager);
    viewPagerTab.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
        @Override
        public void onPageScrollStateChanged(int state) { }

        @Override
        public void onPageSelected(int position) {
            View page = adapter.getPage(position);
            RecyclerView cardList = (RecyclerView) page.findViewById(R.id.cardList);   ---- ERROR ----
            cardList.setHasFixedSize(true);
            GridLayoutManager llm = new GridLayoutManager(page.getContext(), 3);
            cardList.setLayoutManager(llm);
        }
    });

    return v;
}

fragment_author.xml

<com.ogaclejapan.smarttablayout.SmartTabLayout
    android:id="@+id/viewpagertab"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@color/toast_default"
    app:stl_indicatorAlwaysInCenter="false"
    app:stl_indicatorInFront="false"
    app:stl_indicatorInterpolation="smart"
    app:stl_indicatorColor="#40C4FF"
    app:stl_indicatorThickness="4dp"
    app:stl_indicatorCornerRadius="2dp"
    app:stl_underlineColor="#4D000000"
    app:stl_underlineThickness="1dp"
    app:stl_dividerColor="#00000000"
    app:stl_dividerThickness="100dp"
    app:stl_defaultTabTextColor="#FC000000"
    app:stl_defaultTabTextSize="12sp"
    app:stl_defaultTabTextHorizontalPadding="16dp"
    app:stl_defaultTabTextMinWidth="0dp"
    app:stl_distributeEvenly="false" />

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

books_list.xml

<android.support.v7.widget.RecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/cardList"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:layout_marginTop="16dp"/>

from smarttablayout.

ogaclejapan avatar ogaclejapan commented on September 21, 2024

Hi @crysan

Why not use the ViewPager of Fragment?
You should use the Fragment-based Adapter.

//Activity:

    FragmentPagerItemAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpager_with_tab);

        List<String> book_type = new ArrayList<>();

        //load books

        FragmentPagerItems items = new FragmentPagerItems(this);
        for (String type : book_type) {
            Bundle args = new Bundler().putString("book_type", type).get();
            items.add(FragmentPagerItem.of(type, BookListFragment.class, args));
        }

        mAdapter = new FragmentPagerItemAdapter(getSupportFragmentManager(), items);

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

        viewPager.setAdapter(mAdapter);
        viewPagerTab.setViewPager(viewPager);
        viewPagerTab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                Fragment f = mAdapter.getPage(position);
                if (f instanceof BookListFragment) {
                    ((BookListFragment)f).update();
                }
            }
        });

}


//BookListFragment

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.books_list, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        RecyclerView cardList = (RecyclerView) view.findViewById(R.id.cardList);
        cardList.setHasFixedSize(true);
        GridLayoutManager llm = new GridLayoutManager(page.getContext(), 3);
        cardList.setLayoutManager(llm);

        //...

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        update();
    }

    public void update() {
        //load data of booktype
    }

This is not a library side issue. This is the implementation side issues.
I wish you the best. πŸ‘

from smarttablayout.

crysan avatar crysan commented on September 21, 2024

Thank you! Everything turned out!

from smarttablayout.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.