ListFragment

MyListView

import java.util.ArrayList;
import java.util.List;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MyListFragment extends ListFragment {

    private Listlist = new ArrayList();
    private ArrayAdapter adapter;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        for (int i = 0; i < 30; i++) {
            list.add("item"+i);
        }
        
        adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, list);
        setListAdapter(adapter);
//      getListView().setOnItemClickListener(listener);
//      getListView().setOnItemLongClickListener(listener);
        return inflater.inflate(R.layout.listfragment, container, false);
    }
    
    //ListView item的点击事件
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Toast.makeText(getActivity(), position+"", Toast.LENGTH_SHORT).show();
    }
}

MainActivity

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
/**
 * Fragment子类
 * 列表:ListFragment
 * 对话框:DialogFragment
 * 选项设置:PreferenceFragment
 * WebView界面:WebViewFragment
 * 
 * ListFragment继承与Fragment 特点:内容以列表的形式显示的 布局中默认包含了一个ListView
 * 必须指定一个AndroidL:id 为“@id/android:list”或者@android:id/list
 * @author Administrator
 *
 */
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @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;
    }    
}

activity_main.xml布局:



   

list_fragment



    
           
    

你可能感兴趣的:(ListFragment)