1.继承ListFragment类, 实现方法getView方法
package com.lance.sample;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
public class UserListFragment extends ListFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<User> mUsers = UserDB.get().getUsers();
UserAdapter adapter = new UserAdapter(mUsers);
setListAdapter(adapter);
}
/**
* Listener List item click event
*/
public void onListItemClick(ListView l, View v, int position, long id) {
User user = (User)getListAdapter().getItem(position);
Intent intent = new Intent(getActivity(), UserDetailActivity.class);
intent.putExtra(UserDetailFragment.USER_ID, user.getId());
startActivity(intent);
}
/**
* rewrite getView method
* @author lance
*/
private class UserAdapter extends ArrayAdapter<User>{
public UserAdapter(ArrayList<User> mUsers) {
super(getActivity(), 0, mUsers);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.activity_list_item, null);
}
User user = getItem(position);
CheckBox vipBox = (CheckBox)convertView.findViewById(R.id.list_item_vip);
vipBox.setChecked(user.isVip());
TextView nameText = (TextView)convertView.findViewById(R.id.list_item_name);
nameText.setText(user.getName());
TextView cardNoText = (TextView)convertView.findViewById(R.id.list_item_cardno);
cardNoText.setText(String.valueOf(user.getCardNo()));
return convertView;
}
}
}
2.定义显示view文件activity_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="4dp">
<CheckBox
android:id="@+id/list_item_vip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="6dp"
android:layout_marginTop="4dp"
android:text="@string/list_item_vip"
android:focusable="false"/>
<TextView
android:id="@+id/list_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:textStyle="bold"
android:layout_toLeftOf="@id/list_item_vip"
android:text="@string/list_item_name" />
<TextView
android:id="@+id/list_item_cardno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/list_item_name"
android:layout_toLeftOf="@id/list_item_vip"
android:padding="4dp"
android:text="@string/list_item_name" />
</RelativeLayout>
3.fragment托管给activity
package com.lance.sample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
/**
* 提供基本FragmentActivity, 供list列表和detail用
* @author lance
*/
public abstract class BaseFragmentActivity extends FragmentActivity {
/**
* create fragment
* @return
*/
protected abstract Fragment createFrag();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.frag_container);
if(fragment == null) {
fragment = createFrag();
fm.beginTransaction()
.add(R.id.frag_container, fragment)
.commit();
}
}
}
效果图
附件完整项目