Error inflating RecyclerView in fragment - android

I want to use a recyclerview in fragment but i got this error :
java.lang.RuntimeException: Unable to start activity ComponentInfo: android.view.InflateException: Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
Main Activity :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
public void init() {
setContentView(R.layout.main);
Fragment fr;
fr = new AuctionsList();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
Fragment :
public class AuctionsList extends Fragment {
private List<Goods> goodsList = new ArrayList<>();
private RecyclerView recyclerView;
private GoodsAdapter mAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
final View rootView = inflater.inflate(
R.layout.frg_auction_list, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
mAdapter = new GoodsAdapter(goodsList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this.getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setAdapter(mAdapter);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(this.getActivity(), LinearLayoutManager.VERTICAL));
prepareGoodsData();
mAdapter.setOnItemClickListener(new GoodsAdapter.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
Goods good = goodsList.get(position);
Toast.makeText(getActivity(), good.getTitle() + " was clicked!", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
Main.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="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal">
<Button android:id="#+id/but_terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_but_terms"/>
<Button android:id="#+id/but_guide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_but_guide"/>
<Button android:id="#+id/but_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:text="#string/main_but_login"/>
</LinearLayout>
<fragment android:name="com.ods.activity.AuctionsList"
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
Fragment layout xml:
<?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.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>
RecyclerView item xml :
<?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:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:orientation="vertical">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/title"
android:text="عنوان"
android:textColor="#color/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/lastBidPrice"
android:layout_below="#id/title"
android:gravity="right"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="356500"/>
<TextView
android:id="#+id/remainingTime"
android:layout_below="#id/lastBidPrice"
android:textColor="#color/year"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"
android:text="24:15:12"/>
</RelativeLayout>

I figured it out. I should add whole RecyclerView library (Whole folder from Android SDK) to my modules as Library module and then add it as a dependency to my main project and adding it's jar library as well.

Related

Fragment does not create its corresponding view

I have a TabLayout with 2 tabs. For each tab, I created the corresponding fragments.
The activity class (extending AppCompatActivity) is this (ToolsActivity.java):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tools);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
mViewPager = findViewById(R.id.view_pager);
mViewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(mViewPager);
tabs.getTabAt(0).setIcon(R.drawable.ic_tab_reading);
tabs.getTabAt(1).setIcon(R.drawable.ic_tab_writing);
tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
mCurrentSelectedListener = new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
// ...
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
// ...
}
};
tabs.addOnTabSelectedListener(mCurrentSelectedListener);
}
The corresponding layout is this (activity_tools.xml):
<?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"
tools:context=".ToolsActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="#dimen/appbar_padding"
android:text="#string/title_activity_tools"
android:textAppearance="#style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabInlineLabel="true" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
The pager adapter contains an array of Fragment object.
mLstFragments.add(new UHFReadFragment());
mLstFragments.add(new UHFWriteFragment());
that adapter also contains this piece of code:
#Override
public Fragment getItem(int position) {
if (mLstFragments.size() > 0) {
return mLstFragments.get(position);
}
throw new IllegalStateException("No fragment at position " + position);
}
First tab is UHFReadFragment. This is part of the code of it:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mContext = (ToolsActivity) getActivity();
mSound = new Sound(mContext);
mTagList = new ArrayList<HashMap<String, String>>();
mAdapter = new SimpleAdapter(mContext, mTagList, R.layout.listtag_items,
new String[]{"tagUii", "tagLen", "tagCount", "tagRssi"},
new int[]{R.id.TvTagUii, R.id.TvTagLen, R.id.TvTagCount,
R.id.TvTagRssi});
mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
String result = msg.obj + "";
String[] strs = result.split("#");
addEPCToList(strs[0], strs[1]);
mSound.playSound(1);
}
};
mUHF = new UHF(mContext, mHandler);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mTvCount = (TextView) getView().findViewById(R.id.tv_count);
mLvTags = (ListView) getView().findViewById(R.id.LvTags);
mLvTags.setAdapter(mAdapter);
}
And finally, this is the layout for the first fragment (uhf_read_fragment.xml):
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".ui.main.UHFReadFragment">
<LinearLayout
android:id="#+id/layout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="#color/white"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp" >
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tvTagUii"
android:textSize="15sp" />
<TextView
android:id="#+id/tv_count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="0"
android:textColor="#color/red"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tvTagLen"
android:visibility="gone" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="#string/tvTagCount"
android:textSize="15sp" />
<TextView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="RSSI"
android:textSize="15sp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#color/gray" />
<ListView
android:id="#+id/LvTags"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
The problem is that the layout of the fragments does not appear, and in fact, onViewCreated is never called.
What else is missing? I thought that by using "tools:context=".ui.main.UHFReadFragment"" the layout will be associated to the class that controls the Fragment.
Only the layout of the activity is shown (when running the app, it shows only the title and the tabs header).
Regards
Jaime
You need to have the onCreateView method in the Fragment class(UHFReadFragment) to inflate the layout
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.uhf_read_fragment, container, false);
}
Hope this will solve your issue

