• Android Studio [ListView]


    ListViewActivity.java

    
    
    package com.xdw.a122.listview;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ListView;
    import android.widget.Toast;
    
    import com.xdw.a122.R;
    
    public class ListViewActivity extends Activity {
        private ListView mLv1;
        @Override
        protected void onCreate( Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_listview);
            mLv1 = findViewById(R.id.lv_1);
            mLv1.setAdapter(new MyListAdapter(ListViewActivity.this));
            mLv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(ListViewActivity.this, "点击 pos:" + position, Toast.LENGTH_LONG).show();
                }
            });
            mLv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(ListViewActivity.this, "长按 pos:" + position, Toast.LENGTH_LONG).show();
                    return true;
                }
            });
        }
    }
    
    
    
     

    MyListAdapter.java

    package com.xdw.a122.listview;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import com.bumptech.glide.Glide;
    import com.xdw.a122.R;
    
    public class MyListAdapter extends BaseAdapter {
        private Context mContext;
        private LayoutInflater mLayoutInflater;
    
        MyListAdapter(Context context){
            this.mContext=context;
            mLayoutInflater=LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            return 10;
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            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("2088-08-08");
            holder.tvContent.setText("这是内容");
            Glide.with(mContext).load("https://www.baidu.com/img/bd_logo1.png?where=super").into(holder.imageView);
            return convertView;
        }
    }

    layout_list_item.xml

    <?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="2019.4.14"
                android:textSize="20sp"
                android:textColor="#000"
                />
            <TextView
                android:id="@+id/tv_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="内容"
                android:textSize="20sp"
                android:textColor="#000"/>
        </LinearLayout>
    </LinearLayout>

    activity_listview.xml

    <?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="@+id/lv_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:listSelector="@drawable/list_item"></ListView>
    </LinearLayout>

    结果:

  • 相关阅读:
    codevs2574 波兰表达式
    绿书模拟day10 单词前缀
    codevs2171 棋盘覆盖
    noip2008 双栈排序
    图论总结复习
    noip2010 关押罪犯
    flask使用geventwebsocket完成小型聊天系统
    MongoDB
    flask基础内容总览
    flask蓝图,cbv,python中的redis操作
  • 原文地址:https://www.cnblogs.com/zlc364624/p/10707266.html
Copyright © 2020-2023  润新知