I need to inflate a RecyclerView inside a Fragment AndroidStudio - android

I would like to use a Tab/Fragment to show RecyclerView, but I don't know how to do it because the method of the Fragment class return a View, not a Reclycler View:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.fragment_needs, container, false);
return myFragmentView;
}
The code of the fragment.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".NeedsFragment">
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/all_user_need_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</FrameLayout>
</RelativeLayout>
Thank u.

First off, your onCreateView() method makes no sense:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.fragment_needs, container, false);
return needsFragmentRecyclerView; //you should be returning myFragmentView here, not whatever needsFragmentRecyclerView is
}
Second, once you inflate your layout, just use findViewById():
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.fragment_needs, container, false);
RecyclerView recyclerView = myFragmentView.findViewById(R.id.all_user_need_list); //assign to a global variable if needed
return myFragmentView; //replaced with the proper variable
}
Alternatively, you can get the RecyclerView from anywhere in your Fragment, as long as it's called after onCreateView() with the following:
RecyclerView recyclerView = getView().findViewById(R.id.all_user_need_list);

You can return any type of view component and Custom view that is inherit from android.view class in onCreateView() method.

Related

List View instance error of cannot find symbol

public class GroupFragment extends Fragment {
private View groupFragmentView;
private ListView list_View;
here i have created instances as well
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
groupFragmentView = inflater.inflate(R.layout.fragment_group, container, false);
InitializeFields();
return groupFragmentView;
}
`
private void InitializeFields()
{
list_View = (ListView) groupFragmentView.findViewById(android.R.id.**list_View**);
}}
1 now here im getting error for listView
2 on the left side of equals im having no issue even for the casting there is no error
3 i have tried everything but still couldnt resolve it
4 im still learning android
error showing is-----
error: cannot find symbol
list_View = (ListView) groupFragmentView.findViewById(android.R.id.list_View);
^
symbol: variable list_View
location: class id
`
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".GroupFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" />
`
You have to pass the object of view in your method.
#Override public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
groupFragmentView=inflater.inflate(R.layout.fragment_group,container,false);
InitializeFields(groupFragmentView);
return groupFragmentView;
}
private void InitializeFields(View view)
{
list_View = (ListView) view.findViewById(android.R.id.**list_View**);}

image slider works in activity but not in fragment

the code from this video https://www.youtube.com/watch?v=1AS5HcXPpqk works fine in activity, but when i try to implement it in fragment it crashes.
java code and xml file respectively
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_facts, container, false);
ImageSlider imageSlider = (ImageSlider) getView().findViewById(R.id.slide);
List<SlideModel> slideModels = new ArrayList<>();
slideModels.add(new SlideModel(R.drawable.ic_launcher_background, "Image1"));
slideModels.add(new SlideModel(R.drawable.ic_launcher_background, "Image2"));
slideModels.add(new SlideModel(R.drawable.ic_launcher_background, "Image3"));
imageSlider.setImageList(slideModels, true);
return view;
}
<?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"
tools:context=".MainActivity"
android:background="#E4E3E3">
<com.denzcoskun.imageslider.ImageSlider
android:id="#+id/slide"
android:layout_width="match_parent"
android:layout_height="250dp"
app:auto_cycle="true"
app:delay="0"
app:period="1000"
app:corner_radius="20"
/>
</RelativeLayout>
Don't
ImageSlider imageSlider = (ImageSlider) getView().findViewById(R.id.slide);
Do
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_facts, container, false);
ImageSlider imageSlider = (ImageSlider) view.findViewById(R.id.slide);
Change this line
ImageSlider imageSlider = (ImageSlider) getView().findViewById(R.id.slide);
to
ImageSlider imageSlider = (ImageSlider) getActivity().findViewById(R.id.slide);
You need to pass correct context getActivity()

Unable to Position Bottom Sheet Dialog at the bottom of the screen

I have a Video Detail Fragment which I want to behave as a bottom sheet behaviour, but unfortunately, the dialog is getting displayed at the centre of the screen. I am unable to figure out the exact problem in the code. Below I am attaching the code and the layout file, in regard to the same.
This is my Video Detail fragment:
public class VideoDetailsFragment extends BottomSheetDialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AppCompatDialog dialog = new AppCompatDialog(getActivity());
dialog.setTitle(titleId);
return dialog;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View dialogView = inflater.inflate(R.layout.video_detail_fragment, container, false);
Button cancelButton = dialogView.findViewById(R.id.track_selection_dialog_cancel_button);
Button okButton = dialogView.findViewById(R.id.track_selection_dialog_ok_button);
return dialogView;
}
public static final class TrackSelectionViewFragment extends BottomSheetDialogFragment
implements TrackSelectionView.TrackSelectionListener {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.exo_track_selection_dialog, container, /* attachToRoot= */ false);
TrackSelectionView trackSelectionView = rootView.findViewById(R.id.exo_track_selection_view);
trackSelectionView.setShowDisableOption(true);
trackSelectionView.setAllowMultipleOverrides(allowMultipleOverrides);
trackSelectionView.setAllowAdaptiveSelections(allowAdaptiveSelections);
trackSelectionView.init(mappedTrackInfo, rendererIndex, isDisabled, overrides, /* listener= */ this);
return rootView;
}
}
}
The xml file for video_detail_fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="270dp"
android:visibility="visible"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
<FrameLayout
android:id="#+id/child_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end">
</LinearLayout>
</LinearLayout>
I went through many posts but almost all the posts were showing the solution as setting the app attribute app:layout_behavior="android.support.design.widget.BottomSheetBehavior". But it's not working in my case. Kindly help me understand as to where I am doing wrong?

search box with fragment does not respond

Hello I have a problem when I search the page tomorrow the process is successful but when I search the page today there is a problem , I used Fragment
the problem here : https://www.youtube.com/watch?v=FiHGME6i97I&feature=youtu.be
codes :
public void btn_search(View view) {
TextView txtexample = (TextView)findViewById(R.id.txtexample);
txtexample.setText("hello");
}
fragment days
<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.example.user.weather.Fragment_days">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/txtexample"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
fragment days
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_days, container, false);
return v;
}
and fragment today and tomorrow are empty.
please help me , two weeks ago I was looking for a problem
Use the following (note two lines with txtexample):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_days, container, false);
TextView txtexample = (TextView)v.findViewById(R.id.txtexample);
return v;
}
and
public void btn_search(View view) {
txtexample.setText("hello");
}

Set HorizontalGridView to not Scroll in Vertical

I'm using the HorizontalGridView to create a slide of products, but I only need that the slide scroll in horizontal. I try setVerticalScrollBarEnable(false) but don't work.
I think that the problem isn't the Scroll, because is a movement like as moving all horizontalgridview in all directions.
Part of my code:
public class ListProductsFragment extends android.support.v4.app.Fragment {
private HorizontalGridView gridView;
private OffersAdapter offersAdapter;
private ArrayList<Offer> offers;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
offersKey = getArguments().getStringArrayList(Constants.EXTRA_OFFERS_KEYS);
View view = inflater.inflate(R.layout.list_products_fragment,null);
gridView = (HorizontalGridView) view.findViewById(R.id.product_gridview);
gridView.setNumRows(2);
gridView.setRowHeight(225);
return view;
}}
my layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:background="#android:color/transparent"
android:layout_height="500dp">
<android.support.v17.leanback.widget.HorizontalGridView
android:id="#+id/product_gridview"
android:layout_width="match_parent"
android:layout_height="500dp"
/>
</LinearLayout>

Categories

Resources