Reputation: 10095
I want to add a view to a fragment via an animation. I want the view to appear by sliding in from the left of the screen and exit by sliding out the same way.
I'm not able to get this to work. When the fragment appears, the view is already visible, there's no animation. When I press back to dismiss the fragment, the view animates to the right.
Here's the view class:
public class BannerView extends LinearLayout {
Context context;
public BannerView(Context context) {
super(context);
this.context = context;
View.inflate(context, R.layout.heading_view, this);
}
public BannerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
View.inflate(context, R.layout.heading_view, this);
}
public BannerView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
View.inflate(context, R.layout.heading_view, this);
}
public void show(final LinearLayout viewGroup){
final LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
viewGroup.addView(this,p);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Animation anim_list = AnimationUtils.loadAnimation(context, R.anim.slide_in);
LayoutAnimationController controller = new LayoutAnimationController(anim_list, 0.1f);
setLayoutAnimation(controller);
}
}
The animation Xml is
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%" android:toXDelta="0%" android:duration="1000"/>
</set>
This is how I'm adding the view:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
BannerView b = new BannerView(getActivity());
b.show((LinearLayout) getView().findViewById(R.id.footerContainerLayout));
}
I would really appreciate help with this
Upvotes: 0
Views: 1045
Reputation: 2485
I do that by another way, you can try my code: ( this is a full project). You also can download full project here
1) activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frameToReplace"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
2) MainActivity.class
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = FragmentParent.newInstance();
getFragmentManager().beginTransaction().replace(R.id.frameToReplace,fragment).commit();
}
3) mdemir_layout.xml
--> animation is occurred here.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add View"/>
<RelativeLayout
android:id="@+id/rlt"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
<FrameLayout
android:id="@+id/fragInvi"
android:layout_width="match_parent"
android:layout_height="200dp"
android:visibility="invisible"
android:background="#253">
</FrameLayout>
</LinearLayout>
4) FragmentParent.class
public class FragmentParent extends Fragment {
public FragmentParent (){
}
public static FragmentParent newInstance(){
return new FragmentParent();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mdemir_layout,container,false);
final FrameLayout fragInvi = (FrameLayout)rootView.findViewById(R.id.fragInvi);
Fragment fragment = FragmentForStefanFalk.newInstance();
getFragmentManager().beginTransaction().replace(R.id.fragInvi,fragment).commit();
//Prepair for translating View
final int screenWidth = dpToPx(getResources().getConfiguration().screenWidthDp);
// translate fragInvi to exact position and prepair size
fragInvi.setTranslationX(-screenWidth);
fragInvi.setTranslationY(-dpToPx(200));
setLayoutSize(fragInvi,screenWidth,dpToPx(200));
final boolean[] isTranslated = {false};
Button btn_Click = (Button)rootView.findViewById(R.id.btn_Click);
// translate view with animation when button is Clicked
btn_Click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!isTranslated[0]){
fragInvi.setVisibility(View.VISIBLE);
fragInvi.setTranslationX(0);
fragInvi.setTranslationX(-screenWidth);
fragInvi.animate().translationX(0).setDuration(1000);
}else{
fragInvi.animate().translationX(fragInvi.getWidth()).setDuration(1000);
}
isTranslated[0] = !isTranslated[0];
}
});
return rootView;
}
private static void setLayoutSize(View view, int width, int height) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = width;
params.height = height;
view.setLayoutParams(params);
}
private int dpToPx(int dp) {
return (int) (dp * getResources().getDisplayMetrics().density + 0.5f);
}
}
5. stefan_falk_frame.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">
<ListView
android:id="@+id/lv_test"
android:background="#253"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
6) FragmentForStefanFalk.java
public class FragmentForStefanFalk extends Fragment {
public FragmentForStefanFalk (){
}
public static FragmentForStefanFalk newInstance(){
return new FragmentForStefanFalk();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.stefan_falk_frame,container,false);
ListView lv_test = (ListView) rootView.findViewById(R.id.lv_test);
List<String> arr = new ArrayList<>();
for(int i = 0;i < 10; i++){arr.add("String "+i);}
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arr);
lv_test.setAdapter(adapter);
lv_test.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(), "Click on " + position, Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
Ok, this is all. Just test and tell me the result.
Upvotes: 0
Reputation: 2955
This will solve ur problem
myView.startAnimation(AnimationUtils.loadAnimation(mContext, android.R.anim.slide_out_left));
Upvotes: 1