Recyclerview has no LayoutManager android studio - android

I'm trying to implement RecyclerView in my android app but it gives me error
"RecyclerView has no Layoutmanager"
even though I initialize it in my code. My java and xml file:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_list, container, false);
mRecyclerView = rootView.findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
mRecyclerView.setLayoutManager(layoutManager);
mAdapter = new CustomAdapter(mDataset);
mRecyclerView.setAdapter(mAdapter);
return rootView;
}
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".List"
android:background="#color/black">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="495dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"/
</androidx.constraintlayout.widget.ConstraintLayout>

You can't use signal activity for recy.... And main activity . Plz create a new xml file like list_file

Are you sure that added recyclerview dependency into gradle
AndroidX
implementation 'androidx.recyclerview:recyclerview:1.1.0'
Support library
implementation 'com.android.support:recyclerview-v7:28.0.0'

Related

How do I resolve InflateException: Binary XML file line problem?

I'm going to inflate the recycler view in the home fragment using the Navigation component. There are two problems here.
InflateException Issues
Only one first index appears in view in ArrayList
here is error message.
Caused by: android.view.InflateException: Binary XML file line #11 in kr.hnu.project:layout/activity_navigation: Binary XML file line #10 in kr.hnu.project:layout/content_navigation: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #10 in kr.hnu.project:layout/content_navigation: Error inflating class fragment
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void kr.hnu.project.MyRecyclerAdapter.notifyDataSetChanged()' on a null object reference
at kr.hnu.project.ui.home.HomeFragment.init(HomeFragment.java:81)
at kr.hnu.project.ui.home.HomeFragment.onCreateView(HomeFragment.java:55)
And this is my code.
homefragment.java:
public class HomeFragment extends Fragment {
private final static String setMsg = "SELECT sender, receiver, title, date FROM MessageDB";
DBHelper dbHelper;
SQLiteDatabase readDB;
Cursor cursor;
ArrayList<MyItem> mailItem;
MyRecyclerAdapter myAdapter;
String curUser;
private FragmentHomeBinding binding;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_home, container, false);
NavigationActivity main = (NavigationActivity) getActivity();
curUser = main.getCurrentUserID();
dbHelper = new DBHelper(inflater.getContext());
readDB = dbHelper.getReadableDatabase();
mailItem = new ArrayList<MyItem>();
init();
myAdapter = new MyRecyclerAdapter(mailItem);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
binding.recyclerview.setLayoutManager(layoutManager); // 앞에 binding!!!
binding.recyclerview.setAdapter(myAdapter);
return root;
}
public void init() {
cursor = readDB.rawQuery(setMsg, null);
mailItem.clear();
while (cursor.moveToNext()) {
if (curUser.equals(cursor.getString(cursor.getColumnIndexOrThrow("receiver")))) {
mailItem.add(new MyItem(cursor.getString(cursor.getColumnIndexOrThrow("sender")),
cursor.getString(cursor.getColumnIndexOrThrow("title")), cursor.getString(cursor.getColumnIndexOrThrow("date"))
));
}
cursor.close();
myAdapter.notifyDataSetChanged();
}
}
fragment_home.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ui.home.HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
activity_navigation.xml:
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
android:id="#+id/app_bar_navigation"
layout="#layout/app_bar_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_navigation"
app:menu="#menu/activity_navigation_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
content_navigation.xml:
<androidx.constraintlayout.widget.ConstraintLayout 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_navigation">
<fragment
android:id="#+id/nav_host_fragment_content_navigation"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
You are calling notifyDataSetChanged() on myAdapter inside init before initializing it. That's why it's throwing NullPointerException.
Move init() call after you've initialized myAdapter
myAdapter = new MyRecyclerAdapter(mailItem);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
binding.recyclerview.setLayoutManager(layoutManager); // 앞에 binding!!!
binding.recyclerview.setAdapter(myAdapter);
init(); //here move it down after initialising myAdapter

Generated FragmentBinding does not contain my CustomView id

