ScrollView not scrolling smooth with inside 3 recyclerviews - android

Here is my code.i want to put 3 recyclerViews in my layout but it is very slow on device while running. If there is 2 recyclerview it works smooth but if 3 slow very :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:id="#+id/scroll_view"
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="wrap_content"
tools:context=".fragments.MainFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Техника для офиса"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_popular_goods"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Компьютерная техника"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_competitive_goods"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Канцелярские товары"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:visibility="gone"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_stationery_goods"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
Please if somebody had such kind of problem please help me to fix my issue.
I tried Nested scrollview and setNestedScrollingEnabled = false.but nothing helps
Here is my Activity
#SuppressLint("ValidFragment")
public class MainFragment extends BasicFragment implements
MainFragmentView, PopularGoodsAdapter.ProductListener {
#BindView(R.id.scroll_view)
NestedScrollView mScrollView;
#BindView(R.id.recyclerView_stationery_goods)
RecyclerView mRecyclerViewStationeryGoods;
#BindView(R.id.recyclerView_popular_goods)
RecyclerView mRecyclerViewPopularGoods;
#BindView(R.id.recyclerView_competitive_goods)
RecyclerView mRecyclerViewCompetitiveGoods;
#BindView(R.id.imageSlider)
SliderView mSliderView;
#Inject
Navigator mNavigator;
#Inject
#Named(DISPLAY_WIDTH)
int mDisplayWidth;
#InjectPresenter
MainFragmentPresenter mPresenter;
List<Product> list1;
List<Product> list2;
List<Product> list3;
private PopularGoodsAdapter mAdapterPopularGoods;
private PopularGoodsAdapter mAdapterPopularGoods2;
private PopularGoodsAdapter mAdapterPopularGoods3;
private LayoutAnimationController layoutAnimationController;
private List<String> images;
#SuppressLint("ValidFragment")
public MainFragment() {
}
private void initView(View view) {
ButterKnife.bind(this,view);
images = new LinkedList<>();
images.add("https://images.pexels.com/photos/747964/pexels-photo-747964.jpeg?auto=compress&cs=tinysrgb&h=750&w=1260");
images.add("https://images.pexels.com/photos/929778/pexels-photo-929778.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
images.add("https://images.pexels.com/photos/218983/pexels-photo-218983.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
mSliderView.setSliderAdapter(new SliderAdapterExample(getContext(), images, "mainPage"));
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
LinearLayoutManager layoutManager2 = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
LinearLayoutManager layoutManager3 = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
mRecyclerViewPopularGoods.setLayoutManager(layoutManager);
layoutAnimationController = AnimationUtils.loadLayoutAnimation(getContext(),R.anim.layout_item_from_left);
list1 = new LinkedList<>();
mRecyclerViewPopularGoods.setLayoutAnimation(layoutAnimationController);
mRecyclerViewCompetitiveGoods.setLayoutManager(layoutManager2);
list2 = new LinkedList<>();
mRecyclerViewCompetitiveGoods.setLayoutAnimation(layoutAnimationController);
mRecyclerViewStationeryGoods.setLayoutManager(layoutManager3);
list3 = new LinkedList<>();
mRecyclerViewStationeryGoods.setLayoutAnimation(layoutAnimationController);
mRecyclerViewPopularGoods.setAdapter(mAdapterPopularGoods = new PopularGoodsAdapter(getContext(),list1,mDisplayWidth,"popular",this));
mRecyclerViewCompetitiveGoods.setAdapter(mAdapterPopularGoods2 = new PopularGoodsAdapter(getContext(),list2,mDisplayWidth,"competitive",this));
mRecyclerViewStationeryGoods.setAdapter(mAdapterPopularGoods3 = new PopularGoodsAdapter(getContext(),list3,mDisplayWidth,"stationery",this));
mRecyclerViewPopularGoods.setNestedScrollingEnabled(false);
mRecyclerViewCompetitiveGoods.setNestedScrollingEnabled(false);
mRecyclerViewStationeryGoods.setNestedScrollingEnabled(false);
getLoadingDialog().showDialog(getFragmentManager());
mPresenter.GET_TOP_HOME_VIEW();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container,
false);
initView(view);
return view;
}
#OnClick({R.id.item_sellers,
R.id.item_price_request,R.id.item_price_competitions})
void Onclick(View view) {
switch (view.getId()) {
case R.id.item_sellers:
mNavigator.toSellersActivty(getContext());
break;
case R.id.item_price_request:
mNavigator.toPriceRequestActivty(getContext());
break;
case R.id.item_price_competitions:
mNavigator.toCompetitionsActivty(getContext());
break;
}
}
#Override
public void initTopHomeView(List<HomeViewObject> result) {
if (null != result ) {
if (result.size() >= 1 && null != result.get(0)) {
list1.addAll(result.get(0).getTopProducts());
mAdapterPopularGoods.notifyDataSetChanged();
mRecyclerViewPopularGoods.setLayoutAnimation(layoutAnimationController);
}
if (result.size() >= 2 && null != result.get(1)) {
list2.addAll(result.get(1).getTopProducts());
mAdapterPopularGoods2.notifyDataSetChanged();
mRecyclerViewPopularGoods.setLayoutAnimation(layoutAnimationController);
}
if (result.size() >= 3 && null != result.get(2)) {
list3.addAll(result.get(2).getTopProducts());
mAdapterPopularGoods3.notifyDataSetChanged();
mRecyclerViewStationeryGoods.setLayoutAnimation(layoutAnimationController);
}
}
}
#Override
public void stopProgress() {
getLoadingDialog().hideDialog();
}
#Override
public void onProductClicked(Product product) {
mNavigator.toProductActivity(getContext(),product);
}
}
Is there any mistake?

