android开发程序之路(列表视图 ListView和GridView使用)
程序员文章站
2022-03-11 23:05:14
小提示Listview实用性不高,了解即可Ctrl+Alt+L 格式化代码Alt+Enter 打开提示(需要复写的方法等)...
文章目录
小提示
Listview GridView实用性不高,了解即可。
Ctrl+Alt+L 格式化代码
Alt+Enter 打开提示(需要复写的方法等)
ListView
闪退的可能原因
检查MyListAdapter中的代码
1.检查getview()的return是否是null,如果是改成view或convertview(看你那个函数中的参数是什么)
2.检查getcount()return是否是0,如果是改成10
3.activity都需要在AndroidMainfest.xml中注册
MyListAdapter
package com.example.myapplication.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.example.myapplication.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 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 i, View view, ViewGroup viewGroup) {
ViewHolder holder = null;
if (view == null) {
view = mLayoutInflater.inflate(R.layout.layout_list_item, null);
holder=new ViewHolder();
holder.imageView=view.findViewById(R.id.iv);
holder.tvTitle=view.findViewById(R.id.tv_title2);
//这里本来是tv_title 但是有重名所以会出错!
holder.tvTime=view.findViewById(R.id.tv_time);
holder.tvContent=view.findViewById(R.id.tv_content);
//注意要把所有属性赋值
view.setTag(holder);
}
else{
holder= (ViewHolder) view.getTag();
}
//给控件赋值
holder.tvTitle.setText("如果真爱有颜色");
holder.tvTime.setText("那一定是");
holder.tvContent.setText("蓝色");
Glide.with(mCOntext).load("https://i0.hdslb.com/bfs/article/1f2d43c9129520f5e595ab7d5b7eae8fe64b009d.jpg@1320w_778h.webp").into(holder.imageView);
return view;
}
}
ListViewActivity
package com.example.myapplication.Listview;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import androidx.annotation.Nullable;
import com.example.myapplication.R;
public class ListViewActivity extends Activity {
private ListView mLv1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
mLv1=findViewById(R.id.lv_1);
mLv1.setAdapter(new MyListAdapter(ListViewActivity.this));
}
}
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" />
</LinearLayout>
layout_list_item.xml
注意这里对元素的赋值都可以在MyListAdapter中被覆盖
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingTop="20dp"
android:padding="20dp">
<ImageView
android:id="@+id/iv"
android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:background="#117CE6" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="20sp">
<TextView
android:id="@+id/tv_title2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:textSize="25sp"
android:textColor="@color/colorBlack"/>
<!--这里和之前那个文字赋值方法一样,可以先在colors.xml文件中定义颜色,然后直接引用 -->
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="2020-7-28"
android:textColor="#000000"
android:layout_marginTop="10dp"/>
<TextView
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小方哥哥最帅"
android:textSize="20sp"
android:textColor="#2196F3"
android:layout_marginTop="10dp"/>
</LinearLayout>
</LinearLayout>
效果图
GridView
这里增加了点击事件
GridViewActivity
package com.example.myapplication.Gridview;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.R;
public class GridViewActivity extends AppCompatActivity {
private GridView mGv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
mGv=findViewById(R.id.gv);
mGv.setAdapter(new MyGridViewAdapter(GridViewActivity.this));
//加个点击事件
mGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(GridViewActivity.this,"你在点击蕾姆"+i+"号",Toast.LENGTH_SHORT).show();
}
});
mGv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(GridViewActivity.this,"你在长按蕾姆"+i+"号",Toast.LENGTH_SHORT).show();
return true;//return false的话会再执行点击事件,return true的话则表示已处理完这个事件
}
});
}
}
MyGridViewAdapter
package com.example.myapplication.Gridview;
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.example.myapplication.R;
public class MyGridViewAdapter extends BaseAdapter {
private Context mcontext;
private LayoutInflater mLayoutinflater;
public MyGridViewAdapter(Context context){
this.mcontext=context;
mLayoutinflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return 20;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
static class ViewHold{
public ImageView imageView;
public TextView textView;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHold holder=null;
if (view==null){
view=mLayoutinflater.inflate(R.layout.layout_grid_item,null);
holder=new ViewHold();
holder.imageView=view.findViewById(R.id.iv_grid);
holder.textView=view.findViewById(R.id.tv_title3);
view.setTag(holder);
}
else{
holder=(ViewHold) view.getTag();
}
//赋值
holder.textView.setText("蕾姆!");
Glide.with(mcontext).load("https://tiebapic.baidu.com/forum/w%3D580%3B/sign=62e9df9de036afc30e0c3f6d8322eac4/b999a9014c086e067c33ad1615087bf40bd1cbd0.jpg").into(holder.imageView);
return view;
}
}
layout_grid_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="vertical">
<ImageView
android:id="@+id/iv_grid"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:background="@color/colorBlack"/>
<TextView
android:id="@+id/tv_title3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello"
android:gravity="center"
android:textColor="#03A9F4"
android:textSize="30sp"
android:layout_marginTop="10dp"/>
</LinearLayout>
activity_gridview.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"
android:padding="15dp">
<GridView
android:id="@+id/gv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:horizontalSpacing="20dp"
android:verticalSpacing="20dp"/>
<!--numColumns 列数
spacing 间距 >-->
</LinearLayout>
效果图
本文地址:https://blog.csdn.net/weixin_45941945/article/details/107702031