Updated browseable samples for april push
Change-Id: Idd8cc6b7c43ab93f05f0a5d69d5379631721d185
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* 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.fragmenttransition;
|
||||
|
||||
import com.example.android.common.logger.Log;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.transition.Scene;
|
||||
import android.transition.TransitionManager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.AnimationUtils;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
public class DetailFragment extends Fragment implements Animation.AnimationListener {
|
||||
|
||||
private static final String TAG = "DetailFragment";
|
||||
|
||||
private static final String ARG_RESOURCE_ID = "resource_id";
|
||||
private static final String ARG_TITLE = "title";
|
||||
private static final String ARG_X = "x";
|
||||
private static final String ARG_Y = "y";
|
||||
private static final String ARG_WIDTH = "width";
|
||||
private static final String ARG_HEIGHT = "height";
|
||||
|
||||
/**
|
||||
* Create a new instance of DetailFragment.
|
||||
*
|
||||
* @param resourceId The resource ID of the Drawable image to show
|
||||
* @param title The title of the image
|
||||
* @param x The horizontal position of the grid item in pixel
|
||||
* @param y The vertical position of the grid item in pixel
|
||||
* @param width The width of the grid item in pixel
|
||||
* @param height The height of the grid item in pixel
|
||||
* @return a new instance of DetailFragment
|
||||
*/
|
||||
public static DetailFragment newInstance(int resourceId, String title,
|
||||
int x, int y, int width, int height) {
|
||||
DetailFragment fragment = new DetailFragment();
|
||||
Bundle args = new Bundle();
|
||||
args.putInt(ARG_RESOURCE_ID, resourceId);
|
||||
args.putString(ARG_TITLE, title);
|
||||
args.putInt(ARG_X, x);
|
||||
args.putInt(ARG_Y, y);
|
||||
args.putInt(ARG_WIDTH, width);
|
||||
args.putInt(ARG_HEIGHT, height);
|
||||
fragment.setArguments(args);
|
||||
return fragment;
|
||||
}
|
||||
|
||||
public DetailFragment() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_detail, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
FrameLayout root = (FrameLayout) view;
|
||||
Context context = view.getContext();
|
||||
assert context != null;
|
||||
// This is how the fragment looks at first. Since the transition is one-way, we don't need to make
|
||||
// this a Scene.
|
||||
View item = LayoutInflater.from(context).inflate(R.layout.item_meat_grid, root, false);
|
||||
assert item != null;
|
||||
bind(item);
|
||||
// We adjust the position of the initial image with LayoutParams using the values supplied
|
||||
// as the fragment arguments.
|
||||
Bundle args = getArguments();
|
||||
FrameLayout.LayoutParams params = null;
|
||||
if (args != null) {
|
||||
params = new FrameLayout.LayoutParams(
|
||||
args.getInt(ARG_WIDTH), args.getInt(ARG_HEIGHT));
|
||||
params.topMargin = args.getInt(ARG_Y);
|
||||
params.leftMargin = args.getInt(ARG_X);
|
||||
}
|
||||
root.addView(item, params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
/**
|
||||
* Bind the views inside of parent with the fragment arguments.
|
||||
*
|
||||
* @param parent The parent of views to bind.
|
||||
*/
|
||||
private void bind(View parent) {
|
||||
Bundle args = getArguments();
|
||||
if (args == null) {
|
||||
return;
|
||||
}
|
||||
ImageView image = (ImageView) parent.findViewById(R.id.image);
|
||||
image.setImageResource(args.getInt(ARG_RESOURCE_ID));
|
||||
TextView title = (TextView) parent.findViewById(R.id.title);
|
||||
title.setText(args.getString(ARG_TITLE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
|
||||
Animation animation = AnimationUtils.loadAnimation(getActivity(),
|
||||
enter ? android.R.anim.fade_in : android.R.anim.fade_out);
|
||||
// We bind a listener for the fragment transaction. We only bind it when
|
||||
// this fragment is entering.
|
||||
if (animation != null && enter) {
|
||||
animation.setAnimationListener(this);
|
||||
}
|
||||
return animation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationStart(Animation animation) {
|
||||
// This method is called at the end of the animation for the fragment transaction.
|
||||
// There is nothing we need to do in this sample.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationEnd(Animation animation) {
|
||||
// This method is called at the end of the animation for the fragment transaction,
|
||||
// which is perfect time to start our Transition.
|
||||
Log.i(TAG, "Fragment animation ended. Starting a Transition.");
|
||||
final Scene scene = Scene.getSceneForLayout((ViewGroup) getView(),
|
||||
R.layout.fragment_detail_content, getActivity());
|
||||
TransitionManager.go(scene);
|
||||
// Note that we need to bind views with data after we call TransitionManager.go().
|
||||
bind(scene.getSceneRoot());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAnimationRepeat(Animation animation) {
|
||||
// This method is never called in this sample because the animation doesn't repeat.
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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.fragmenttransition;
|
||||
|
||||
import com.example.android.common.logger.Log;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.view.animation.Animation;
|
||||
import android.view.animation.AnimationUtils;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.GridView;
|
||||
|
||||
public class FragmentTransitionFragment extends Fragment implements AdapterView.OnItemClickListener {
|
||||
|
||||
private static final String TAG = "FragmentTransitionFragment";
|
||||
|
||||
private MeatAdapter mAdapter;
|
||||
|
||||
public static FragmentTransitionFragment newInstance() {
|
||||
return new FragmentTransitionFragment();
|
||||
}
|
||||
|
||||
public FragmentTransitionFragment() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
// This is the adapter we use to populate the grid.
|
||||
mAdapter = new MeatAdapter(inflater, R.layout.item_meat_grid);
|
||||
// Inflate the layout with a GridView in it.
|
||||
return inflater.inflate(R.layout.fragment_fragment_transition, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||
GridView grid = (GridView) view.findViewById(R.id.grid);
|
||||
grid.setAdapter(mAdapter);
|
||||
grid.setOnItemClickListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
Meat meat = mAdapter.getItem(position);
|
||||
Log.i(TAG, meat.title + " clicked. Replacing fragment.");
|
||||
// We start the fragment transaction here. It is just an ordinary fragment transaction.
|
||||
getActivity().getSupportFragmentManager()
|
||||
.beginTransaction()
|
||||
.replace(R.id.sample_content_fragment,
|
||||
DetailFragment.newInstance(meat.resourceId, meat.title,
|
||||
(int) view.getX(), (int) view.getY(),
|
||||
view.getWidth(), view.getHeight())
|
||||
)
|
||||
// We push the fragment transaction to back stack. User can go back to the
|
||||
// previous fragment by pressing back button.
|
||||
.addToBackStack("detail")
|
||||
.commit();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
|
||||
return AnimationUtils.loadAnimation(getActivity(),
|
||||
enter ? android.R.anim.fade_in : android.R.anim.fade_out);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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.fragmenttransition;
|
||||
|
||||
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();
|
||||
FragmentTransitionFragment fragment = new FragmentTransitionFragment();
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -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.fragmenttransition;
|
||||
|
||||
/**
|
||||
* This represents a 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"),
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* 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.fragmenttransition;
|
||||
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
class MeatAdapter extends BaseAdapter {
|
||||
|
||||
private final LayoutInflater mLayoutInflater;
|
||||
private final int mResourceId;
|
||||
|
||||
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.image);
|
||||
holder.title = (TextView) view.findViewById(R.id.title);
|
||||
view.setTag(holder);
|
||||
} else {
|
||||
view = convertView;
|
||||
holder = (ViewHolder) view.getTag();
|
||||
}
|
||||
bindView(holder, position);
|
||||
return view;
|
||||
}
|
||||
|
||||
public void bindView(ViewHolder holder, int position) {
|
||||
Meat meat = getItem(position);
|
||||
holder.image.setImageResource(meat.resourceId);
|
||||
holder.title.setText(meat.title);
|
||||
}
|
||||
|
||||
public static class ViewHolder {
|
||||
public ImageView image;
|
||||
public TextView title;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user