Try the following -
Try to put all the views inside NestedScrollView i.e, root view.
Set recyclerView.setNestedScrollingEnabled(false); for both the recyclerViews in your xml.

Change your recycler view's android:layout_height="match_parent" to android:layout_height="wrap_content" and then use Nested scrollView with recyclerView.setNestedScrollingEnabled(false);

try to change your ScrollView with NestedScrollViewand Change your recycler view's android:layout_height="match_parent" to android:layout_height="wrap_content" :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
android:id="#+id/scroll_view"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragments.MainFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Техника для офиса"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_popular_goods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Компьютерная техника"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:nestedScrollingEnabled="false"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_competitive_goods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:textColor="#color/blacker"
android:textStyle="bold"
android:textSize="18sp"
android:text="Канцелярские товары"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp" />
<android.support.v7.widget.RecyclerView
android:visibility="gone"
android:fillViewport="true"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:id="#+id/recyclerView_stationery_goods"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
and :
RecyclerView recycleView1 = (RecyclerView) findViewById(R.id.recyclerView_popular_goods);
RecyclerView recycleView2 = (RecyclerView) findViewById(R.id.recyclerView_competitive_goods);
RecyclerView recycleView3 = (RecyclerView) findViewById(R.id.recyclerView_stationery_goods);
recycleView1.setNestedScrollingEnabled(false);
recycleView2.setNestedScrollingEnabled(false);
recycleView3.setNestedScrollingEnabled(false);
If your devices API is below v 21, use the following:
ViewCompat.setNestedScrollingEnabled(recycleView1, false);
ViewCompat.setNestedScrollingEnabled(recycleView2, false);
ViewCompat.setNestedScrollingEnabled(recycleView3, false);

Your design needs to change. As soon as you're adding RecyclerView with nested scrolling disabled, you should keep in mind that view will have its items created all at once.
It basically means no typical RecyclerView optimization magic when only needed amount of items is created and rendered, then reused, based on screen real estate etc.
In your case the situation is even less good as you have 3 such RecyclerViews. The performance of your solution will of course depend on hardware and number of items inside each RecyclerView, but pls do consider using just ONE RecyclerView and no ScrollView at all.

