floating action button animation works only on first click - android

fab antimation only work on first time I click. after that when I click on the button, on click listener works and fab show but without animation. I am creating it on fragment. mAddFab is the main button to show other fab's and mRequestFab, mDonateFab are the buttons which shows after mAddFab button clicked. Tell me if animation file needed
public class NameFragment extends Fragment {
private FloatingActionButton mAddFab, mRequestFab, mDonateFab;
private Animation mFabOpenAnim, mFabCloseAnim, rotateOpen, rotateClose;
private boolean isOpen;
private Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
context = getContext();
super.onCreate(savedInstanceState);
mFabOpenAnim = AnimationUtils.loadAnimation(context, R.anim.fab_open_anim);
mFabCloseAnim = AnimationUtils.loadAnimation(context, R.anim.fab_close_anim);
rotateOpen = AnimationUtils.loadAnimation(context, R.anim.rotate_open_anim);
rotateClose = AnimationUtils.loadAnimation(context, R.anim.rotate_close_anim);
isOpen = false;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view = inflater.inflate(R.layout.fragment_plasma, container, false);
RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
RecyclerViewAdapter adapter = new RecyclerViewAdapter();
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
mAddFab = view.findViewById(R.id.addbtn);
mRequestFab = view.findViewById(R.id.requestbtn);
mDonateFab = view.findViewById(R.id.donatebtn);
mAddFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isOpen){
mAddFab.setAnimation(rotateClose);
mRequestFab.setAnimation(mFabCloseAnim);
mDonateFab.setAnimation(mFabCloseAnim);
mRequestFab.setVisibility(View.INVISIBLE);
mDonateFab.setVisibility(View.INVISIBLE);
isOpen = false;
} else {
mAddFab.setAnimation(rotateOpen);
mDonateFab.setAnimation(mFabOpenAnim);
mRequestFab.setAnimation(mFabOpenAnim);
mDonateFab.setVisibility(View.VISIBLE);
mRequestFab.setVisibility(View.VISIBLE);
isOpen = true;
}
}
});
return view;
}
}

I found the solution. the problem was with the function that I have used to do the animation which is setAnimation() and I have changed it with startAnimation().Main differences between both the functions are-
SetAnimation-
when the view is added to the viewGroup, animation will be called and when the view has been added, the animation will not be called
StartAnimation-
animation will be called all the time even though the view has been added.

Related

Fragement class inside a recycler view

