码迷,mamicode.com
首页 > 移动开发 > 详细

【Android】11.0 UI开发(三)——列表控件ListView的简单实现2

时间:2019-02-11 14:11:20      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:sha   rom   贵州   not   private   ble   roi   获取   finish   

1.0 由于书上内容,已经和实际编程的兼容性已经不太友好,重写了项目,用于进一步学习列表控件ListView。

2.0 新建项目ListViewTest,其中文件目录如下:

 技术图片

 

3.0 ActivityCollector.java

package com.example.listviewtest;

import android.app.Activity;

import java.util.ArrayList;
import java.util.List;

public class ActivityCollector {
    public static List<Activity> activities = new ArrayList<>();

    //添加活动
    public static void addActivity(Activity activity){activities.add(activity);}
    //移除活动
    public  static void removeActivity(Activity activity){activities.remove(activity);}

    //销毁所有活动

    public static void finishAll(){
        for (Activity activity:activities){
            if (!activity.isFinishing()){
                activity.finish();
            }
        }
    }
}

 

4.0 BaseActivity.java

package com.example.listviewtest;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("BaseActivity:***",getClass().getSimpleName());
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

 

5.0 Province.java

package com.example.listviewtest;

public class Province {
    private String name;
    public Province(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

 

6.0 ProvinceAdapter.java

package com.example.listviewtest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

public class ProvinceAdapter extends ArrayAdapter<Province> {
    private int resourceId;

    public ProvinceAdapter(Context context, int textViewResourceId, List<Province> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Province province = getItem(position);//获取当前的Province实例
        View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);
        TextView provinceName =(TextView) view.findViewById(R.id.province_name);
        provinceName.setText(province.getName());
        return view;
    }
}

 

7.0 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/list_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.05" />

</android.support.constraint.ConstraintLayout>

 

8.0 province_item.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/province_name"
        android:layout_width="match_parent"
        android:layout_height="32dp"
        android:textSize="18dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

 

9.0 MainActivity.java

 

package com.example.listviewtest;


import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private List<Province> provincelist = new ArrayList<>();
    private String[] data = {
            "广西壮族自治区", "内蒙古自治区", "宁夏回族自治区", "西藏藏族自治区", "新疆维吾尔自治区", "香港特别行政区", "澳门特别行政区",
            "北京市", "天津市", "上海市", "重庆市", "吉林省", "辽宁省", "黑龙江省",
            "河北省", "河南省", "安徽省", "甘肃省", "山东省",
            "湖南省", "湖北省", "江苏省", "浙江省", "江西省",
            "云南省", "广西省", "贵州省", "海南省", "台湾省",};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initProvince();
        ProvinceAdapter adapter = new ProvinceAdapter(MainActivity.this,
                R.layout.province_item, provincelist);

        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Province province = provincelist.get(position);
                Toast.makeText(MainActivity.this, province.getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void initProvince() {
        for (String dataone : data) {
            Province province = new Province(dataone);
            provincelist.add(province);
        }
//        for (int i = 0;i<data.length;i++){
//            Province  province = new Province(data[i]);
//            provincelist.add(province);
//        }
    }
}

 

10.0 运行效果如下:

 技术图片

【Android】11.0 UI开发(三)——列表控件ListView的简单实现2

标签:sha   rom   贵州   not   private   ble   roi   获取   finish   

原文地址:https://www.cnblogs.com/xiaofu007/p/10361635.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!