kosas kosas - 6 months ago 56
Android Question

Fab button not working

My Activity MainFragment extends ListFragment and it creates items using this part of code

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

ViewHolder holder;

if (convertView == null) {
convertView = getActivity().getLayoutInflater()
.inflate(R.layout.list_item_drs, parent, false);

holder = new ViewHolder();
holder.dateTextView = (TextView)convertView.findViewById(R.id.drs_dateTextView);
holder.dsMainDisplay = (ImageView)convertView.findViewById(R.id.cloud_MainView);

getListView().setDivider(null);
getListView().setDividerHeight(0);
getListView().setSelector(android.R.color.transparent);

convertView.setTag(holder);

}else {
holder = (ViewHolder) convertView.getTag();
}
Cloud c = getItem(position);

return convertView;
}

}


And I wanted to put Fab button on top|bottom position of main view so I used onCreateView method to inflate my created xml layout list_view where I described Fab button

@TargetApi(11)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.list_view,null);

FrameLayout fl = (FrameLayout)inflater.inflate(R.layout.list_view, parent,false);

FloatingActionButton fab = (FloatingActionButton) fl.findViewById(R.id.addNew);
fab.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick (View view) {

//Intent i = new Intent(getActivity(), AddActivity.class);
// getActivity().startActivity(i);

}

});


list_view xml this code is in FrameLayout when I use other types of layouts it disappears

<ListView android:id="@id/android:list"
android:layout_height="match_parent"
android:layout_width="match_parent"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/addNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:useCompatPadding="true"
app:elevation="5dp"
app:pressedTranslationZ="0dp"
android:src="@android:drawable/ic_dialog_email"/>


Now Fab button appears in right position and stays in place when I scroll through list but now it doesn't show any response. Any ideas how to fix it?

Answer

Since the fab is defined in the list_view.xml then you have to access it by

FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.addNew);

then the onCreateView must return the view just inflated, that is,

return v;