I'm using databinding in my android project and my dashboard_fragment_layout.xml contain LinearLayout which contain TextView and CustomView:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.ui.dashboard.DashboardFragment">
<data>
<variable
name="ViewModel"
type="com.example.ui.dashboard.DashboardViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvSome"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<view
android:id="#+id/viewCustom"
class="com.example.ui.dashboard.CustomView"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="bottom|center_horizontal" />
</LinearLayout>
</layout>
When i trying to access my custom view via generated FragmentDashboardBinding:
mCustomView = mBinding.viewCustom;
i'm getting 'Can't resolve symbol viewCustom' in AndroidStudio. And i don't have this problem with TextView, it's accessible from mBinding object:
mSomeTextView = mBinding.tvSome; // all fine, no errors
I'm always getting this error with custom views, and the only way to access my custom view object is to do this by old way with findViewById:
mCustomView = view.findViewById(R.id.viewCustom); // that works
All together:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mViewDataBinding = DataBindingUtil.inflate(inflater, fragment_dashboard_layout, container, false);
view = mViewDataBinding.getRoot();
mCustomView = mViewDataBinding.viewCustom; // 'Can't resolve symbol viewCustom
mTextView = mViewDataBinding.tvSome; // all fine
mCustomView = view.findViewById(R.id.viewCustom); // that works
}
How can i access custom views via generated databinding object ?
Change to com.example.ui.dashboard.CustomView in xml code .
And remove the class in xml code .
Try this .
<com.example.ui.dashboard.CustomView
android:id="#+id/viewCustom"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="bottom|center_horizontal" />
You have used wrong view tag. try replacing view tag to View
Replace your view code to this.
<View
android:id="#+id/viewCustom"
class="com.example.ui.dashboard.CustomView"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="bottom|center_horizontal" />
Happy coding!!

Unable to update views through data binding on Android

I was trying to migrate from ButterKnife to Android data binding.
When using ButterKnife (which is working perfectly fine):
// Called after a network call
public void updateView(MyAdapter adapter, String title) {
toolbar.setTitle(title);
recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
Using Android data binding:
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
binding = MyFragmentBinding.inflate(inflater, container, false);
binding.toolbar.setTitle(R.string.app_name); // this works
return binding.getRoot();
}
// Called after a network call
public void updateView(MyAdapter adapter, String title) {
binding.toolbar.setTitle(title);
binding.recyclerView.setHasFixedSize(true);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
binding.recyclerView.setLayoutManager(layoutManager);
binding.recyclerView.setAdapter(adapter);
}
After calling updateView on the later code, the screen goes blank.
I debugged to find out that view remains attached to the Fragment.
Also, if I am updating something inside onCreateView then that works.
Is there anything am I doing wrong?
Layout:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="#dimen/_6sdp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:navigationIcon="#drawable/default_nav_icon_back"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize" />
</android.support.design.widget.CoordinatorLayout>
</layout>

how to use Recycler View in custom dialog [duplicate]

This question already has answers here:
How can I display a list view in an Android Alert Dialog?
(12 answers)
Closed 6 years ago.
How can I display a list view in an Android Alert Dialog? i tried this.
Try this:
custom_dialog.xml
<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.RecyclerView
android:id="#+id/my_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Activity/Fragment.java
RecyclerView mRecyclerView;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
View content = inflater.inflate(R.layout.custom_dialog, null);
builder.setView(content);
mRecyclerView = (RecyclerView) content.findViewById(R.id.my_recycler_view);
you should extend DialogFragment and create custom dialog which will have recycler view in it.
Layout :-fragment_dialog.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
your custom dialog :- MyDialogFragment
public class MyDialogFragment extends DialogFragment {
private RecyclerView mRecyclerView;
// this method create view for your Dialog
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//inflate layout with recycler view
View v = inflater.inflate(R.layout.fragment_dialog, container, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.recycler_view);
//setadapter
//get your recycler view and populate it.
return v;
}
}

Setting scrollbars property in RecyclerView crashes my app

I recently started using RecyclerView replacing older ListView. But whenever I use android:scrollbars="vertical" in android.support.v7.widget.RecyclerView element, my app crashes with the following error:
Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
This is how I'm using RecyclerView:
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_fragment, container, false);
return rootView;
}
protected void onPostExecute(Boolean result) {
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.recyclerview);
LinearLayoutManager llm = new LinearLayoutManager(context);
rv.setLayoutManager(llm);
RecycleViewAdapter adapter = new RecycleViewAdapter(myRecyclerViewAdapter);
rv.setHasFixedSize(true);
rv.setAdapter(adapter);
}
And this is my_fragment.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Everything works perfectly fine if I don't use android:scrollbars="vertical". Thanks for your help.
When using the RecyclerView you don't really need to specify the scrollbars display; it is the LayoutManager job where you specify if it is Vertical or Horizontal orientation and of course the scrollbar display will automatically change whatever orientation you choose.
//for vertical scrollbar and orientation
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false);
//for horizontal scrollbar and orientation
LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);

Categories

Resources