上次我们说到ActionBar的适配器
mActionBar.setListNavigationCallbacks(mAdapter, this);
使用上述代码关联,而mAdapter的适配器是单独的一个私有类的实例
private ClusterAdapter mAdapter = new ClusterAdapter();
private class ClusterAdapter extends BaseAdapter {
@Override
public int getCount() {
return sClusterItems.length;
}
@Override
public Object getItem(int position) {
return sClusterItems[position];
}
@Override
public long getItemId(int position) {
return sClusterItems[position].action;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.action_bar_text,
parent, false);
}
TextView view = (TextView) convertView;
view.setText(sClusterItems[position].spinnerTitle);
return convertView;
}
}
类中主要使用5个菜单项目来组织。
而只有getView的时候,使用的是action_bar_text的layout.
那么这个GalleryActionBar,并没有直接继承,而是将ActionBar做一个成员变量放入类中,自己仅仅实现一个接口 OnNavigationListerer
public class GalleryActionBar implements OnNavigationListener {
@SuppressWarnings("unused")
private static final String TAG = "GalleryActionBar";
private ClusterRunner mClusterRunner; --用来回调接口doCluster的成员变量
private CharSequence[] mTitles;
private ArrayList<Integer> mActions;
private Context mContext;
private LayoutInflater mInflater;
private AbstractGalleryActivity mActivity;
private ActionBar mActionBar; ---实际的上层基础实现
private int mCurrentIndex;
private ClusterAdapter mAdapter = new ClusterAdapter(); --相册集使用的适配器(时间 地点 人物 标签)
private AlbumModeAdapter mAlbumModeAdapter; -- 相册使用的适配器(幻灯片视图 网格视图)
private OnAlbumModeSelectedListener mAlbumModeListener;
private int mLastAlbumModeSelected;
private CharSequence [] mAlbumModes;
public static final int ALBUM_FILMSTRIP_MODE_SELECTED = 0;
public static final int ALBUM_GRID_MODE_SELECTED = 1;
public interface ClusterRunner { --后面Page要实现的接口
public void doCluster(int id);
}
public interface OnAlbumModeSelectedListener { --后面Page要实现的接口
public void onAlbumModeSelected(int mode);
}
相册集页面的调用
@Override
public void doCluster(int clusterType) {
String basePath = mMediaSet.getPath().toString();
String newPath = FilterUtils.switchClusterPath(basePath, clusterType);
Bundle data = new Bundle(getData());
data.putString(AlbumSetPage.KEY_MEDIA_PATH, newPath);
data.putInt(KEY_SELECTED_CLUSTER_TYPE, clusterType);
mActivity.getStateManager().switchState(this, AlbumSetPage.class, data);
}
相册页面的调用
@Override
public void doCluster(int clusterType) {
String basePath = mMediaSet.getPath().toString();
String newPath = FilterUtils.newClusterPath(basePath, clusterType);
Bundle data = new Bundle(getData());
data.putString(AlbumSetPage.KEY_MEDIA_PATH, newPath);
if (mShowClusterMenu) {
Context context = mActivity.getAndroidContext();
data.putString(AlbumSetPage.KEY_SET_TITLE, mMediaSet.getName());
data.putString(AlbumSetPage.KEY_SET_SUBTITLE,
GalleryActionBar.getClusterByTypeString(context, clusterType));
}
// mAlbumView.savePositions(PositionRepository.getInstance(mActivity));
mActivity.getStateManager().startStateForResult(
AlbumSetPage.class, REQUEST_DO_ANIMATION, data);
}
而客户点击的是ActionBar的菜单,必须要实现菜单点击的事件,经常使用ActionBar的同志们应该都是熟悉的onNavigationItemSelected,
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (itemPosition != mCurrentIndex && mClusterRunner != null
|| mAlbumModeListener != null) {
// Need to lock rendering when operations invoked by system UI (main thread) are
// modifying slot data used in GL thread for rendering.
mActivity.getGLRoot().lockRenderThread();
try {
if (mAlbumModeListener != null) {
mAlbumModeListener.onAlbumModeSelected(itemPosition); --相册的时候不同菜单的实现
} else {
mClusterRunner.doCluster(sClusterItems[itemPosition].action); ---相册集的时候不同菜单的实现
}
} finally {
mActivity.getGLRoot().unlockRenderThread();
}
}
return false;
}
为什么是这个呢,因为我们实现了这个系统接口,而系统在实现ActionBar的时候会回调回来
GalleryActionBar implements OnNavigationListener
那么ActionBar左边的东西在两种状态下的实现我们都清楚了,下次讲讲右边的内容如何实现的。