Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Created July 3, 2012 07:13
Show Gist options
  • Save daichan4649/3038211 to your computer and use it in GitHub Desktop.
Save daichan4649/3038211 to your computer and use it in GitHub Desktop.
縦横切替で ListView/GridView を切り替える(表示用データは毎回再生成しない)
package test.fragment.configchange;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
/**
* 縦横切替で ListView/GridView を切り替えるサンプル
* (表示用データは毎回再生成しない)
*
* @author daichan4649
*/
public class ConfigChangeTestActivity extends Activity {
private static final String TAG_LIST = "list";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fragment fragment = getFragmentManager().findFragmentByTag(TAG_LIST);
if (fragment == null) {
fragment = ConfigChangeTestFragment.newInstance();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(android.R.id.content, fragment, TAG_LIST);
ft.commit();
}
}
}
package test.fragment.configchange;
import java.util.ArrayList;
import java.util.List;
import test.fragment.R;
import android.app.Fragment;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class ConfigChangeTestFragment extends Fragment {
public static ConfigChangeTestFragment newInstance() {
return new ConfigChangeTestFragment();
}
private ArrayAdapter<String> adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// fragment再生成抑止
setRetainInstance(true);
// adapter
adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_column, R.id.text);
adapter.addAll(createDataList(100));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// toast
showOrientaionToast(getActivity());
// list setting
View parent = inflater.inflate(R.layout.list, container, false);
AbsListView list = (AbsListView) parent.findViewById(R.id.list);
list.setAdapter(adapter);
return parent;
}
private static List<String> createDataList(int counts) {
List<String> list = new ArrayList<String>();
for (int i = 0; i < counts; i++) {
list.add("i=" + i);
}
return list;
}
private static void showOrientaionToast(Context context) {
String text = (isPortrait(context) ? "portrait" : "landscape");
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
private static boolean isPortrait(Context context) {
int orientation = context.getResources().getConfiguration().orientation;
return (orientation == Configuration.ORIENTATION_PORTRAIT);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2" >
</GridView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icon"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_gravity="center"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="text" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment