Adding browsable prebuilt samples for march push

Change-Id: I952db10d9c9acb4940db08a07789347ea2effe4d
This commit is contained in:
Alexander Lucas
2014-03-07 13:34:45 -08:00
parent 7cd4524c3b
commit a780ba4b15
213 changed files with 15681 additions and 93 deletions

View File

@@ -0,0 +1,244 @@
/*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.adaptertransition;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.transition.AutoTransition;
import android.transition.Scene;
import android.transition.Transition;
import android.transition.TransitionManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ListView;
/**
* Main screen for AdapterTransition sample.
*/
public class AdapterTransitionFragment extends Fragment implements Transition.TransitionListener {
/**
* Since the transition framework requires all relevant views in a view hierarchy to be marked
* with IDs, we use this ID to mark the root view.
*/
private static final int ROOT_ID = 1;
/**
* This is where we place our AdapterView (ListView / GridView).
*/
private FrameLayout mContent;
/**
* This is where we carry out the transition.
*/
private FrameLayout mCover;
/**
* This list shows our contents. It can be ListView or GridView, and we toggle between them
* using the transition framework.
*/
private AbsListView mAbsListView;
/**
* This is our contents.
*/
private MeatAdapter mAdapter;
public static AdapterTransitionFragment newInstance() {
return new AdapterTransitionFragment();
}
public AdapterTransitionFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// We use a ListView at first
mAbsListView = (AbsListView) inflater.inflate(R.layout.fragment_meat_list, container, false);
mAdapter = new MeatAdapter(inflater, R.layout.item_meat_list);
return inflater.inflate(R.layout.fragment_adapter_transition, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// Retaining references for FrameLayouts that we use later.
mContent = (FrameLayout) view.findViewById(R.id.content);
mCover = (FrameLayout) view.findViewById(R.id.cover);
// We are attaching the list to the screen here.
mAbsListView.setAdapter(mAdapter);
mContent.addView(mAbsListView);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragment_adapter_transition, menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
// We change the look of the icon every time the user toggles between list and grid.
MenuItem item = menu.findItem(R.id.action_toggle);
if (null != item) {
if (mAbsListView instanceof ListView) {
item.setIcon(R.drawable.ic_action_grid);
} else {
item.setIcon(R.drawable.ic_action_list);
}
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_toggle: {
toggle();
return true;
}
}
return false;
}
@Override
public void onTransitionStart(Transition transition) {
}
// BEGIN_INCLUDE(on_transition_end)
@Override
public void onTransitionEnd(Transition transition) {
// When the transition ends, we remove all the views from the overlay and hide it.
mCover.removeAllViews();
mCover.setVisibility(View.INVISIBLE);
}
// END_INCLUDE(on_transition_end)
@Override
public void onTransitionCancel(Transition transition) {
}
@Override
public void onTransitionPause(Transition transition) {
}
@Override
public void onTransitionResume(Transition transition) {
}
/**
* Toggle the UI between ListView and GridView.
*/
private void toggle() {
// We use mCover as the overlay on which we carry out the transition.
mCover.setVisibility(View.VISIBLE);
// This FrameLayout holds all the visible views in the current list or grid. We use this as
// the starting Scene of the Transition later.
FrameLayout before = copyVisibleViews();
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
mCover.addView(before, params);
// Swap the actual list.
swapAbsListView();
// We also swap the icon for the toggle button.
ActivityCompat.invalidateOptionsMenu(getActivity());
// It is now ready to start the transition.
mAbsListView.post(new Runnable() {
@Override
public void run() {
// BEGIN_INCLUDE(transition_with_listener)
Scene scene = new Scene(mCover, copyVisibleViews());
Transition transition = new AutoTransition();
transition.addListener(AdapterTransitionFragment.this);
TransitionManager.go(scene, transition);
// END_INCLUDE(transition_with_listener)
}
});
}
/**
* Swap ListView with GridView, or GridView with ListView.
*/
private void swapAbsListView() {
// We save the current scrolling position before removing the current list.
int first = mAbsListView.getFirstVisiblePosition();
// If the current list is a GridView, we replace it with a ListView. If it is a ListView,
// a GridView.
LayoutInflater inflater = LayoutInflater.from(getActivity());
if (mAbsListView instanceof GridView) {
mAbsListView = (AbsListView) inflater.inflate(
R.layout.fragment_meat_list, (ViewGroup) mAbsListView.getParent(), false);
mAdapter = new MeatAdapter(inflater, R.layout.item_meat_list);
} else {
mAbsListView = (AbsListView) inflater.inflate(
R.layout.fragment_meat_grid, (ViewGroup) mAbsListView.getParent(), false);
mAdapter = new MeatAdapter(inflater, R.layout.item_meat_grid);
}
mAbsListView.setAdapter(mAdapter);
// We restore the scrolling position here.
mAbsListView.setSelection(first);
// The new list is ready, and we replace the existing one with it.
mContent.removeAllViews();
mContent.addView(mAbsListView);
}
/**
* Copy all the visible views in the mAbsListView into a new FrameLayout and return it.
*
* @return a FrameLayout with all the visible views inside.
*/
private FrameLayout copyVisibleViews() {
// This is the FrameLayout we return afterwards.
FrameLayout layout = new FrameLayout(getActivity());
// The transition framework requires to set ID for all views to be animated.
layout.setId(ROOT_ID);
// We only copy visible views.
int first = mAbsListView.getFirstVisiblePosition();
int index = 0;
while (true) {
// This is one of the views that we copy. Note that the argument for getChildAt is a
// zero-oriented index, and it doesn't usually match with its position in the list.
View source = mAbsListView.getChildAt(index);
if (null == source) {
break;
}
// This is the copy of the original view.
View destination = mAdapter.getView(first + index, null, layout);
assert destination != null;
destination.setId(ROOT_ID + first + index);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
source.getWidth(), source.getHeight());
params.leftMargin = (int) source.getX();
params.topMargin = (int) source.getY();
layout.addView(destination, params);
++index;
}
return layout;
}
}

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.adaptertransition;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ViewAnimator;
import com.example.android.common.activities.SampleActivityBase;
import com.example.android.common.logger.Log;
import com.example.android.common.logger.LogFragment;
import com.example.android.common.logger.LogWrapper;
import com.example.android.common.logger.MessageOnlyLogFilter;
/**
* A simple launcher activity containing a summary sample description, sample log and a custom
* {@link android.support.v4.app.Fragment} which can display a view.
* <p>
* For devices with displays with a width of 720dp or greater, the sample log is always visible,
* on other devices it's visibility is controlled by an item on the Action Bar.
*/
public class MainActivity extends SampleActivityBase {
public static final String TAG = "MainActivity";
// Whether the Log Fragment is currently shown
private boolean mLogShown;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
AdapterTransitionFragment fragment = new AdapterTransitionFragment();
transaction.replace(R.id.sample_content_fragment, fragment);
transaction.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem logToggle = menu.findItem(R.id.menu_toggle_log);
logToggle.setVisible(findViewById(R.id.sample_output) instanceof ViewAnimator);
logToggle.setTitle(mLogShown ? R.string.sample_hide_log : R.string.sample_show_log);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_toggle_log:
mLogShown = !mLogShown;
ViewAnimator output = (ViewAnimator) findViewById(R.id.sample_output);
if (mLogShown) {
output.setDisplayedChild(1);
} else {
output.setDisplayedChild(0);
}
supportInvalidateOptionsMenu();
return true;
}
return super.onOptionsItemSelected(item);
}
/** Create a chain of targets that will receive log data */
@Override
public void initializeLogging() {
// Wraps Android's native log framework.
LogWrapper logWrapper = new LogWrapper();
// Using Log, front-end to the logging chain, emulates android.util.log method signatures.
Log.setLogNode(logWrapper);
// Filter strips out everything except the message text.
MessageOnlyLogFilter msgFilter = new MessageOnlyLogFilter();
logWrapper.setNext(msgFilter);
// On screen logging via a fragment with a TextView.
LogFragment logFragment = (LogFragment) getSupportFragmentManager()
.findFragmentById(R.id.log_fragment);
msgFilter.setNext(logFragment.getLogView());
Log.i(TAG, "Ready");
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.adaptertransition;
/**
* Sample data.
*/
public class Meat {
public int resourceId;
public String title;
public Meat(int resourceId, String title) {
this.resourceId = resourceId;
this.title = title;
}
public static final Meat[] MEATS = {
new Meat(R.drawable.p1, "First"),
new Meat(R.drawable.p2, "Second"),
new Meat(R.drawable.p3, "Third"),
new Meat(R.drawable.p4, "Fourth"),
new Meat(R.drawable.p5, "Fifth"),
new Meat(R.drawable.p6, "Sixth"),
new Meat(R.drawable.p7, "Seventh"),
new Meat(R.drawable.p8, "Eighth"),
new Meat(R.drawable.p9, "Ninth"),
new Meat(R.drawable.p10, "Tenth"),
new Meat(R.drawable.p11, "Eleventh"),
};
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.adaptertransition;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* This class provides data as Views. It is designed to support both ListView and GridView by
* changing a layout resource file to inflate.
*/
public class MeatAdapter extends BaseAdapter {
private final LayoutInflater mLayoutInflater;
private final int mResourceId;
/**
* Create a new instance of {@link MeatAdapter}.
*
* @param inflater The layout inflater.
* @param resourceId The resource ID for the layout to be used. The layout should contain an
* ImageView with ID of "meat_image" and a TextView with ID of "meat_title".
*/
public MeatAdapter(LayoutInflater inflater, int resourceId) {
mLayoutInflater = inflater;
mResourceId = resourceId;
}
@Override
public int getCount() {
return Meat.MEATS.length;
}
@Override
public Meat getItem(int position) {
return Meat.MEATS[position];
}
@Override
public long getItemId(int position) {
return Meat.MEATS[position].resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view;
final ViewHolder holder;
if (null == convertView) {
view = mLayoutInflater.inflate(mResourceId, parent, false);
holder = new ViewHolder();
assert view != null;
holder.image = (ImageView) view.findViewById(R.id.meat_image);
holder.title = (TextView) view.findViewById(R.id.meat_title);
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
}
Meat meat = getItem(position);
holder.image.setImageResource(meat.resourceId);
holder.title.setText(meat.title);
return view;
}
private static class ViewHolder {
public ImageView image;
public TextView title;
}
}