Finally I achieved.I used NestedScrollView as root.And used only 1 recyclerview.And in adapter of this recyclerview I created other recyclerViews.Scrolling smooth Thanks #Venky

Related

button text doesn't appear when rotate emulator

I have an activity that contain a fragment which contains a recycler view and i use recycler view adapter to fill the recycler view with data and i use layout which contains 2 text view and 3 buttons when rotate emulator the language changed so I save the state of the fragment in activity, the language doesn't changed but the text on buttons doesn't appear I try to save state of text in fragment but i get error null pointer exception for button object
the fragment code
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.daily_azkaar_fragment,container, false);
rv = (RecyclerView) view.findViewById(R.id.recyclerviewcard);
SnapHelper snapHelper = new PagerSnapHelper();
snapHelper.attachToRecyclerView(rv);
activity = getActivity().getIntent().getIntExtra("activity", 3);
position = getActivity().getIntent().getIntExtra("position", 3);
rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
rv.setLayoutManager(layoutManager);
rv.setHasFixedSize(true);
rv.scrollToPosition(position);
settings= getActivity().getSharedPreferences("MyPrefs", 0);
favourite=loadArray("fav",getActivity());
favourite_evening=loadArray("fav_evening",getActivity());
for(int i=0;i<favourite.length;i++)
Log.d("msg ",String.valueOf(favourite[i]));
if (settings.getBoolean("is_first_time_presses", true)) {
//the app is being launched for first time, do something
Log.d("TAG", "First time Presses");
settings.edit().putBoolean("is_first_time_presses", false).commit();
storePresses(presses,"presses",0);
storePresses(presses_evening,"presses_evening",0);
} else {
//second time launch..
presses=loadPresses("presses");
presses_evening=loadPresses("presses_evening");
for(int i=0;i<presses.length;i++)
Log.d("presses ",String.valueOf(presses[i]));
}
if (settings.getBoolean("is_sett", true)) {
//the app is being launched for first time, do something
Log.d("TAG", "First time");
settings.edit().putBoolean("is_sett", false).commit();
storeArraySettings(settingsArray,"sett",true,getActivity());
} else {
//second time launch..
settingsArray=loadArray("sett",getActivity());
for(int i=0;i<settingsArray.length;i++)
Log.d("msg ",String.valueOf(settingsArray[i]));
}
if(activity == 3){
azkarForms=getSunsetFavourites("fav","presses", this.getContext());
adapter=new RecyclerFavouriteAdapter(azkarForms,getActivity(),activity,0);
rv.setAdapter(adapter);
adapter.setClickListener(this);
}
else{
azkarForms=getSunsetFavourites("fav_evening","presses_evening", this.getContext());
adapter=new RecyclerFavouriteAdapter(azkarForms,getActivity(),activity,0);
rv.setAdapter(adapter);
Button next=view.findViewById(R.id.next);
if(savedInstanceState != null)
next.setText(savedInstanceState.getString("next"));
adapter.setClickListener(this);
}
return view;
}
private ArrayList<AzkarForm> getSunsetFavourites(String arrayName, String pressesArray, Context mContext) {
AzkaarData azkaarData = new AzkaarData(mContext);
list = new ArrayList<>();
int size = settings.getInt(arrayName + "_size", 0);
Boolean[] array = new Boolean[size];
int[] numPresses = new int[size];
ArrayList<MyFavourites> myResults = new ArrayList<MyFavourites>();
MyFavourites myFavourites;
for (int i = 0; i < size; i++) {
array[i] = settings.getBoolean(arrayName + "_" + i, true);
numPresses[i] = settings.getInt(pressesArray + "_" + i, 0);
myFavourites=new MyFavourites(i,array[i],numPresses[i]);
myResults.add(myFavourites);
}
if (myResults != null)
list = azkaarData.getArrayListDataFav(myResults,activity, 0);
return list;
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
savedInstanceState.putString("next", next.getText().toString());
}
adapter code
} else if (activity == 3) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_details,parent,false);
holder=new MyViewHolder(view,activity,c);
return holder;
} else if (activity == 4) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_details,parent,false);
holder=new MyViewHolder(view,activity,c);
return holder;
}
layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="6">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:weightSum="6">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/icon"
android:layout_weight="2"
android:src="#drawable/favorite_border"
android:clickable="true"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="#string/add_zekr_txt"
android:textSize="26sp"/>
</LinearLayout>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/cardview"
android:layout_weight="4">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:id="#+id/relative">
<TextView
android:id="#+id/sample"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:text="#string/sample_zekr"
android:textIsSelectable="true"
android:textSize="20dp"
android:textStyle="bold" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_below="#+id/cardview"
android:weightSum="5">
<Button
android:text="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/count"
android:layout_weight="2"
android:layout_gravity="center"
android:background="#drawable/btnborder"
android:layout_margin="10dp"/>
<Button
android:text="#string/num_press"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/press"
android:layout_weight="1"
android:background="#drawable/btncircle"
android:layout_margin="10dp"/>
<Button
android:text="#string/next"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#+id/next"
android:layout_weight="2"
android:background="#drawable/btnborder"
android:layout_margin="10dp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
fragment layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerviewcard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></android.support.v7.widget.RecyclerView>
</LinearLayout>
Use override method onConfigurationChanged() that will help you with orientation change problems.

