class MyAdapter extends BaseAdapter {
private Context context;
private LayoutInflater layoutInflater;
public MyAdapter(Context context) {
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return 8;
}
@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, tvDate, tvContext;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = layoutInflater.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.tvDate = convertView.findViewById(R.id.tv_date);
holder.tvContext = convertView.findViewById(R.id.tv_context);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//给控件赋值
holder.tvTitle.setText("高山");
holder.tvDate.setText("1984-01-01");
holder.tvContext.setText("喜马拉雅");
String picURL = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=100529321,758151142&fm=26&gp=0.jpg";
Glide.with(context).load(picURL).disallowHardwareConfig().into(holder.imageView);
return convertView;
}
}
Activity:
public class ListViewActivity extends Activity {
private ListView lv_1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
lv_1 = findViewById(R.id.lv_1);
lv_1.setAdapter(new MyAdapter(this));
}
布局:
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lv_1"
android:background="@drawable/selector_lv"/>
样式:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/colorGray" />
<item android:state_focused="true" android:drawable="@color/colorGray" />
<item android:state_selected="true" android:drawable="@color/colorGray" />
<item android:drawable="@color/colorBlack"/>
</selector>
效果:
这个问题怎么解决,萌新刚写Android,求大佬指导指导