ListFragment onCreateView not called

I'm trying to use a FragmentManager to replace a fragment but for some reason the OnCreateView() isn't called.
This is the code I use to create the fragment:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FeedbackListFragment listFragment = new FeedbackListFragment();
fragmentTransaction.replace(R.id.feedback_placeholder, listFragment);
fragmentTransaction.commit();
This is the code for FeedbackListFragment:
public class FeedbackListFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG,"create feedbacklistview");
View view = inflater.inflate(R.layout.feedback_popup, container, false);
return view;
}
}
This is my main 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:background="#color/background_normal"
android:id="#+id/main_layout">
<FrameLayout
android:id="#+id/feedback_placeholder"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"></FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="90dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/overview_scan_text"
android:id="#+id/scantagLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="42sp"
android:textIsSelectable="false"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:id="#+id/cleanjack_image"
android:src="#drawable/logo_clean_jack"
android:layout_gravity="left|center_vertical"
android:contentDescription="#string/app_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="#+id/messageLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textIsSelectable="false"/>
</LinearLayout>
I've removed the android:visibility="gone" to see if that was the issue but it didn't work.
I tried your Sample code
Activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_list);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FeedbackListFragment listFragment = new FeedbackListFragment();
fragmentTransaction.replace(R.id.feedback_placeholder, listFragment);
fragmentTransaction.commit();
}
FragmentList :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.feedback_popup, container, false);
return view;
}
XML Activity
<?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:background="#bcbcbc"
android:id="#+id/main_layout">
<FrameLayout
android:id="#+id/feedback_placeholder"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="match_parent"></FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="90dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="overview_scan_text"
android:id="#+id/scantagLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="42sp"
android:textIsSelectable="false"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="180dp"
android:id="#+id/cleanjack_image"
android:src="#drawable/ic_launcher_background"
android:layout_gravity="left|center_vertical"
android:contentDescription="#string/app_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=""
android:id="#+id/messageLabel"
android:layout_gravity="center"
android:gravity="center"
android:textSize="20sp"
android:textIsSelectable="false"/>
</LinearLayout>
Xml Fragment
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</android.support.constraint.ConstraintLayout>
Have you set the id of listView of fragmentList to
android:id="#android:id/list"
Hope this is helpful :)
From documentation for onCreateView in ListFragment:
Provide default implementation to return a simple list view. Subclasses
can override to replace with their own layout. If doing so, the
returned view hierarchy must have a ListView whose id
is {#link android.R.id#list android.R.id.list} and can optionally
have a sibling view id {#link android.R.id#empty android.R.id.empty}
that is to be shown when the list is empty. If you are overriding this method with your own custom content,
consider including the standard layout {#link android.R.layout#list_content}
in your layout file, so that you continue to retain all of the standard
behavior of ListFragment. In particular, this is currently the only
way to have the built-in indeterminant progress state be shown.

Why isn't the view found for the id?

I'm trying to launch a fragment from an activity. However, when I run the app and click on the button that has to launch the fragment I get the error:
java.lang.IllegalArgumentException: No view found for id 0x7f0e0074 (com.example.hudhud.islam:id/kontaktfragment) for fragment Kontakt{aaaed67 #1 id=0x7f0e0074}
I can't see where I've made something wrong. It should be correct. This is the class where I implement the fragment. I'm only gonna upload the View onCreateView for this class as no more is needed:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_kontakt, container, false);
sendmail = (Button) view.findViewById(R.id.sendknap);
sendmail.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
msg = (EditText) view.findViewById(R.id.besked);
String message = msg.getText().toString();
sendemail(message);
}
});
return view;
}
I launch the fragment from here:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.Kontakt) {
Fragment fragment = new Kontakt();
getFragmentManager().beginTransaction()
.add(R.id.kontaktfragment, fragment)
.commit();
}
return super.onOptionsItemSelected(item);
}
What have I missed?
Any help is appreciated!
EDIT:
This is the XML for the activity:
<?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"
android:background="#32c6a6"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:id="#+id/velkomst"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_margin="10dp" />
<FrameLayout
android:id="#+id/Buttons"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="#id/velkomst" >
</FrameLayout>
</RelativeLayout>
And this is the kontakfrag 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"
android:weightSum="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/kontaktfragment"></FrameLayout>
</RelativeLayout>
And this is the fragment_kontakt XML:
<?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:background="#32c6a6"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/besked"
android:layout_weight="0.70"
android:textSize="20dp"
android:layout_marginTop="30dp"
android:textIsSelectable="true"
android:textColor="#000000"
android:background="#ffffff"
android:gravity="top"
android:paddingTop="30dp"
android:hint="Hvis du har feedback eller nogle spørgsmål, så er du velkommen til at skrive. Vi besvarer mailen hurtigst muligt"
android:scrollIndicators="right"/>
<Button
android:layout_width="144dp"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/sendknap"
android:layout_gravity="center_horizontal"
android:layout_weight="0.05"
android:textSize="20dp"/>
</LinearLayout>
My mistake was that I created the fragment as a separate layout and not as a framelayout on the frontpage. So the solution was to create a framelayout on the frontpage. That makes sense :)
First to the layout of activity, add this
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Then in that activity java code add
Fragment fragment = new Kontakt();
getFragmentManager().beginTransaction()
.replace(R.id.container, fragment)
.commit();

