Comments (8)
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.
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.
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;
}
from smarttablayout.
Give an example for
public class MainFragment extends Fragment
from smarttablayout.
See demo π
Demo app uses the Fragment-based PagerAdapter :)
from smarttablayout.
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.
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.
Thank you! Everything turned out!
from smarttablayout.
Related Issues (20)
- PagerAdapter for View-based Page
- java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
- Its not working with ViewPager2? HOT 7
- Selected Text Color
- custom integration
- How to change tab text color dynamically from coding end.
- Background Color is not 100 percent white !!!
- how to set indicator's width to the textview's width
- Changing stl_defaultTabTextSize programmatically
- Nested Tabs
- Only works for 2 tabs. the 3rd tab does not render content HOT 1
- Me HOT 1
- H
- how to unselect all tab?
- Viewpager2 not supported
- How Do I disable scrolling right and left.
- ζη€ΊηΊΏθ·ζεζ Ήζ¬δΈεΉι HOT 1
- Doesn't inherit font style from theme an style xml file
- How to get Indicator Thickness Trick 1 style for the tab buttons. HOT 1
- How do I set up icons instead of letters?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from smarttablayout.