Created
August 30, 2013 16:28
-
-
Save TomckySan/6391689 to your computer and use it in GitHub Desktop.
ListFragmentを使ったチェックボックス付きアイテムリスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.customlist; | |
import java.util.List; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.TextView; | |
/** | |
* ArrayAdapterクラスの継承 | |
*/ | |
public class CustomAdapter extends ArrayAdapter<CustomData> { | |
// LayoutInflaterはレイアウトxmlファイルからIDを指定して | |
// Viewが使えちゃう仕組み | |
private LayoutInflater mLayoutInflater; | |
public CustomAdapter(Context context, int resourceId, List<CustomData> objects) { | |
super(context, resourceId, objects); | |
// getLayoutInflater()メソッドはActivityじゃないと使えない | |
mLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
} | |
// getView()メソッドは各行を表示しようとした時に呼ばれる | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
// 特定行(position)のデータを得る | |
CustomData item = (CustomData)getItem(position); | |
// convertViewは使いまわされている可能性があるのでnullの時だけ新しく作る | |
if (null == convertView) convertView = mLayoutInflater.inflate(R.layout.list_item, null); | |
// CustomDataのデータをViewの各Widgetにセットする | |
TextView textView = (TextView)convertView.findViewById(R.id.check_box); | |
textView.setText(item.getTextData()); | |
return convertView; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.customlist; | |
/** | |
* 各行に表示させるデータ | |
*/ | |
class CustomData { | |
private String mTextData; | |
public void setTextData(String text) { | |
mTextData = text; | |
} | |
public String getTextData() { | |
return mTextData; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.customlist; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.os.Bundle; | |
import android.support.v4.app.ListFragment; | |
public class CustomListFragment extends ListFragment { | |
/** | |
* 所有元のActivityのonCreate終了を知らせる | |
*/ | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
// データの作成 | |
List<CustomData> objects = new ArrayList<CustomData>(); | |
CustomData item1 = new CustomData(); | |
item1.setTextData("ひとつめ"); | |
CustomData item2 = new CustomData(); | |
item2.setTextData("2つめ"); | |
CustomData item3 = new CustomData(); | |
item3.setTextData("さーん"); | |
objects.add(item1); | |
objects.add(item2); | |
objects.add(item3); | |
CustomAdapter customAdapter = new CustomAdapter(getActivity(), 0, objects); | |
setListAdapter(customAdapter); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> | |
<fragment | |
android:id="@+id/list_fragment" | |
class="com.example.customlist.CustomListFragment" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"> | |
<CheckBox | |
android:id="@+id/check_box" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.customlist; | |
import android.os.Bundle; | |
import android.support.v4.app.FragmentActivity; | |
import android.view.Menu; | |
public class MainActivity extends FragmentActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.list); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.main, menu); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment