经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android学习-列表视图ListView
来源:cnblogs  作者:YouChaoMin  时间:2018/11/28 9:45:51  对本文有异议

一、简介:

ListView,列表视图,直接继承了AbsListView,是一个以垂直方式在项目中显示View视图的列表。ListView的数据项,来自一个继承了ListAdapter接口的适配器。

二、新建一个包listview并新建ListViewActivity.java活动:

  1. 1
    2
    3
    4
    5
    6
    7
    8
  1. public class ListViewActivity extends AppCompatActivity {

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

三、在AndroidManifest.xml中声名activity:

  1. 1
  1. <activity android:name=".listview.ListViewActivity"></activity>

四、建立activity_list_view.xml布局:

  1. 1
    2
    3
    4
    5
    6
  1. <?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">

    </LinearLayout>

五、在activity_main.xml中新建一个按钮:

  1. 1
    2
    3
    4
    5
    6
  1. <Button
    android:id="@+id/btn_listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="ListView"
    android:textAllCaps="false"/>

六、在MainActivity.java中声名控件:

  1. 1
  1. private Button mBtnListView;

七、在MainActivity.java中找到控件:

  1. 1
  1. mBtnListView=findViewById(R.id.btn_listview);

八、设置点击事件:

  1. 1
    2
    3
    4
    5
    6
    7
    8
  1. mBtnListView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    //跳转到ListView演示页面
    Intent intent=new Intent(MainActivity.this,ListViewActivity.class);
    startActivity(intent);
    }
    });

九、在activity_list_view.xml布局中写代码:

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
  1. <?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">

    <ListView
    android:id="@+id/lv_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    </ListView>
    </LinearLayout>

十、新建layout_list_item.xml:

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
  1. <?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="horizontal"
    android:paddingLeft="15dp"
    android:paddingRight="15dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">

    <ImageView
    android:id="@+id/iv"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:scaleType="centerCrop"
    android:background="#000"/>

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="10dp">

    <TextView
    android:id="@+id/tv_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello"
    android:textSize="20sp"
    android:textColor="#000"/>

    <TextView
    android:id="@+id/tv_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="2018-11-27"
    android:textSize="18sp"
    android:textColor="#808080"
    android:layout_marginTop="10dp"/>

    <TextView
    android:id="@+id/tv_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="这是内容"
    android:textSize="18sp"
    android:textColor="#808080"
    android:layout_marginTop="10dp"/>

    </LinearLayout>
    </LinearLayout>

十一、在包listview中新建MyListAdapter.java继承自BaseAdapter:

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
  1. public class MyListAdapter extends BaseAdapter {

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public MyListAdapter(Context context){
    this.mContext=context;
    mLayoutInflater=LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
    return 10;
    }

    @Override
    public Object getItem(int i) {
    return null;
    }

    @Override
    public long getItemId(int i) {
    return 0;
    }

    static class ViewHolder{
    public ImageView imageView;
    public TextView tvTitle,tvTime,tvContent;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder=null;
    if(convertView==null){
    convertView=mLayoutInflater.inflate(R.layout.layout_list_item,null);
    holder=new ViewHolder();
    holder.imageView=convertView.findViewById(R.id.iv);
    holder.tvTitle=convertView.findViewById(R.id.tv_title);
    holder.tvTime=convertView.findViewById(R.id.tv_time);
    holder.tvContent=convertView.findViewById(R.id.tv_content);
    convertView.setTag(holder);
    }else{
    holder=(ViewHolder)convertView.getTag();
    }
    //给控件赋值
    holder.tvTitle.setText("这是标题");
    holder.tvTime.setText("2018-11-28");
    holder.tvContent.setText("这是内容");
    Glide.with(mContext).load("https://www.baidu.com/img/bd_logo1.png?where=super").into(holder.imageView);
    return convertView;
    }
    }

十二、在ListViewActivity.java中写代码:

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

  1. public class ListViewActivity extends AppCompatActivity {

    private ListView mLv1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    mLv1=findViewById(R.id.lv_1);
    mLv1.setAdapter(new MyListAdapter(ListViewActivity.this));
    }
    }

运行结果:
listview

十三、在drawable下新建一个list_item:

  1. 1
    2
    3
    4
    5
    6
    7
  1. <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@color/colorAccent"/>
    <item android:state_pressed="true" android:drawable="@color/colorAccent"/>
    <item android:state_focused="true" android:drawable="@color/colorAccent"/>
    <item android:drawable="@color/colorWhite"/>
    </selector>

十四、在activity_list_view.xml下配置:

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
  1. <?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">

    <ListView
    android:id="@+id/lv_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:listSelector="@drawable/list_item">

    </ListView>
    </LinearLayout>

运行截图:
listview

十五、在ListViewActivity.java设置点击和长按事件:

  1. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
  1. mLv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
    Toast.makeText(ListViewActivity.this,"点击 pos:"+position,Toast.LENGTH_SHORT).show();
    }
    });

    mLv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long l) {
    Toast.makeText(ListViewActivity.this,"长按 pos:"+position,Toast.LENGTH_SHORT).show();
    return true;//松开后不会显示点击事件
    }
    });

运行截图:
listview

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号