TextView setText and setVisibility not working in fragment

I have ControllerFragment which has 3 child fragments. Children fragments change are provided in the tabLayout via ViewPager. Unsend Apple and Banana badge count are computable variable values. unSendAppleCountTxtView and unSendBananaCountTxtView are changed according to these variable values.
My function is running main Thread.
Tried methods:
I have controlled null check and textView isn't null
setUnSendAppleCount function is taken in these threads (runOnUIThread, new Handler(), new Handler(Looper.getMainLooper()))
I have tried AsyncTask and not working setView and setText in postExecute too.
And finally I scan all related questions and tried.
All methods didn't work. TextView is empty value.
public class ControllerFragment extends Fragment {
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.controller_fragment, viewGroup, false);
tabs = ViewUtil.findById(rootView, R.id.tabs);
viewPager = ViewUtil.findById(rootView, R.id.controllerViewPager);
setFragments();
setupViewPager(viewPager);
tabs.setupWithViewPager(viewPager);
setupTabIcons(viewGroup);
return rootView;
}
private void setupTabIcons(ViewGroup viewGroup) {
LinearLayout appleListTab = (LinearLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text_and_badge, viewGroup, false);
RelativeLayout pearListTab = (RelativeLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text, viewGroup, false);
LinearLayout bananaListTab = (LinearLayout) LayoutInflater.from(MyApplication.getContext()).inflate(R.layout.tab_title_text_and_badge, viewGroup, false);
unSendAppleCountTxtView = appleListTab.findViewById(R.id.badgeView);
unSendBananaCountTxtView = bananaListTab.findViewById(R.id.badgeView);
TextView appleTextView = appleListTab.findViewById(R.id.textView);
TextView pearTextView = pearListTab.findViewById(R.id.textView);
TextView bananaTextView = bananaListTab.findViewById(R.id.textView);
setUnSendAppleCount(2); //....this function is not working
setUnSendBananaCount(2); //.....this function is not working
bananaTextView.setText(getString(R.string.bananas));
tabs.getTabAt(0).setCustomView(bananaListTab);
appleTextView.setText(getString(R.string.apples));
tabs.getTabAt(1).setCustomView(appleListTab);
pearTextView.setText(getString(R.string.pears));
tabs.getTabAt(2).setCustomView(pearListTab);
}
private void setupViewPager(ViewPager viewPager) {
TabsViewPagerAdapter adapter = new TabsViewPagerAdapter(getFragmentManager());
adapter.addFrag(bananaFragment, getString(R.string.bananas));
adapter.addFrag(appleFragment, getString(R.string.apples));
adapter.addFrag(pearFragment, getString(R.string.pears));
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(1);
}
private void setUnSendAppleCount(int unSendCount) {
if (unSendAppleCountTxtView != null) {
if (unSendCount > 0) {
unSendAppleCountTxtView.setVisibility(View.VISIBLE);
unSendAppleCountTxtView.setText(String.format(getMyLocale(), "%d", unSendCount));
} else {
unSendAppleCountTxtView.setVisibility(View.GONE);
}
}
}
}
controller_fragment xml:
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/tab_unpressed_color">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/white"
app:tabTextColor="#color/tabColor" />
</FrameLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/controllerViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/app_bar_layout" />
</RelativeLayout>
tab_title_text_and_badge.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:weightSum="2"
android:orientation="horizontal"
tools:background="#color/Black">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:layout_gravity="center"
android:maxLines="1"
android:layout_weight="1.99"
android:textColor="#color/white"
android:textSize="#dimen/infoTitle"/>
<TextView
android:id="#+id/badgeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_weight="0.01"
android:background="#drawable/tab_controller_badge"
android:gravity="center"
android:textColor="#color/white"
android:textSize="#dimen/miniTextSize"
android:visibility="gone"
tools:text="11"
tools:visibility="visible" />
</LinearLayout>
Please help me
try to setVisibility android:visibility="visible" or android:visibility="invisible" delete gone attribute it remove TextView.
<TextView
android:id="#+id/badgeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_weight="0.01"
android:background="#drawable/tab_controller_badge"
android:gravity="center"
android:textColor="#color/white"
android:textSize="#dimen/miniTextSize"
android:visibility="visible" //change here
tools:text="11"
tools:visibility="visible" />
Please use android:text instead tools:text
tools:text="text" is used only for Android Studio layout preview (wont be visible while running the app)
android:text="text" is used to set text to a a layout element, which you can see when the app is run
Try using the below xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:weightSum="2"
android:orientation="horizontal"
android:background="#color/Black">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:layout_gravity="center"
android:maxLines="1"
android:layout_weight="1.99"
android:textColor="#color/white"
android:textSize="#dimen/infoTitle"/>
<TextView
android:id="#+id/badgeView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_weight="0.01"
android:background="#drawable/tab_controller_badge"
android:gravity="center"
android:textColor="#color/white"
android:textSize="#dimen/miniTextSize"
android:text="11"/>
</LinearLayout>
Hope it helps!
I guess different thread problem. I have tried that eventBus for activity passing data to fragment and it solved.
ControllerFragment(){
#Override
public void onStart() {
super.onStart();
if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
}
#Override
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}
#Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEventMain(ControllerEvent event) {
if (event.getSelectedEventType() == ControllerEvent.tabControllerEventTypes.refreshCount.ordinal()) {
setUnSendAppleCount(event.getAppleCount());
}
}
}

