4

我使用一个 listView,其中一行的行有 2 个布局

setOnItemClickListener

但在另一行它不识别水龙头,

public void initItemTable()
{
    listViewItem = (ListView) getView().findViewById(R.id.listViewItem);

    listViewItem.setAdapter(new PhoneItemAdapter(new ItemPhoneDataSource().getItems()));

    listViewItem.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub

    Log.d("mensa", "item index :"+arg2);    
        }
    });
}

//
    private class PhoneItemAdapter extends BaseAdapter {  
    final List<RowPhone> rows;//row
    //data source, style
    PhoneItemAdapter(List<ItemPhone> animals) {
        rows = new ArrayList<RowPhone>();//member variable

        //choose cell! iterator
        for (ItemPhone item : animals) {

            if (item.getType().equals("item")) {
                rows.add(new RowItemPhone(LayoutInflater.from(getActivity()), item));                   

            } else {
                rows.add(new RowFolderPhone(LayoutInflater.from(getActivity()), item)); //imageRow!

            }                
        }
    }

    //delegate
    @Override
    public int getViewTypeCount() {
        return RowTypePhone.values().length;
    }

    @Override
    public int getItemViewType(int position) {
        //con cast
        return ((RowPhone) rows.get(position)).getViewType();

    }

    public int getCount() {
        return rows.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        //cambiado con cast!
        return ((RowPhone) rows.get(position)).getView(convertView);
    }
    }

因此,当“item.getType()”是文件夹但不在项目中时,会检测到点击,

. 我应该包含 RowItemPhone.java 和 RowFolderPhone.java 的代码吗?

那么如何解决这个水龙头问题呢?

谢谢!

编辑 1. RowPhone.java

public  interface RowPhone {
public  View getView(View convertView);
public  int getViewType();
}

编辑 2. RowFolder.java .. 水龙头检测良好的那个:

public class RowItemPhone implements RowPhone{

private final ItemPhone item;
private final LayoutInflater inflater;

public RowItemPhone(LayoutInflater inflater, ItemPhone animal) {
    this.item = animal;
    this.inflater = inflater;
}

//text doble
public View getView(View convertView) {
    ViewHolder holder;
    View view = null;

    if (convertView == null) {
        //ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_item_phone, null);

        ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_item_phone, (ViewGroup) view, false);


        holder = new ViewHolder(
                (ImageView)viewGroup.findViewById(R.id.image_item), 
                (TextView)viewGroup.findViewById(R.id.title),
                (TextView)viewGroup.findViewById(R.id.description));

        viewGroup.setTag(holder); //pa q y como usa tag!
        view = viewGroup;
    } else {
        view = convertView;
        holder = (ViewHolder)convertView.getTag();
    }


    //setup
    holder.imageView.setImageResource(item.getImageId());
    holder.descriptionView.setText(item.getDescription());
    holder.titleView.setText(item.getName());

    return view;
}

public int getViewType() {
    return RowTypePhone.DESCRIPTION_ROW.ordinal();
}

private static class ViewHolder {
    final ImageView imageView;
    final TextView titleView;
    final TextView descriptionView;

    private ViewHolder(ImageView imageView, TextView titleView, TextView descriptionView)     {
        this.imageView = imageView;
        this.titleView = titleView;
        this.descriptionView = descriptionView;
    }
}

   }

在这里 RowFolderPhone.java ... 显示正常,但未检测到水龙头:

public class RowFolderPhone implements RowPhone {


private final ItemPhone item;
private final LayoutInflater inflater;

public RowFolderPhone(LayoutInflater inflater, ItemPhone animal) {
    this.item = animal;
    this.inflater = inflater;
}

public View getView(View convertView) {
    ViewHolder holder;
    View view = null;

    //we have a don't have a converView so we'll have to create a new one
    if (convertView == null) {
       // ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_folder_phone, null);
        ViewGroup viewGroup = (ViewGroup)inflater.inflate(R.layout.row_folder_phone, (ViewGroup) view, false);

        //use the view holder pattern to save of already looked up subview
        holder = new ViewHolder((ImageView)viewGroup.findViewById(R.id.image),
                (TextView)viewGroup.findViewById(R.id.title));

        viewGroup.setTag(holder);

        view = viewGroup;

    } else {
        //get the holder back out
        holder = (ViewHolder)convertView.getTag();

        view = convertView;
    }

    //actually setup the view
    holder.imageView.setImageResource(item.getImageId());
    holder.titleView.setText(item.getName());

    return view;
}

public int getViewType() {
    return RowTypePhone.IMAGE_ROW.ordinal();
}

private static class ViewHolder {
    final ImageView imageView;
    final TextView titleView;

    private ViewHolder(ImageView imageView, TextView titleView) {
        this.imageView = imageView;
        this.titleView = titleView;
    }
  } 
 }
4

2 回答 2

3

PhoneItemAdapter 上的 getItem 方法有问题。getItem 方法应该返回列表项而不是位置。因此,您应该将代码替换为

public RowPhone getItem(int position) {
    return rows.get(position);
}
于 2013-09-20T03:35:20.800 回答
1

通过设置 descendantFocusability 解决了问题

在:row_item_phone.xml

 <RelativeLayout ...
 android:descendantFocusability="blocksDescendants"
于 2013-09-23T13:28:42.510 回答