i know that i can use a recycler view to show a list of item but what if I wanna show a list of existing Fragments?
I'll try to explain better with an example:
In my app I have a fragment called review that's look like this:
public class ReviewView extends Fragment implements ReviewViewContract.View {
private TextView tvContent, tvName,tvDate,tvReactionCounter, tvCommentCountCounter;
private ImageView userPhoto;
private ImageButton btnMore;
private Button btnComment, btnLike;
private PopupMenu popup;
private ReviewViewContract.Presenter presenter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.component_review, container, false);
presenter = new ReviewViewPresenter(this,getContext(),getArguments());
initComponent(v);
presenter.getDetail();
return v;
}
//Some view methods...
#Override
public void initComponent(View view) {
userPhoto = view.findViewById(R.id.img_userphoto_profile);
tvName = view.findViewById(R.id.tv_name_profile);
tvDate = view.findViewById(R.id.tvdate_profile);
tvContent = view.findViewById(R.id.content_review_component);
tvReactionCounter = view.findViewById(R.id.tv_reactionCounter);
tvCommentCountCounter = view.findViewById(R.id.tv_commentsCounter);
btnComment = view.findViewById(R.id.btn_comment);
btnLike=view.findViewById(R.id.btn_like);
popup= new PopupMenu(view.getContext(), btnMore);
popup.getMenuInflater()
.inflate(R.menu.menu_review, popup.getMenu());
btnMore = view.findViewById(R.id.more_review);
popup = new PopupMenu(view.getContext(), btnMore);
popup.getMenuInflater()
.inflate(R.menu.menu_review, popup.getMenu());
btnMore.setOnClickListener(v->popup.show());
}
#Override
public void setDetail(String title, String date, String authorImageURL, String overView) {
tvName.setText("ciao");
}
private void likeBtnListener(){...};
private void commentsBtnListener(){...};
}
Now I need to show an indefinite number of reviewView in my activity, I thought to use a recycler view to do that but I do not want to rewrite all methods of ReviewView in the viewHolder class,(it doesn't seem a smart thing to do) so what should I do?
PS: I know that I can archive this with something like
Review fragment = new ReviewView();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
But this way doesn't seem the best way to do this.

Fragment goes blank when returning from another activity

There are two activities in my app:
MainActivity (containing 3 fragments)
FragmentHome
FragmentOrders
FragmentAccount
AccountEditActivity
The code to set fragments in MainActivity is this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
// set Home as the default fragment
setFragment(FragmentMainHome.getInstance());
}
private void setFragment(Fragment fragment){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, fragment);
transaction.commit();
}
The code for FragmentAccount is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_activity_main_account, container, false);
layout = rootView.findViewById(R.id.layout_fragment_main_account);
etName = (EditText) layout.findViewById(R.id.etNameLabelValue);
etEmail = (EditText) layout.findViewById(R.id.etEmailLabelValue);
etGender = (EditText) layout.findViewById(R.id.etGenderLabelValue);
etPhoneNumber = (EditText) layout.findViewById(R.id.etPhoneNumberLabelValue);
btnEditAccount = (ImageButton) layout.findViewById(R.id.btnEditAccount);
btnManageAddresses = (ImageButton) layout.findViewById(R.id.btnAccountManageAddresses);
btnManageAddresses.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
btnEditAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
return rootView;
}
The code for FragmentOrders is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_activity_main_order_history, container, false);
layout = rootView.findViewById(R.id.layout_fragment_main_order_history);
lvOrders = (ListView) layout.findViewById(R.id.lvOrders);
tvNoOrdersFound = (TextView) layout.findViewById(R.id.tvNoOrdersFound);
final SwipeRefreshLayout pullToRefresh = rootView.findViewById(R.id.swipe_refresh_layout_order_history);
pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// load orders
pullToRefresh.setRefreshing(false);
}
});
lvOrders.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
return rootView;
}
and similar kind of code goes for FragmentHome
The Problem
I can move between fragments and the view works fine.
Now from Account fragment, I move to AccountEditActivity
I do some update there and come back to MainActivity by pressing hardware back button
FragmentHome is displayed fine but when I click on FragmentAccount, screen goes blank
Now, if I click on Home fragment and click FragmentAccount again, it displays fine.
What is wrong here?
better use viewpager in your activity to show the fragments
it's easy to manage while using viewpagers
have a look at the link
Fragments in viewpager
and in your case just move this code from onCreate to onResume
setFragment(FragmentMainHome.getInstance());
for understanding better have a look at the lifecycle of activity in android
Activity life cycle

Custom footer menu appear after clicking floating button

How I can apply the animation of material design to display a custom footer menu after clicking on the floating button?
Components – Buttons: Floating Action Button
First create a class extends BottomSheetDialogFragment
public class FooterSheetDialogFragment extends BottomSheetDialogFragment {
public static FooterSheetDialogFragment newInstance() {
return new FooterSheetDialogFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.footer_al_dia, container, false);
return v;
}
}
Second: `
mFloatingButton = (FloatingActionButton) view.findViewById(R.id.floatingButtonAlDia);`
mFloatingButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BottomSheetDialogFragment bsdFragment = FooterSheetDialogFragment.newInstance();
bsdFragment.show(FragmentAlDia.this.getFragmentManager(), "BSDialog");
}
}
);

Property Animaton

In the recent days, I plan to write a guide animation, I make use of viewpager. but in the second pager, I used the property Animation, but it don't show as i image.this is the code.
public class HabitFragmnet extends BaseFragment {
private ImageView first;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.habit_layout, null);
first = (ImageView) rootView.findViewById(R.id.habit_button);
startAnimaton();
return rootView;
}
#Override
public void startAnimaton() {
ValueAnimator firetAnim = ObjectAnimator.ofFloat(first,"scale",0.0f,1.0f);
firetAnim.setDuration(1000);
first.setPivotX(first.getWidth());
first.setPivotY(first.getHeight());
first.invalidate();
firetAnim.setInterpolator(new CycleInterpolator(2));
AnimatorSet set = new AnimatorSet();
set.play(firetAnim);
set.start();
}
#Override
public void stopAnimation() {
}
who could give me some solution?