Horizontal recyclerview inside fragment not scrolling

I want to add horizontal recyclerview at the bottom of fragment. I followed the tutorial and successfully implemented horizontal recyclerview and cardview but after trying all possible answers,horizontal scrolling is not working.
Here is my code :-
XML :-
<RelativeLayout tools:context="com.AlfaCab.Menuactivtiy"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:layout_marginTop="56dp"
android:id="#+id/Mainlayout"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#color/BgColor"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="#drawable/offer_white_box">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Offers"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/BlackTextColor"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView_offer"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:scrollbars="none"/>
<!-- android:orientation="horizontal"
android:scrollbars="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
/> -->
</LinearLayout>
</RelativeLayout>
Here is my offer_cardview.xml / custom layout for recyclerview :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<!-- <!–Offer Start–>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/offer_white_box">
-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--Offer Coupon-->
<LinearLayout
android:layout_width="140dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/round_box"
>
<LinearLayout
android:id="#+id/ll_offer_bg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:background="#drawable/offer_red_box"
android:layout_marginBottom="3dp">
<TextView
android:id="#+id/tv_offer_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get 30% Cashback"
android:textSize="12sp"
android:textColor="#color/WhiteTextColor"
android:textStyle="bold"
android:layout_gravity="center_vertical"
android:gravity="center"/>
</LinearLayout>
<TextView
android:id="#+id/tv_offer_disc"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="upto Rs. 300 cashback on Outstation"
android:textSize="10sp"
android:textColor="#color/BlackTextColor"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
</LinearLayout>
<!--Offer Coupon Ends-->
</LinearLayout>
<!--</LinearLayout>-->
<!--Offer Ends-->
</android.support.v7.widget.CardView>
</LinearLayout>
HomeFragment.java :-
//a list to store all the products
List<Offer_Data> offerList;
//the recyclerview
RecyclerView recyclerView;
String appOfferId,appOfferTitle,appOfferDes,status;
In onCreate :-
//getting the recyclerview from xml
recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView_offer);
After successfully adding data to offerList :-
//creating recyclerview adapter
Offer_Adapter adapter = new Offer_Adapter(getActivity(), offerList);
//adapter.notifyDataSetChanged();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();// Notify the adapter
Offer_Adapter.java :- (Recyclervire adapter)
public class Offer_Adapter extends RecyclerView.Adapter<Offer_Adapter.ProductViewHolder> {
int[] myImageList;
//this context we will use to inflate the layout
private Context mCtx;
//we are storing all the products in a list
private List<Offer_Data> offerList;
//getting the context and product list with constructor
public Offer_Adapter(Context mCtx, List<Offer_Data> offerList) {
this.mCtx = mCtx;
this.offerList = offerList;
this.myImageList = new int[]{R.drawable.offer_red_box, R.drawable.offer_megento_box, R.drawable.offer_yellow_box};
}
#Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//inflating and returning our view holder
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.offer_cardview, null);
return new ProductViewHolder(view);
}
#Override
public void onBindViewHolder(ProductViewHolder holder, int position) {
//getting the product of the specified position
Offer_Data product = offerList.get(position);
//binding the data with the viewholder views
holder.tv_offer_title.setText(product.getOffer_title());
holder.tv_offer_disc.setText(product.getOffer_disc());
int random_box = getRandom(myImageList);
holder.ll_offer_bg.setBackgroundResource(random_box);
}
public static int getRandom(int[] array) {
int rnd = new Random().nextInt(array.length);
return array[rnd];
}
#Override
public int getItemCount() {
return offerList.size();
}
class ProductViewHolder extends RecyclerView.ViewHolder {
TextView tv_offer_title, tv_offer_disc;
LinearLayout ll_offer_bg;
public ProductViewHolder(View itemView) {
super(itemView);
tv_offer_title = (TextView) itemView.findViewById(R.id.tv_offer_title);
tv_offer_disc = (TextView) itemView.findViewById(R.id.tv_offer_disc);
ll_offer_bg = (LinearLayout)itemView.findViewById(R.id.ll_offer_bg);
}
}
}
Please, help me to make it scroll horizontally.
LinearLayoutManager linearLayoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(linearLayoutManager);
Found the Problem
Change this line
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
to
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
Problem is that , you are passing wrong context to recycler view manager
Should use getActivity() not getContext()
ALso no need to add this line in xml
app:layoutManager="android.support.v7.widget.LinearLayoutManager"

