I'm populating a list of items using RecyclerView inside a Fragment. I'm able to get the list however the layout being displayed is not correctly being showned. The list contains only 5 items and it's only displaying 4 items correctly.
public class Old extends Fragment {
public Old() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(old_layout, container, false);
RecyclerView mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setHasFixedSize(true);
DatabaseData data = new DatabaseData(getActivity());
List<OldModel> oldModelList = data.getOldData();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getActivity());
mRecyclerView.setLayoutManager(linearLayoutManager);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
OldAdapter oldAdapter = new OldAdapter(getActivity(), oldModelList);
mRecyclerView.setAdapter(oldAdapter );
return rootView;
}
}
Layout xml's old_layout.xml
<android.support.design.widget.CoordinatorLayout
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="match_parent"
android:fitsSystemWindows="true"
tools:context=".Old">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/recylerview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="#+id/appBar"/>
//also tried android:layout_below="#+id/toolbar"
</android.support.design.widget.CoordinatorLayout>
recyclerview.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/old_layout"
tools:context=".Old">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
This is the output that I'm getting.
Just move
app:layout_behavior="#string/appbar_scrolling_view_behavior" from reyclerview.xml to inside the include tag.
<include layout="#layout/recylerview"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="#+id/appBar"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
Related
I have been using the NavigationDrawer activity. I have been calling the fragment from the activity. It gets called successfully but the actionbar is not showing up.
Code: Gallery_Frag - an activity created to extend teh fragment that i want to show up.
public class Gallery_Frag extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_bar_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new GalleryFragment()).commit(); }
}
CODE: GalleryFragment
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
/* final TextView textView = root.findViewById(R.id.text_gallery);
galleryViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});*/
return root;
}}
CODE: manifest
<activity android:name=".Gallery_Frag"
android:label="My Bookings"
android:theme="#style/AppTheme.NoActionBar"/>
CODE:app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_main" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
CODE: content_main
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</RelativeLayout>
You may have a container inside this content_main.xml;
if yes, pass that id to the add(R.id.container_id, new GalleryFragment())...
Otherwise change <include layout="#layout/content_main" /> to
<include layout="#layout/content_main"
android:id="#+id/fragment_container"/>
and use
add(R.id.fragment_container, new GalleryFragment())...
First of all, you have to set layout to Activity using setContentView which contain Toolbar
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Set it first
setContentView(R.layout.your_activity);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (savedInstanceState == null){
getSupportFragmentManager().beginTransaction()
.add(R.id.content, new GalleryFragment()).commit(); }
}
Got it
In your content_main.xml file, add an id to the parent relative layout like below,
<RelativeLayout
android:id="#+id/fragment_container"
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="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main">
And replace,
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, new GalleryFragment()).commit();
with
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new GalleryFragment()).commit();
This will hopefully work.
I am building android shopping app that have the cart menu item and all products are opening in fragment and i want to set the MainActivity menu items on the product fragment and other fragments.
So the user is easy to open the cart from the product view fragment.below code
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.product_view_fragment, null);
setHasOptionsMenu(true);
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
getActivity().invalidateOptionsMenu();
toolbar = (Toolbar)view.findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getActivity().onBackPressed();
}
});
this.context = getContext();
String id=getArguments().getString("message");
//String size=getArguments().getString("size");
viewPager = (ViewPager)view.findViewById(R.id.viewPager);
SessionManager sessionManager = new SessionManager(getContext());
email = sessionManager.getUserDetails().get("id");
sliderDotspanel = (LinearLayout)view.findViewById(R.id.SliderDots);
GetOneProduct(id);
return view;
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
xmlns:segmentedgroup="http://schemas.android.com/tools"
android:orientation="vertical"
android:clickable="true"
android:background="#color/colorWhite"
android:layout_height="wrap_content">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#android:style/Animation.Activity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:navigationIcon="?attr/homeAsUpIndicator"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:titleTextColor="#color/Black" />
</android.support.design.widget.AppBarLayout>
Blockquote
I assume you use a single activity approach.
Then your activity should include Toolbar in its layout something like this.
<android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".navigation.HomeActivity">
<androidx.appcompat.widget.Toolbar></androidx.appcompat.widget.Toolbar>
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/app_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView>
// if you have bottom nav.
</com.google.android.material.bottomnavigation.BottomNavigationView>
Then set up the toolbar as you wish in your activity.
I am using LinearLayoutManager with RecyclerView which is inside NestedScrollView. Everything is working fine but addOnChildAttachStateChangeListener not wroking
My xml file of fragmnet is
=====================================================:
<LinearLayout 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/parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#dedcdc"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/layout_rel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/app_bg"
android:paddingBottom="#dimen/margin_16dp"
android:paddingLeft="#dimen/margin_10dp"
android:paddingRight="#dimen/margin_10dp"
android:paddingTop="#dimen/margin_16dp">
<ImageView
android:id="#+id/image_user"
android:layout_width="#dimen/post_profile_dp_size"
android:layout_height="#dimen/post_profile_dp_size"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"
tools:src="#drawable/default_user" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_margin="#dimen/margin_5dp"
android:layout_toLeftOf="#+id/button_add_post"
android:layout_toRightOf="#+id/image_user"
android:gravity="center_vertical"
android:text="Say something" />
</RelativeLayout>
<ProgressBar
android:id="#+id/progress_bar_horizontal"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="10dip"
android:scaleY="2"
android:layout_below="#+id/layout_rel"
android:layout_centerVertical="true"
android:indeterminate="true" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_below="#+id/progress_bar_horizontal"/>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
and code in myfragmnet is
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mNestedScrollView = (NestedScrollView) view.findViewById(R.id.nested_scrollview);
mNestedScrollView.setNestedScrollingEnabled(false);
mHorizontalProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar_horizontal);
mHorizontalProgressBar.setVisibility(View.GONE);
mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
mSwipeRefreshLayout.setRefreshing(false);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
mRecyclerView.setNestedScrollingEnabled(false);
mLinearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
mAdapter = new DataListAdapter(this, getContext(), arrayList);
mRecyclerView.setAdapter(mAdapter);
endlessNestedScrollListener = new EndlessNestedScrollListener(mLinearLayoutManager) {
#Override
public void onLoadMore(int page, int totalItemsCount) {
LoadMoreData();
}
};
mNestedScrollView.setOnScrollChangeListener(endlessNestedScrollListener);
mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
#Override
public void onChildViewAttachedToWindow(View view) {
}
#Override
public void onChildViewDetachedFromWindow(View view) {
Log.d("tag","on detach");
}
});
}
Method onChildViewDetachedFromWindow in above code is not called because of nrestedscrollview.Is there any solution for it? Without nestedscrollview its woking perfectly, but i need to use nestedscrollview in my code.
onChildViewDetachedFromWindow is not called simply because when you use RecyclerView with WRAP_CONTENT inside NestedScrollView it attaches all elements at once and keeps them all the time. In other words elements will never be detached from RecyclerView inside NestedScrollView
So i was using SwipeRefreshLayout and Fragments for a simple app that i am working on and i have three files:
1.app_bar_main.xml which contains a FrameLayout which is a container for the fragments heres the code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.surafel.tryingfragment.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="58dp"
>
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
2.fragment_main.xml which contains components of the main fragment and a SwipeRefreshLayout heres the code:
<android.support.v4.widget.SwipeRefreshLayout 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:id="#+id/refreshMainContent"
tools:context="com.example.surafel.tryingfragment.MainFragment">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAIN"
android:padding="8dp"
android:textColor="#fff"
android:background="#color/colorPrimary"
android:textSize="28sp"
android:id="#+id/buttonmain"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
3.MainFragment.java this is the java code for the fragment_main.xml heres the code
//i have imported all the necessary classes
public class MainFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout swipeView;
public MainFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_main,container,false);
swipeView = (SwipeRefreshLayout) layout.findViewById(R.id.refreshMainContent);
swipeView.setColorSchemeColors(getResources().getColor(android.R.color.holo_blue_dark),getResources().getColor(android.R.color.holo_red_dark),getResources().getColor(android.R.color.holo_green_light),getResources().getColor(android.R.color.holo_orange_dark));
swipeView.setOnRefreshListener(this);
return inflater.inflate(R.layout.fragment_main, container, false);
}
public void onRefresh() {
Log.d("Refreshing","The refresh is working");
}
}
4.i also have MainActivity.java file which contains all the necessary codes
the problem is its not changing the color of the swipeView and its not even calling the onRefresh() method. when i run the
app and swipe down the refresher comes but its color is black and its not executing the onRefresh() method.
and also there are no compiletime and runtime errors.
So can anyone please help me,i posted all the code because i thought it could be usefull,Thanks.
It seems like you have called the code inflater.inflate(R.layout.fragment_main, container, false); twice.
Try returning the object layout instead of inflater.inflate(R.layout.fragment_main, container, false)
I am having troubles integrating a RecyclerView (using cardviews for the rows) inside a fragment. For some reason I cannot scroll the card view? Please, any suggestions?
This is my app design:
This is my fragment code:
public class PageFragment2 extends Fragment {
public static final String ARG_PAGE2 = "ARG_PAGE";
private int mPage2;
public static PageFragment2 newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE2, page);
PageFragment2 fragment = new PageFragment2();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage2 = getArguments().getInt(ARG_PAGE2);
Log.d("MainActivity", "onCreate" + mPage2);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_page2,container,false);
RecyclerView recList = (RecyclerView) view;
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
recList.setLayoutManager(llm);
ArrayList<String> names = new ArrayList<>();
names.add("Georgi Koemdzhiev");
names.add("Mariya Menova");
names.add("Simeon Simeonov");
names.add("Ivan Dqkov");
names.add("Dymityr Vasilev");
names.add("Petar Dimov");
CardAdapter adapter = new CardAdapter(names);
recList.setAdapter(adapter);
Log.d("MainActivity","onCreateView" + mPage2);
return view;
}
}
This is my fragment XML code:
<android.support.v7.widget.RecyclerView
android:id="#+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
xmlns:android="http://schemas.android.com/apk/res/android"/>
My activity_main layout:
<android.support.design.widget.CoordinatorLayout
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="match_parent"
android:fitsSystemWindows="true"
tools:context="koemdzhiev.com.newtablayouttest.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"/>
The Activity from the "Tabbed Activity" template, which contains the ViewPager which is containing your Fragment, uses an AppBarLayout.
This AppBarLayout is stealing the vertical movement, since it doesn't know there's a scrollable nested view (the RecyclerView) inside the ViewPager. Remove the AppBarLayout from the XML file of your Activity (you can take the TabLayout and ToolBar to the upper level).