How to use MVVM with any fragment? - android

I have a TabLayout on a activity and when user clicks on any tab then a fragment is loaded. One of those fragments contains a NavigationView running inside a DrawerLayout. Now the problem is I have to implement MVVM for the whole app and so for the fragments too, I have done this for Activities but I am having trouble while implementing this for Fragments and how to deal with ItemSelected with MVVM and what to bind and how? I am unable to find a proper working sample of the same. Here's the code:
MoreFragment.java:
package com.abc.Fragments;
import android.arch.lifecycle.ViewModel;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.abc.MainActivity;
import com.abc.MoreFragmentViewModel;
import com.abc.R;
public class MoreFragment extends Fragment {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ViewModel viewModel;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewModel = ViewModelProviders.of(this).get(MoreFragmentViewModel.class);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragView = inflater.inflate(R.layout.fragment_more, container, false);
return fragView;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mDrawerLayout = getView().findViewById(R.id.drawer_layout);
mDrawerLayout.openDrawer(Gravity.END);
}
}
fragment_more.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/grey"
app:navigationItemSelectedListener="#{()-> viewModel.onMyItemSelected()}"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
MoreFragmentViewModel.java:
package com.abc;
import android.arch.lifecycle.ViewModel;
import android.databinding.BaseObservable;
public class MoreFragmentViewModel extends ViewModel {
public void onMyItemSelected(){
System.out.print("Item Clicked");
}
}
Please do any possible help.

Use:
DataBindingUtil.inflate(inflater, layoutId(), container, false)
To inflate your layout.
And don't forget to add:
<data>
<variable
name="model"
type="MoreFragmentViewModel"/>
</data>
To your fragment layout.

Related

Render error Unknown Fragment Android Studio

Hello I am wondering how to clear a render error I am having with a fragment which is saying unknown fragment. I can seem to find what I did wrong to fix it. I have a recycleview in the fragment that is just a test to make sure that is working as well right now.
Here is the Xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="jlfletcher.cpsc4367.ualr.edu.hw2.MainActivity">
<fragment
android:id="#+id/fragmentone"
android:name="layout.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here is the Java of the fragment
package layout;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import jlfletcher.cpsc4367.ualr.edu.hw2.CPSClist;
import jlfletcher.cpsc4367.ualr.edu.hw2.R;
public class FragmentOne extends Fragment
{
public void OnCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View View = inflater.inflate(R.layout.fragment_fragment_one, container, false);
RecyclerView recyclerView =
(RecyclerView)View.findViewById(R.id.Class_list);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
CPSClist textAdapter= new CPSClist();
recyclerView.setAdapter(textAdapter);
List<String> stringList = new ArrayList<>();
stringList.add("This");
stringList.add("is");
stringList.add("a");
stringList.add("of");
stringList.add("the");
stringList.add("recycle");
stringList.add("view");
stringList.add("system");
stringList.add("this");
stringList.add("is");
stringList.add("only");
stringList.add("a");
stringList.add("test.");
List<String> list = new ArrayList<>();
list.addAll(stringList);
list.addAll(stringList);
list.addAll(stringList);
list.addAll(stringList);
list.list(stringList);
list.addAll(stringList);
textAdapter.setItems(list);
}
}
Thanks again for the help on this.

cannot resolve FragmentTransaction.add() method . Why am I getting this error?

I have already looked at the other answers but none of them help me.
I made sure to use import android.support.v4.app.Fragment; so that's not the problem.
MainActivity.java
package com.example.nirvan.fragmentsexample2;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myFragment myfragment=new myFragment();
FragmentTransaction fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer,myfragment);
fragmentTransaction.commit();
}
}
Error :
Error:(20, 28) error: no suitable method found for add(int,myFragment)
method FragmentTransaction.add(Fragment,String) is not applicable
(argument mismatch; int cannot be converted to Fragment)
method FragmentTransaction.add(int,Fragment) is not applicable
(argument mismatch; myFragment cannot be converted to Fragment)
Why am I getting this?
In activity_main.xml I have a FrameLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:id="#+id/fragmentContainer">
</FrameLayout>
myFragment.java
package com.example.nirvan.fragmentsexample2;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class myFragment extends Fragment
{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState)
{
return inflater.inflate(R.layout.fragment,container,false);
}
public myFragment(){}
}
fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="145dp"
android:text="fragment One"/>
</RelativeLayout>
Sloppy mistake . You should call v4.app.Fragment instead of app.Fragment .
Open myFragment.java
Don't
import android.app.Fragment;
Do
import android.support.v4.app.Fragment;
Change this
import android.app.Fragment;
to
import android.support.v4.app.Fragment;
in myFragment class
Also i would suggest to have proper naming convention for your fragment.
No need for android:orientation="vertical" in relative layout.

