I would like to create a component which would allow me to simplify a ViewFlipper.
The first view of my flipper is a progressBar, which show to the user that the content is loading, and the second view in my flipper is my content (for example another layout). In other words, I want to simplify this:
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/myContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ViewFlipper>
The first view (the progressBar) is always the same for all my activities, so the component I want to create could be used like that (file layout-activity.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">
<com.example.fred.myapplication.MyCustomComponent
android:id="@+id/myComponent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/myContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</com.example.fred.myapplication.MyCustomComponent>
</LinearLayout>
I started to create the component like that:
public class MyCustomComponent extends RelativeLayout {
ViewFlipper myFlipper;
public MyCustomComponent(Context context) {
super(context);
}
public MyCustomComponent(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyCustomComponent(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
//the main template
View template = inflate(getContext(), R.layout.template, null);
//get the viewFlipper from my template
myFlipper = (ViewFlipper) template.findViewById(R.id.myFlipper);
//add the first child of my custom component to the view flipper
myFlipper.addView(getChildAt(0));
}
/**
* Show the progressBar
*/
public void showProgressBar(){
myFlipper.setDisplayedChild(0);
}
/**
* Show my custom content which can be a textview in my example but also a LinearLayout for example...
*/
public void showContent(){
myFlipper.setDisplayedChild(1);
}
}
And the template.xml which contains the viewFlipper is:
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myFlipper"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ViewFlipper>
In my activities I want to use my custom component like that:
public class MainActivity extends Activity {
MyCustomComponent myCustomComponent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity);
myCustomComponent = (MyCustomComponent) findViewById(R.id.myComponent);
myCustomComponent.showProgressBar();
loadLongStuff();
}
private void loadLongStuff() {
//processing... long stuff
//working again...
//at the end, show my content loaded
myCustomComponent.showContent();
}
}
I created each files I wrote here but it doesn't work: my app crash but I don't have any stack trace.... can you tell me what's wrong?
Thank you.
In case you haven't been able to figure this out yet, I believe the problem is that you're missing an onMeasure method.
See Google's example for reference
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Do something
}
Again, Google's Example offers some great demonstrations on how to use this method and what to do with it.
Cheers!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.