Updated browseable samples for april push
Change-Id: Idd8cc6b7c43ab93f05f0a5d69d5379631721d185
This commit is contained in:
@@ -33,6 +33,7 @@ import android.widget.AbsListView;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.GridView;
|
||||
import android.widget.ListView;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* Main screen for AdapterTransition sample.
|
||||
@@ -45,6 +46,11 @@ public class AdapterTransitionFragment extends Fragment implements Transition.Tr
|
||||
*/
|
||||
private static final int ROOT_ID = 1;
|
||||
|
||||
/**
|
||||
* A tag for saving state whether the mAbsListView is ListView or GridView.
|
||||
*/
|
||||
private static final String STATE_IS_LISTVIEW = "is_listview";
|
||||
|
||||
/**
|
||||
* This is where we place our AdapterView (ListView / GridView).
|
||||
*/
|
||||
@@ -80,20 +86,32 @@ public class AdapterTransitionFragment extends Fragment implements Transition.Tr
|
||||
}
|
||||
|
||||
@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);
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
// If savedInstanceState is available, we restore the state whether the list is a ListView
|
||||
// or a GridView.
|
||||
boolean isListView;
|
||||
if (null == savedInstanceState) {
|
||||
isListView = true;
|
||||
} else {
|
||||
isListView = savedInstanceState.getBoolean(STATE_IS_LISTVIEW, true);
|
||||
}
|
||||
inflateAbsList(inflater, container, isListView);
|
||||
return inflater.inflate(R.layout.fragment_adapter_transition, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
outState.putBoolean(STATE_IS_LISTVIEW, mAbsListView instanceof ListView);
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@@ -109,8 +127,10 @@ public class AdapterTransitionFragment extends Fragment implements Transition.Tr
|
||||
if (null != item) {
|
||||
if (mAbsListView instanceof ListView) {
|
||||
item.setIcon(R.drawable.ic_action_grid);
|
||||
item.setTitle(R.string.show_as_grid);
|
||||
} else {
|
||||
item.setIcon(R.drawable.ic_action_list);
|
||||
item.setTitle(R.string.show_as_list);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -151,6 +171,29 @@ public class AdapterTransitionFragment extends Fragment implements Transition.Tr
|
||||
public void onTransitionResume(Transition transition) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Inflate a ListView or a GridView with a corresponding ListAdapter.
|
||||
*
|
||||
* @param inflater The LayoutInflater.
|
||||
* @param container The ViewGroup that contains this AbsListView. The AbsListView won't be
|
||||
* attached to it.
|
||||
* @param inflateListView Pass true to inflate a ListView, or false to inflate a GridView.
|
||||
*/
|
||||
private void inflateAbsList(LayoutInflater inflater, ViewGroup container,
|
||||
boolean inflateListView) {
|
||||
if (inflateListView) {
|
||||
mAbsListView = (AbsListView) inflater.inflate(R.layout.fragment_meat_list,
|
||||
container, false);
|
||||
mAdapter = new MeatAdapter(inflater, R.layout.item_meat_list);
|
||||
} else {
|
||||
mAbsListView = (AbsListView) inflater.inflate(R.layout.fragment_meat_grid,
|
||||
container, false);
|
||||
mAdapter = new MeatAdapter(inflater, R.layout.item_meat_grid);
|
||||
}
|
||||
mAbsListView.setAdapter(mAdapter);
|
||||
mAbsListView.setOnItemClickListener(mAdapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle the UI between ListView and GridView.
|
||||
*/
|
||||
@@ -190,15 +233,8 @@ public class AdapterTransitionFragment extends Fragment implements Transition.Tr
|
||||
// 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);
|
||||
}
|
||||
inflateAbsList(inflater, (ViewGroup) mAbsListView.getParent(),
|
||||
mAbsListView instanceof GridView);
|
||||
mAbsListView.setAdapter(mAdapter);
|
||||
// We restore the scrolling position here.
|
||||
mAbsListView.setSelection(first);
|
||||
|
||||
@@ -16,18 +16,22 @@
|
||||
|
||||
package com.example.android.adaptertransition;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.AbsListView;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
public class MeatAdapter extends BaseAdapter implements AbsListView.OnItemClickListener {
|
||||
|
||||
private final LayoutInflater mLayoutInflater;
|
||||
private final int mResourceId;
|
||||
@@ -80,6 +84,16 @@ public class MeatAdapter extends BaseAdapter {
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
ViewHolder holder = (ViewHolder) view.getTag();
|
||||
Context context = view.getContext();
|
||||
if (null != holder && null != holder.title && null != context) {
|
||||
Toast.makeText(context, context.getString(R.string.item_clicked,
|
||||
holder.title.getText()), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
private static class ViewHolder {
|
||||
public ImageView image;
|
||||
public TextView title;
|
||||
|
||||
Reference in New Issue
Block a user