处理条目点击事件冲突加上下面这句:
android:descendantFocusability="blocksDescendants"
package com.qidian.chapter07.bean;
import com.qidian.chapter07.R;
import java.util.ArrayList;
import java.util.List;
public class Planet {
public int image;
public String name;
public String desc;
public Planet(int image, String name, String desc) {
this.image = image;
this.name = name;
this.desc = desc;
}
private static int[] iconArray = {R.drawable.jin, R.drawable.jin, R.drawable.jin};
private static String[] nameArray = {"水星", "金星", "木星"};
private static String[] descArray = {"地球是太阳系八大行星之一、、、、、、", "地球是太阳系八大行星之一、、、、、、", "地球是太阳系八大行星之一、、、、、、"};
public static List<Planet> getDefaultList() {
List<Planet> list = new ArrayList<>();
for (int i = 0; i < iconArray.length; i++) {
list.add(new Planet(iconArray[i],nameArray[i],descArray[i]));
}
return list;
}
}
package com.qidian.chapter07.adapter;
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.qidian.chapter07.R;
import com.qidian.chapter07.bean.Planet;
import java.util.List;
public class PlanetBaseAdapter extends BaseAdapter {
private Context mContext;
private List<Planet> mPlanetList;
public PlanetBaseAdapter(Context mContext, List<Planet> mPlanetList) {
this.mContext = mContext;
this.mPlanetList = mPlanetList;
}
@Override
public int getCount() {
return mPlanetList.size();
}
@Override
public Object getItem(int position) {
return mPlanetList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// 根据布局文件item_list.xml 生成转换视图对象
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, null);
holder = new ViewHolder();
holder.iv_icon = convertView.findViewById(R.id.iv_icon);
holder.tv_name = convertView.findViewById(R.id.tv_name);
holder.tv_desc = convertView.findViewById(R.id.tv_desc);
// 将视图持有者保存到转换视图中
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// 设置数据
Planet planet = mPlanetList.get(position);
holder.iv_icon.setImageResource(planet.image);
holder.tv_name.setText(planet.name);
holder.tv_desc.setText(planet.desc);
return convertView;
}
public final class ViewHolder {
public ImageView iv_icon;
public TextView tv_name;
public TextView tv_desc;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base_adapter);
sp_base = findViewById(R.id.sp_base);
List<Planet> planetList = Planet.getDefaultList();
PlanetBaseAdapter adapter = new PlanetBaseAdapter(this, planetList);
sp_base.setAdapter(adapter);
}