Android Fragment ArrayAdapter ListView 自定义列表显示

Android Fragment ArrayAdapter ListView 自定义列表显示

package cn.mf.cts;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

/**
 * CopyRright (c)2018-2028: chanpinxue.cn
 * Project: cts
 * Module Name: Index
 * Comments: 首页
 * JDK version used: JDK1.8
 * Author: jzh
 * Create Date: 2018-09-17
 * Modified By: jzh
 * Modified Date: 2018-09-17
 * Why & What is modified:
 * Version: <1.0>
 */
public class IndexFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_index, null, false);

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);

        // 基础数据
        String[] arr = {"A","B","C","D","E"};
        // 创建ArrayAdapter
        ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_expandable_list_item_1, arr);
        // 获取ListView对象,通过调用setAdapter方法为ListView设置Adapter设置适配器
        final ListView list = new ListView(getContext());
        list.setAdapter(adapter);

        // import android.widget.AdapterView.OnItemClickListener;
        list.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                                    long id) {
                String text = list.getItemAtPosition(position).toString();
                System.out.println("选择" +text);
            }
        });

        return view;
    }
}

 

发表回复

您的电子邮箱地址不会被公开。