Android Fragment not inflating after transaction

I've been trying to inflate a simple contentFragment after clicking on the tab from my navigation drawer. Here is my CentralView class that contains the attempt to inflate the Fragment
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.Toast;
#SuppressWarnings("deprecation")
public class CentralView extends AppCompatActivity {
private Toolbar toolbar;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_central_view);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.home:
Toast.makeText(getApplicationContext(),"Inbox Selected",Toast.LENGTH_SHORT).show();
ContentFragment fragment = new ContentFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame,fragment);
fragmentTransaction.commit();
return true;
And here is my ContentFragment class
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Admin on 04-06-2015.
*/
public class ContentFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_content_fragment,container,false);
return v;
}
}
I've searched StackOverflow and while people have had similar issues, none of the fixes worked for me. Apologies if it's out there. Here is the XML for my content Fragment as well
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INBOX"
android:padding="8dp"
android:textColor="#fff"
android:background="#color/colorPrimary"
android:textSize="28sp"
android:id="#+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
edit: adding activity_central_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".CentralView">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar"/>
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="#layout/header"
app:menu="#menu/drawer"/>
</android.support.v4.widget.DrawerLayout>
I suspect your tool_bar layout might be having a height of match_parent.
Could you please share tool_bar file?

Facing Issues when working with fragment

I have to create an application in which I have to work with Fragment.
In the MainActivity, there is a webView. From the SecondActivity I
have starting using Fragment.
Here is the code of SecondActivity:
package com.dev.testapp;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
public class Second extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
public void choosefragment(View view) {
Fragment fg;
if(view == findViewById(R.id.secondbtn2)) {
fg = new SecondFragment();
}
else
{
fg = new FirstFragment();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frag1, fg);
fragmentTransaction.commit();
}
}
Hhere is its second.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff" >
<Button
android:id="#+id/secondbtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first fragment"
android:onClick="chooseFragment" />
<Button
android:id="#+id/secondbtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second fragment"
android:onClick="chooseFragment"/>
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.dev.testapp.fragment1"
android:id="#+id/frag1"
/>
</LinearLayout>
After that I created two more classes for Fragments as it is required
code of FirstFragment:
package com.dev.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
public View onCreate(LayoutInflater inflater,
ViewGroup container , Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1,container, false);
}
}
And here is the SecondFragment:
package com.dev.testapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment{
public View onCreate(LayoutInflater inflater,
ViewGroup container , Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment2,container, false);
}
}
When I am done with all this, my SecondActivity is showing an error that I don't know how to handle:
As noted in previous answers, your problem is incorrect Fragment library for FirstFragment.
Change line 5 of FirstFragment.java:
import android.support.v4.app.Fragment;
to:
import android.app.Fragment;
Your second fragment is of the type android.app.Fragment while your first fragment is android.support.v4.app.Fragment.
your activity use this import android.app.Fragment;
your fragment is import android.support.v4.app.Fragment;
So you either have both extending the Fragment in the app package or in the support library

Error: The method add(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, FragementTest, String)

Im trying to Use Fragements. But in MainActivity.java file the transaction.add(R.id.my_layout, testfrag, "");not Initiated correctly.please find below the code i used.
package com.example.testtabs;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.TabActivity;
import android.view.Menu;
import android.widget.TabHost;
import android.support.v4.app.Fragment;
import com.example.testtabs.R;
public class MainActivity extends TabActivity {
TabHost tab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragementTest testfrag=new FragementTest();
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.my_layout, testfrag, "");//Error is here
}
}
activity_main.xml :
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="#+id/my_layout"
>
Error: The method add(int, Fragment, String) in the type FragmentTransaction is not applicable for the arguments (int, FragementTest, String)
Fragement.test
package com.example.testtabs;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link android.support.v4.app.Fragment} subclass.
*
*/
public class FragementTest extends Fragment {
public FragementTest() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragement, container, false);
}
}
change:
import android.app.FragmentManager;
import android.app.FragmentTransaction;
to:
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
or---
change:
import android.support.v4.app.Fragment;
to
import android.app.Fragment;
All the Fragment should be android.app.Fragment. If you want to use android.support.v4.app.Fragment, you should use getSupportFragmentManager() to get the Manager.
Its working for me:
Instead of using getFragmentManager use getSupportFragmentManager();

Categories

Resources