美文网首页
ListFragment

ListFragment

作者: Mr_LJ | 来源:发表于2016-07-12 12:39 被阅读52次

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 List<String>list = new ArrayList<String>();
    private ArrayAdapter<String> 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<String>(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布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   <fragment 
       android:id="@+id/listFragment"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:name="com.qianfeng.day16_listfragment.MyListFragment"/>
</RelativeLayout>

list_fragment

<?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" >
    
    <ListView 
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent">       
    </ListView>
</LinearLayout>

相关文章

网友评论

      本文标题:ListFragment

      本文链接:https://www.haomeiwen.com/subject/wlzjjttx.html