Card View Layout Android in fragment

I would like to implement a card view layout in a fragment, like that:
https://www.docdroid.net/VT1Fo56/esempio.pdf.html
This is the code of the fragment where i wish to insert the list:
public class Frag_Lista extends Fragment {
RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<Food_Card> list=new ArrayList<Food_Card>();
String[] name=new String[50];
String[] email=new String[50];
String[] mobile=new String[50];
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.frag_lista, container, false);
return(v);
}
#Override
public void onViewCreated (View view, Bundle savedIstanceState) {
//Azioni per generare pulsante menù
FloatingActionButton fab = (FloatingActionButton) getView().findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();*/
android.app.FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
Frag_Nuova_Ricetta FL = new Frag_Nuova_Ricetta();
FT.replace(R.id.posto_per_fragment,FL);
FT.addToBackStack(null);
FT.commit();
}
});
Context context = getActivity();
name[0]="nome0";
name[1]="nome1";
name[2]="nome2";
email[0]="email0";
email[1]="email1";
email[2]="email2";
mobile[0]="mobile0";
mobile[1]="mobile1";
mobile[2]="mobile2";
int count;
for(count=0;count<3;count++){
Food_Card food_card=new Food_Card(0,name[count],email[count],mobile[count]);
list.add(food_card);
}
recyclerView = (RecyclerView) getView().findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter=new food_adapter(list);
recyclerView.setAdapter(adapter);
}
}
Doing that i obtain this:
https://sendvid.com/2s2blbcz
I think that there is something wrong in one of the following xml and not in my Food_Card class or in the food_adapter class (if it is not, i will put their code).
card_view_layout.xml
<?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.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/card_view">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/food_image"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/food_image"
android:hint="edit1"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_alignParentTop="true"
android:id="#+id/food_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/food_name"
android:layout_toRightOf="#id/food_image"
android:hint="edit2"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="#+id/food_email"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/food_email"
android:layout_toRightOf="#+id/food_image"
android:hint="edit3"
android:textSize="18dp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="5dp"
android:id="#+id/food_mobile"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
frag_lista.xml
<?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:background="#FFFF00">
<android.support.v7.widget.RecyclerView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/recyclerView">
</android.support.v7.widget.RecyclerView>
<!--Pulsante menu-->
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_input_add"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Try to replace your loop with this:
int count;
for(count=0;count<3;count++) {
// set count as the id
Food_Card food_card = new Food_Card(count,name[count],email[count],mobile[count]);
list.add(food_card);
}
and your frag_lista.xml with:
<android.support.v7.widget.RecyclerView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/recyclerView" />
Try to change android:layout_height="wrap_content" on your root LinearLayout in card_view_layout.xml. and android:layout_height="wrap content" in RecyclerView

FAB position changes when listview's setAdapter method is called

I'm using this library for the fab implementation.
As you can see in this video, the fab position is changing when the fragment loads.
After debugging it, I found that the listview "setAdapter()" method causes the position change.
I don't have a clue why it's happening..
My layout:
<?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="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="#style/AppTheme.ActionBar" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbar">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sad_face"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_leagues" />
</LinearLayout>
</FrameLayout>
<com.melnykov.fab.FloatingActionButton
android:id="#+id/fab_new_league"
android:src="#drawable/ic_action_add"
app:fab_colorNormal="#color/orange"
app:fab_colorPressed="#color/redDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#id/toolbar"
android:layout_marginBottom="-28dp"
android:layout_marginRight="28dp"
android:elevation="8dp"/>
</RelativeLayout>
My fragment
public class LeagueListFragment extends MyFragment {
#InjectView(android.R.id.list) ListView mListView;
#InjectView(android.R.id.empty) LinearLayout mEmpty;
public static Fragment newInstance() {
return new LeagueListFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_league_list, container, false);
ButterKnife.inject(this, view);
// Fetch league list and set up adapter
List<League> list = League.getAll();
mAdapter = new LeagueAdapter(getActivity(), list);
// Set up ListView
mListView.setEmptyView(mEmpty);
mListView.setDivider(new ColorDrawable(getResources().getColor(R.color.gray)));
mListView.setDividerHeight(2);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
return view;
}
}
Margin changes in the runtime on pre-Lollipop because shadow mustn't be taken into account when margins are set.
Try to disable the shadow by :
fab:fab_shadow="false"
Fixing the issue is not expected for now.. (See makovkastar answer)

Categories

Resources