Skip to content

Instantly share code, notes, and snippets.

@happycodinggirl
Last active August 29, 2015 14:18
Show Gist options
  • Save happycodinggirl/eac0a1b3004850917f43 to your computer and use it in GitHub Desktop.
Save happycodinggirl/eac0a1b3004850917f43 to your computer and use it in GitHub Desktop.
复合控件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
xmlns:sidespinner="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<com.example.huangxingli.androidshaderabout.SideSpinner
android:id="@+id/sidespinner_fruits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
></com.example.huangxingli.androidshaderabout.SideSpinner>
<com.example.huangxingli.androidshaderabout.SideSpinner
android:id="@+id/sidespinner_vegetables"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_below="@+id/sidespinner_fruits"
sidespinner:values="@array/vegetable_array"></com.example.huangxingli.androidshaderabout.SideSpinner>
</RelativeLayout>
package com.example.huangxingli.androidshaderabout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends ActionBarActivity {
/**
* Identifier for the state to save the selected index of
* the side spinner.
*/
private static String STATE_SELECTED_INDEX = "SelectedIndex";
/**
* Identifier for the state of the super class.
*/
private static String STATE_SUPER_CLASS = "SuperClass";
int mSelectedIndex=0;
SideSpinner mSideSpinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSideSpinner= (SideSpinner) findViewById(R.id.sidespinner_vegetables);
if (savedInstanceState!=null){
mSelectedIndex=savedInstanceState.getInt(STATE_SELECTED_INDEX);
Log.v("TAG","=====saveInstanceState is not null ---mSelectedIndex is ---"+mSelectedIndex);
}
mSideSpinner.setSelectedIndex(mSelectedIndex);
}
@Override
protected void onRestart() {
super.onRestart();
Log.v("TAG","---onRestart-----");
}
@Override
protected void onStart() {
super.onStart();
Log.v("TAG","----onStart----");
}
@Override
protected void onStop() {
super.onStop();
Log.v("TAG","---onStop----");
}
@Override
protected void onSaveInstanceState(Bundle bundle) {
bundle.putInt(STATE_SELECTED_INDEX, mSideSpinner.getSelectedIndex());
// bundle.putParcelable(STATE_SUPER_CLASS,
//bundle);
super.onSaveInstanceState(bundle);
Log.v("TAG","----onSaveInstateState-----");
}
@Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
Log.v("TAG","-----onRestoreInstanceState----");
// Bundle bundle= state.getParcelable(STATE_SUPER_CLASS);
mSideSpinner.setSelectedIndex(state.getInt(STATE_SELECTED_INDEX));
}
/* @Override
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
// Makes sure that the state of the child views in the side
// spinner are not saved since we handle the state in the
// onSaveInstanceState.
super.dispatchFreezeSelfOnly(container);
}
@Override
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
// Makes sure that the state of the child views in the side
// spinner are not restored since we handle the state in the
// onSaveInstanceState.
super.dispatchThawSelfOnly(container);
}*/
}
package com.example.huangxingli.androidshaderabout;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by huangxingli on 2015/4/8.
*/
public class SideSpinner extends LinearLayout {
private CharSequence[] mSpinnerValues = null;
private int mSelectedIndex = -1;
private Button mPreviousButton;
private Button mNextButton;
public SideSpinner(Context context) {
super(context);
initializeViews(context);
}
public SideSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray;
typedArray = context
.obtainStyledAttributes(attrs, R.styleable.SideSpinner);
mSpinnerValues = typedArray
.getTextArray(R.styleable.SideSpinner_values);
typedArray.recycle();
initializeViews(context);
}
public SideSpinner(Context context,
AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
TypedArray typedArray;
typedArray = context
.obtainStyledAttributes(attrs, R.styleable.SideSpinner);
mSpinnerValues = typedArray
.getTextArray(R.styleable.SideSpinner_values);
typedArray.recycle();
initializeViews(context);
}
/**
* Inflates the views in the layout.
*
* @param context
* the current context for the view.
*/
private void initializeViews(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.sidespinner_view, this);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
// When the previous button is pressed, select the previous value
// in the list.
mPreviousButton = (Button) this
.findViewById(R.id.sidespinner_view_previous);
mPreviousButton
.setBackgroundResource(android.R.drawable.ic_media_previous);
mPreviousButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mSelectedIndex > 0) {
int newSelectedIndex = mSelectedIndex - 1;
setSelectedIndex(newSelectedIndex);
}
}
});
// When the next button is pressed, select the next item in the
// list.
mNextButton = (Button)this
.findViewById(R.id.sidespinner_view_next);
mNextButton
.setBackgroundResource(android.R.drawable.ic_media_next);
mNextButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (mSpinnerValues != null
&& mSelectedIndex < mSpinnerValues.length - 1) {
int newSelectedIndex = mSelectedIndex + 1;
setSelectedIndex(newSelectedIndex);
}
}
});
// Select the first value by default.
setSelectedIndex(0);
}
public void setValues(CharSequence[] values) {
mSpinnerValues = values;
// Select the first item of the string array by default since
// the list of value has changed.
setSelectedIndex(0);
}
/**
* Sets the selected index of the spinner.
*
* @param index
* the index of the value to select.
*/
public void setSelectedIndex(int index) {
// If no values are set for the spinner, do nothing.
if (mSpinnerValues == null || mSpinnerValues.length == 0)
return;
// If the index value is invalid, do nothing.
if (index < 0 || index >= mSpinnerValues.length)
return;
// Set the current index and display the value.
mSelectedIndex = index;
TextView currentValue;
currentValue = (TextView)this
.findViewById(R.id.sidespinner_view_current_value);
currentValue.setText(mSpinnerValues[index]);
// If the first value is shown, hide the previous button.
if (mSelectedIndex == 0)
mPreviousButton.setVisibility(INVISIBLE);
else
mPreviousButton.setVisibility(VISIBLE);
// If the last value is shown, hide the next button.
if (mSelectedIndex == mSpinnerValues.length - 1)
mNextButton.setVisibility(INVISIBLE);
else
mNextButton.setVisibility(VISIBLE);
}
/**
* Gets the selected value of the spinner, or null if no valid
* selected index is set yet.
*
* @return the selected value of the spinner.
*/
public CharSequence getSelectedValue() {
// If no values are set for the spinner, return an empty string.
if (mSpinnerValues == null || mSpinnerValues.length == 0)
return "";
// If the current index is invalid, return an empty string.
if (mSelectedIndex < 0 || mSelectedIndex >= mSpinnerValues.length)
return "";
return mSpinnerValues[mSelectedIndex];
}
/**
* Gets the selected index of the spinner.
*
* @return the selected index of the spinner.
*/
public int getSelectedIndex() {
return mSelectedIndex;
}
}
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/sidespinner_view_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/sidespinner_view_current_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp" />
<Button
android:id="@+id/sidespinner_view_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</merge>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment