Created
June 18, 2012 06:22
-
-
Save daichan4649/2947119 to your computer and use it in GitHub Desktop.
ListFragment sample
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="vertical" > | |
<TextView | |
android:id="@+id/text" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="text" /> | |
</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 test.fragment.list; | |
import android.app.Activity; | |
import android.app.Fragment; | |
import android.app.FragmentTransaction; | |
import android.os.Bundle; | |
public class ListTestActivity 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 = TestListFragment.newInstance(); | |
FragmentTransaction ft = getFragmentManager().beginTransaction(); | |
ft.add(android.R.id.content, fragment, TAG_LIST); | |
ft.commit(); | |
} | |
} | |
} |
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 test.fragment.list; | |
import java.util.ArrayList; | |
import java.util.List; | |
import test.fragment.R; | |
import android.app.ListFragment; | |
import android.os.Bundle; | |
import android.widget.ArrayAdapter; | |
public class TestListFragment extends ListFragment { | |
public static TestListFragment newInstance() { | |
return new TestListFragment(); | |
} | |
@Override | |
public void onActivityCreated(Bundle savedInstanceState) { | |
super.onActivityCreated(savedInstanceState); | |
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.list_column, R.id.text); | |
setListAdapter(adapter); | |
adapter.addAll(createDataList(100)); | |
} | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fucking shit. This is bad, very fucking bad example