ListView in Fragment not refreshed

I created a tabbed app where the main acitvity contains two layouts. The top layout contains the actual tabbar (which is a fragment). Underneth the content layout contains another fragments depending on which tabbar button the user has clicked. In order to stay compatible with older android versions I use android.support.v4.app.Fragment.
So, in the tabbar menu I added two buttons and each button triggers another fragment in the content. This is what the code looks like:
public class TabbarMenuFragment extends Fragment implements View.OnClickListener {
LiveFragment liveFragment;
OddsFragment oddsFragment;
FragmentTransaction fragmentTransaction;
Button liveButton;
Button oddsButton;
public TabbarMenuFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
liveFragment = new LiveFragment();
oddsFragment = new OddsFragment();
fragmentTransaction = getFragmentManager().beginTransaction().add(R.id.content_view, oddsFragment);
fragmentTransaction.commit();
View rootView = (View) inflater.inflate(R.layout.tabbar_menu, container, false);
liveButton = (Button) rootView.findViewById(R.id.liveButton);
liveButton.setOnClickListener(this);
oddsButton = (Button) rootView.findViewById(R.id.oddsButton);
oddsButton.setOnClickListener(this);
return rootView;
}
public void updateUIInTabs() {
liveFragment.updateUI();
}
#Override
public void onClick(View v) {
if(v == liveButton) {
fragmentTransaction = getFragmentManager().beginTransaction().replace(R.id.content_view, liveFragment);
fragmentTransaction.commit();
}
if(v == oddsButton) {
fragmentTransaction = getFragmentManager().beginTransaction().replace(R.id.content_view, oddsFragment);
fragmentTransaction.commit();
}
updateUIInTabs();
}
}
The important thing is that the liveFragment is a Fragment that contains a ListView:
public class LiveFragment extends Fragment {
LiveMainAdapter adapter;
ListView list;
LayoutInflater inflater;
View rootView;
public LiveFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
this.inflater = inflater;
rootView = (View) inflater.inflate(R.layout.live_fragment, container, false);
adapter = new LiveMainAdapter(inflater);
list = (ListView) rootView.findViewById(R.id.live_fragment_ListView);
list.setAdapter(adapter);
list.setDivider(null);
list.setDividerHeight(0);
return rootView;
}
public void updateUI() {
if(list != null) {
list.invalidate();
adapter.createRowObjects();
adapter.notifyDataSetChanged();
}
}
}
The oddsFragment is so far just a dummy with a TextView:
public class OddsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = (View) inflater.inflate(R.layout.odds_fragment, container, false);
return rootView;
}
}
Now there are two settings which are important:
First:
As described above: I start the app and the oddsFragment is initally shown. Now, when I click on the liveFragment button the liveFragment's ListView is not updated.
Clicking again on the button for oddsFragment the oddsFragment is properly shown. Clicking back to liveFragment I get nothing - no ListView is shown.
The second setting:
Is exactly the same as the first, but instead I set the liveFragment as the intial view that is in TabbarMenuFragment I replace:
fragmentTransaction = getFragmentManager().beginTransaction().add(R.id.content_view, oddsFragment);
with
fragmentTransaction = getFragmentManager().beginTransaction().add(R.id.content_view, liveFragment);
Then the ListView is properly shown and the data in the list view is updated and displayed correctly. However, if I click on oddsFragement and then again on liveFragement the ListView has disappeared.
I already tried different things for over two hours now and I'm pretty desperated because I have not clue what might be the reason for this wired behaviour.
Anybody got an idea what is going on here?
Give this a try...
In your LiveFragment add a Handler and a Runnable and update the UI from your Runnable.
Handler handler = new Handler();
Runnable updater = new Runnable()
{
public void run()
{
if(list != null) {
list.invalidate();
adapter.createRowObjects();
adapter.notifyDataSetChanged();
}
}
};
and then change your updateUI() method in LiveFragment
public void updateUI()
{
handler.post(updater);
}

Categories

Resources