Horizontal View line not displaying in RecyclerVeiw

I am using Horizontal Recycler View :
My Layout file contains recyclerview as below :
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_Styles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white" />
My custom/singleview/raw file to be bind in RecyclerView is as below :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:maxLines="2"
android:ellipsize="end"
android:layout_centerHorizontal="true"
android:id="#+id/txt_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/dimen_10"
android:text="Style"
android:textColor="#color/black" />
<View
android:layout_centerHorizontal="true"
android:id="#+id/viewStyle"
android:layout_width="match_parent"
android:layout_height="#dimen/dimen_2"
android:layout_below="#+id/txt_style"
android:background="#color/colorAppDefault" />
<!--#455A90-->
My viewStyle is display when I am giving its width to Fix. But, Its look improper.
What might be the issue ?
I am doing as below in My Adapter.
public RecyclerViewHolders(View itemView) {
super(itemView);
txt_style = (TextView) itemView.findViewById(R.id.txt_style);
viewStyle = itemView.findViewById(R.id.viewStyle);
}
public void setData(DrinkStyleModel modelBeerStyle, final int position) {
final DrinkStyleModel modelFinal = modelBeerStyle;
if (modelFinal.is_check()) {
txt_style.setTextColor(context.getResources().getColor(R.color.black));
viewStyle.setVisibility(View.VISIBLE);
} else {
txt_style.setTextColor(context.getResources().getColor(R.color.gray));
viewStyle.setVisibility(View.GONE);
}
}

