Android: View cut off when moved out of screen -
i creating custom view can moved on screen left , right. adding view in relative layout. here of code of custom view: parent view of subtopicsview class:
import android.app.activity; import android.content.context; import android.content.res.configuration; import android.support.v4.app.fragment; import android.support.v4.app.fragmentactivity; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.util.attributeset; import android.util.displaymetrics; import android.view.view; import android.widget.relativelayout; import com.example.androidstackedview.r; import com.example.androidstackedview.views.baseview.viewstate; public class stackedview extends relativelayout { context context; view dashboardview; topicsview topicsview; subtopicsview subtopicsview; detailsview detailsview; float widthfactor = 10; public static int screenheight; public static int screenwidth; public static float xdocked = 0; public stackedview(context context) { super(context); this.context = context; init(); } public stackedview(context context, attributeset attrs) { super(context, attrs); this.context = context; init(); } private void init() { initscreendimenstions(); } @override protected void onlayout(boolean changed, int l, int t, int r, int b) { super.onlayout(changed, l, t, r, b); int orientation = context.getresources().getconfiguration().orientation; initcolumnviews(); if(orientation == configuration.orientation_landscape) { topicsview.layout(dashboardview.getwidth(), 0, (int) (dashboardview.getwidth()+screenwidth/2 - screenwidth/2 * .30f), screenheight); } else { topicsview.layout(dashboardview.getwidth(), 0, (int) (dashboardview.getwidth()+screenwidth/2), screenheight); } topicsview.setxnormal(dashboardview.getwidth()); topicsview.setxdocked(xdocked); if(!topicsview.isanimating) { topicsview.changestate(topicsview.mviewstate, false); } if(orientation == configuration.orientation_landscape) { subtopicsview.layout((int) (topicsview.getleft()+topicsview.getwidth()), 0, (topicsview.getleft()+topicsview.getwidth()) + topicsview.getwidth(), screenheight); subtopicsview.setxnormal(topicsview.getleft()+topicsview.getwidth()); } else { subtopicsview.layout((int) (screenwidth/2), 0, screenwidth, screenheight); subtopicsview.setxnormal(screenwidth/2); } subtopicsview.setxdocked(xdocked); subtopicsview.changestate(subtopicsview.mviewstate, false); // detailsview.layout((int) (xdocked), 0, screenwidth, screenheight); // detailsview.setxnormal(xdocked); // detailsview.setxdocked(xdocked); // detailsview.changestate(detailsview.mviewstate, false); if(orientation == configuration.orientation_portrait) { detailsview.layout((int) (xdocked), 0, screenwidth, screenheight); detailsview.setxnormal(xdocked + topicsview.getwidth()); detailsview.setxdocked(xdocked); detailsview.changestate(viewstate.docked, false); } else if(orientation == configuration.orientation_landscape) { detailsview.layout((int) (xdocked + topicsview.getwidth()/2), 0, screenwidth, screenheight); detailsview.setxnormal(xdocked + topicsview.getwidth()); detailsview.setxdocked(xdocked + topicsview.getwidth()/2); detailsview.changestate(viewstate.docked, false); } } public void initcolumnviews() { if(dashboardview == null) { dashboardview = findviewwithtag("dashboard"); } if(dashboardview != null) { view v = dashboardview.findviewbyid(r.id.testimg); if(v != null) { xdocked = v.getleft() + v.getwidth(); } } if(topicsview == null) { topicsview = (topicsview) findviewwithtag("topics"); } if(subtopicsview == null) { subtopicsview = (subtopicsview) findviewwithtag("subtopics"); } if(detailsview == null) { detailsview = (detailsview) findviewwithtag("details"); } } private void initscreendimenstions() { displaymetrics displaymetrics = new displaymetrics(); ((activity)context).getwindowmanager().getdefaultdisplay().getmetrics(displaymetrics); screenheight = displaymetrics.heightpixels; screenwidth = displaymetrics.widthpixels; } public void resetto1stphase() { topicsview.setvisibility(view.visible); // if(topicsview.mviewstate == viewstate.docked) { // topicsview.changestate(viewstate.normal, true); // } else { topicsview.changestate(viewstate.normal, false); // } subtopicsview.setvisibility(view.gone); subtopicsview.changestate(viewstate.normal, false); detailsview.setvisibility(view.gone); detailsview.changestate(viewstate.normal, false); } public void resetto2ndphase() { topicsview.setvisibility(view.visible); int orientation = context.getresources().getconfiguration().orientation; if(orientation == configuration.orientation_landscape) { topicsview.changestate(viewstate.normal, false); } else { if(topicsview.mviewstate == viewstate.docked) { topicsview.changestate(viewstate.docked, false); } else { topicsview.changestate(viewstate.docked, true); } } subtopicsview.setvisibility(view.visible); subtopicsview.changestate(viewstate.normal, false); detailsview.setvisibility(view.gone); detailsview.changestate(viewstate.normal, false); } public void resetto3rdphase() { // todo auto-generated method stub subtopicsview.setvisibility(view.visible); subtopicsview.changestate(viewstate.docked, false); detailsview.setvisibility(view.visible); detailsview.changestate(viewstate.normal, false); } public void loadfragmentintopicsframe(fragment f) { initcolumnviews(); fragmentmanager fm = ((fragmentactivity)context).getsupportfragmentmanager(); fragmenttransaction t = fm.begintransaction(); t.replace(topicsview.findviewbyid(r.id.fr1).getid(), f); t.commit(); resetto1stphase(); } public void loadfragmentinsubtopicsframe(fragment f) { initcolumnviews(); fragmentmanager fm = ((fragmentactivity)context).getsupportfragmentmanager(); fragmenttransaction t = fm.begintransaction(); t.replace(subtopicsview.findviewbyid(r.id.fr2).getid(), f); t.commit(); resetto2ndphase(); } public void loadfragmentindetailsframe(fragment f) { initcolumnviews(); fragmentmanager fm = ((fragmentactivity)context).getsupportfragmentmanager(); fragmenttransaction t = fm.begintransaction(); t.replace(detailsview.findviewbyid(r.id.fr3).getid(), f); t.commit(); resetto3rdphase(); } }
........... baseview i.e. parent of subtopics class:
import android.content.context; import android.util.attributeset; import android.view.motionevent; import android.widget.relativelayout; public class baseview extends relativelayout { float xnormal; float xdocked; float px; boolean isanimating = false; public viewstate mviewstate = viewstate.normal; final float touch_factor = 0.60f; final long animation_duration = 100; public baseview(context context) { super(context); // todo auto-generated constructor stub } public baseview(context context, attributeset attrs) { super(context, attrs); // todo auto-generated constructor stub } public baseview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); // todo auto-generated constructor stub } @override public boolean ontouchevent(motionevent event) { int action = event.getaction(); if(isanimating) { return true; } if(mviewstate == viewstate.docked) { return true; } if(action == motionevent.action_down) { px = event.getx(); } else if(action == motionevent.action_move) { float newx = event.getx(); float dx = newx - px; if(math.abs(dx) > 0) { float value = getleft()+(dx*touch_factor); if(value < 0) { value = 0; } layout((int)(value), 0, (int)(value+getwidth()), getheight()); } px = newx; } else if(action == motionevent.action_cancel || action == motionevent.action_up) { float tox = 0; if(mviewstate == viewstate.normal) { tox =xnormal-getleft(); } else if(mviewstate == viewstate.docked) { tox = xdocked-getleft(); } stackedanimation anim = new stackedanimation(this, 0, tox, animation_duration); anim.setlistener(new onhanimationlistener() { @override public void onstart() { isanimating = true; } @override public void onend() { isanimating = false; changestate(mviewstate, false); } }); anim.start(); } return true; } public void changestate(viewstate state, boolean animate) { mviewstate = state; if(mviewstate == viewstate.normal) { if(animate) { stackedanimation anim = new stackedanimation(this, getleft(), xnormal, animation_duration); anim.setlistener(new onhanimationlistener() { @override public void onstart() { // todo auto-generated method stub isanimating = true; } @override public void onend() { // todo auto-generated method stub isanimating = false; layout((int) xnormal, 0, (int) (xnormal+getwidth()), getheight()); } }); anim.start(); } else { layout((int) xnormal, 0, (int) (xnormal+getwidth()), getheight()); } } else if(mviewstate == viewstate.docked) { if(animate) { stackedanimation anim = new stackedanimation(this, getleft(), xdocked, animation_duration); anim.setlistener(new onhanimationlistener() { @override public void onstart() { // todo auto-generated method stub isanimating = true; } @override public void onend() { // todo auto-generated method stub isanimating = false; layout((int) xdocked, 0, (int) (xdocked+getwidth()), getheight()); } }); anim.start(); } else { layout((int) xdocked, 0, (int) (xdocked+getwidth()), getheight()); } } } public float getxnormal() { return xnormal; } public void setxnormal(float xnormal) { this.xnormal = xnormal; } public float getxdocked() { return xdocked; } public void setxdocked(float xlocked) { this.xdocked = xlocked; } public boolean isanimating() { return isanimating; } public void setanimating(boolean isanimating) { this.isanimating = isanimating; } public enum viewstate { normal, docked, none; } public enum columntype { topic, subtopic, details; } }
...... , subtopics class:
import android.app.activity; import android.content.context; import android.content.res.configuration; import android.util.attributeset; import android.util.displaymetrics; import android.widget.framelayout; public class subtopicsview extends baseview { stackedview stackview; context context; private int screenheight; private int screenwidth; public subtopicsview(context context) { super(context); this.context = context; // todo auto-generated constructor stub setbackgroundcolor(0xff888888); initscreendimenstions(); } public subtopicsview(context context, attributeset attrs) { super(context, attrs); this.context = context; // todo auto-generated constructor stub setbackgroundcolor(0xff888888); initscreendimenstions(); } public subtopicsview(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); this.context = context; // todo auto-generated constructor stub setbackgroundcolor(0xff888888); initscreendimenstions(); } private void initscreendimenstions() { displaymetrics displaymetrics = new displaymetrics(); ((activity)context).getwindowmanager().getdefaultdisplay().getmetrics(displaymetrics); screenheight = displaymetrics.heightpixels; screenwidth = displaymetrics.widthpixels; } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { // todo auto-generated method stub super.onmeasure(widthmeasurespec, heightmeasurespec); if(findviewbyid(r.id.parent) != null) { int orientation = context.getresources().getconfiguration().orientation; framelayout.layoutparams params; if(orientation == configuration.orientation_landscape) { params = new framelayout.layoutparams((int) (screenwidth/2 - screenwidth/2 * .30f), screenheight); } else { params = new framelayout.layoutparams((int) (screenwidth/2), screenheight); } findviewbyid(r.id.parent).setlayoutparams(params); } } }
the view moves on touch when view starts going out of screen either on left or right side, starts cut off other side in below screenshot. idea why view behaving this?
Comments
Post a Comment