I'm trying to animate adding child views to a LinearLayout. Just wanted to know if there is a better way to go about this or if this approach have any drawbacks.
And if this post helped don't forget to up vote :)
Here is what I did:
public class CardLayout extends LinearLayout {
public CardLayout(Context context) {
super(context);
initLayoutObserver();
}
public CardLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initLayoutObserver();
}
#Override
public void addView(View child) {
super.addView(child);
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.slide_up_in);
animation.setStartOffset(getChildCount() * 100 + getResources().getInteger(R.integer.screen_transition_time_fade_in));
child.startAnimation(animation);
}
}
Related
I know the we can turn off the overscroll via overscroll mode. But how do i control it in a way the that bottom has the overscroll effect while the top doesn't has the overscroll effect in a scroll view?
There are one method in ScrollView class called getBottomFadingEdgeStrength() so you can create a Class that extends ScrollView and override the above method. like
/**
* Created by droidwithme on 23/05/16.
*/
public class MyScrollView extends ScrollView {
public MyScrollView(Context context) {
super(context);
}
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected float getBottomFadingEdgeStrength() {
return 0.0f;
}
}
and return a 0 to this method. this may help you.
In this example the contentScrim attribute is set with a color, but I can't figure out how to control when it starts. I woud like to start the color transition sooner.
Can you give me a hint? Thanks in advance.
You'd have to create a class that extends CollapsingToolbarLayout. Something like this (you might need to adjust that so it exactly fits your needs):
public class CustomCollapsingToolbarLayout extends CollapsingToolbarLayout {
public static interface Listener {
public void onContentScrimAnimationStarted(boolean showing);
}
private Listener mListener;
public CustomCollapsingToolbarLayout(Context context) {
super(context);
}
public CustomCollapsingToolbarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomCollapsingToolbarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setScrimsShown(boolean shown, boolean animate) {
super.setScrimsShown(shown, animate);
if (animate && mListener != null) {
mListener.onContentScrimAnimationStarted(shown);
}
}
public void setListener(Listener listener) {
mListener = listener;
}
}
And just call setListener on your CustomCollapsingToolbarLayout instance.
CustomCollapsingToolbarLayout mToolbarLayout =
(CustomCollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
mToolbarLayout.setListener(new Listener() {
#Override
public void onContentScrimAnimationStarted(boolean showing) {
//do what you want
}
});
EDIT (actually answering the question):
Modify the scrimVisibleHeightTrigger value (with the setScrimVisibleHeightTrigger method of the CollapsingToolbarLayout) to change the starting point of the animation.
I've a custom view that extend from view now I want to compound two of them in one and make a new custom view. Now I want to know whether I should redraw my child views in new custom view or there is a way to add them in new custom view.
public class Selector extends View {
// properties and methods
private void init(Context context) {
}
public Selector(Context context) {
super(context);
init(context);
}
public Selector(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// set measures
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// drawing
}
}
Now I want to compound two of Selectors in a view. how can I do it? Thanks
you could try something like this
public class myView extends LinearLayout {
public myView(Context context) {
super(context);
addView(new Selector(context));
addView(new Selector(context))
}
}
I am Having a 10 Image Views Inside a Scroll View
once the Image is clicked I want to perform an action
But when I am trying to scroll the Imags in the ScrollView Touch_down and Touch_UP together are considered as a click
Help me out
I know the solution is easy
But I think I am missing some Logic
I am Putting My Code Here
public class CustomScrollView extends ScrollView {
public CustomScrollView(Context context) {
super(context);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
#Override
public boolean onTouchEvent(MotionEvent p_event) {
if (p_event.getAction() == MotionEvent.ACTION_MOVE && getParent() != null) {
getParent().requestDisallowInterceptTouchEvent(true);
}
return super.onTouchEvent(p_event);
}
}
In this ScrollView I have added an ImageView
well, i'm gonna try to answer..
i think (in my opinion), you should use onclick only for the ImageView, so when you scrolling on the ScrollView, that ImageView won't get clicked..
here a sample on my code
on the layout.xml
<ImageView
android:id="#+id/ur_img_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:contentDescription="#string/description"
android:onClick="onClickEvent"
android:src="#drawable/yourDrawable"
/>
then create the event on your activity.java import android.view.View, create a method similar to the one you use in the .xml file, (on the example android:onClick="onClickEvent")
public void onClickEvent(View v){
//do your event here
}
that's it.
I am creating my own layout based on RelativeLayout as a class in code
I have basics of the layout defined in XML R.layout.menu_layout (style, drawable for background, margin, height)
If I would not need a class then I would call inflater to do this:
RelativeLayout menuLayout = (RelativeLayout)inflater.inflate(R.layout.menu_layout, root);
But I would like to be calling my own class instead
MenuLayout menuLayout = new MenuLayout(myparams);
Since I need to create a class I need to somehow inherit the R.layout.menu_layout in constructor, how can I do that? I guess there is no this.setLayout(res); or this.setResource(res); in View. Maybe I can use the other two parameters in View constructor but I did not find any tutorial how to do that either.
public class MenuLayout extends RelativeLayout {
public MenuLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
public MenuLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public MenuLayout(Context context) {
super(context);
initView(context);
}
private void initView(Context context) {
View view = LayoutInflater.from(context).inflate(R.layout.menu_layout, null);
addView(view);
}
}
now you can use
MenuLayout menuLayout = new MenuLayout(myparams);
you can change constructors for params i think