Unexpected space is coming in recyclerview items

This is silly problem but I could not find what I am doing wrong.
I am using FirebaseRecyclerAdapter to fill recyclerView but there is unexpected space is coming with items.
Fragment class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tags, container, false);
mProgressBar = (ProgressBar)view.findViewById(R.id.progressBar);
mTagsRecyclerView = (RecyclerView) view.findViewById(R.id.tagsRecyclerView);
mLinearLayoutManager = new LinearLayoutManager(getContext());
mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
mFirebaseAdapter = new FirebaseRecyclerAdapter<Tags, TagsViewHolder>(
Tags.class,
R.layout.item_message,
TagsViewHolder.class,
mFirebaseDatabaseReference.child("tags")) {
#Override
protected void populateViewHolder(TagsViewHolder viewHolder, Tags tags, int position) {
mProgressBar.setVisibility(ProgressBar.INVISIBLE);
viewHolder.tagTextView.setText(tags.getTagname());
viewHolder.usrTextView.setText(tags.getUserid());
}
};
mFirebaseAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
#Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
}
});
mTagsRecyclerView.setLayoutManager(mLinearLayoutManager);
mTagsRecyclerView.setAdapter(mFirebaseAdapter);
mTagsRecyclerView.addOnItemTouchListener(new RecyclerViewItemClickListener(getContext(),
new RecyclerViewItemClickListener.OnItemClickListener() {
#Override public void onItemClick(View v, int position) {
//TODO:
// do something on item click
Log.d("Item clicked at",Integer.toString(position));
}
}));
return view;
}
Fragment layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.helpplusapp.amit.helpplus.TagsFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/tagsRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_gravity="center"
/>
List item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#drawable/view_separator"
android:paddingTop="#dimen/margin_default"
android:paddingBottom="#dimen/margin_default"
android:showDividers="end">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/tagTextView"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/usrTextView"
/>
</LinearLayout>
Please suggest.
In listitemlayout xml file make the height of linear layout to wrap content
In list item layout change the linear layout android:height="wrap_content"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="#drawable/view_separator"
android:paddingTop="#dimen/margin_default"
android:paddingBottom="#dimen/margin_default"
android:showDividers="end">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/tagTextView"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/usrTextView"
/>
</LinearLayout>
With the support library 23.2 google introduced a change to the recyclerview:
LayoutManager API: auto-measurement! This allows a RecyclerView to
size itself based on the size of its contents. This means that
previously unavailable scenarios, such as using WRAP_CONTENT for a
dimension of the RecyclerView, are now possible. You’ll find all built
in LayoutManagers now support auto-measurement. Due to this change,
make sure to double check the layout parameters of your item views:
previously ignored layout parameters (such as MATCH_PARENT in the
scroll direction) will now be fully respected.
Source
You need to change the height of the item to wrap_content instead of match_parent.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#drawable/view_separator"
android:paddingTop="#dimen/margin_default"
android:paddingBottom="#dimen/margin_default"
android:showDividers="end">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/tagTextView"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/usrTextView"
/>
</LinearLayout>

Categories

Resources