1.ListView
列表视图,用于展示数据列表,比如帖子列表,商品列表,一般和Adapter一起使用
例子是一个简单的ListView
package com.example.chapter08;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.chapter08.entity.Planet;
import java.util.List;
public class ListViewActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, CompoundButton.OnCheckedChangeListener {
private List<Planet> list;
private ListView lv;
private CheckBox cb_divider;
private CheckBox cb_selector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
lv = findViewById(R.id.lv);
list = Planet.getDefaultList();
PlanetBaseAdapter baseAdapter = new PlanetBaseAdapter(this, list);
lv.setAdapter(baseAdapter);
lv.setOnItemClickListener(this);
cb_divider = findViewById(R.id.cb_divider);
cb_selector = findViewById(R.id.cb_selector);
cb_divider.setOnCheckedChangeListener(this);
cb_selector.setOnCheckedChangeListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(this,"You select " + list.get(i).name, Toast.LENGTH_SHORT).show();
}
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
switch (compoundButton.getId()){
case R.id.cb_divider:
if(cb_divider.isChecked()){
Drawable divider = getResources().getDrawable(R.color.black, getTheme());
lv.setDivider(divider);
lv.setDividerHeight(1);
}else{
lv.setDivider(null);
lv.setDividerHeight(0);
}
break;
case R.id.cb_selector:
if(cb_selector.isChecked()){
lv.setSelector(R.drawable.list);
}else{
Drawable drawable = getResources().getDrawable(R.color.transparent);
lv.setSelector(drawable);
}
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".ListViewActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="70dp">
<CheckBox
android:id="@+id/cb_divider"
android:layout_width="0dp"
android:layout_weight="1"
android:text="divider"
android:textSize="30dp"
android:layout_height="match_parent"></CheckBox>
<CheckBox
android:id="@+id/cb_selector"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="selector"
android:textSize="30dp"
></CheckBox>
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
2.ListView点击事件冲突
当item里面放置一个button后,设置监听button,会发现button可以正常点击,但是点击item(非button部分)时,会没有响应。
这是因为ListView默认会把焦点传递给子项,可以之间在item的布局中设置
android:descendantFocusability="blocksDescendants"
这样item就会得到焦点,响应用户点击