1

我扩展了一个包含 listView 的 ViewGroup。当我单击列表项时,我调用了 notifyDataChanged 方法,但它没有正常工作,虽然 getCount 方法返回 >0,但调用了 getCount 方法但未调用 getView 方法。此外,我发现 listView 不再可点击。那我就不明白了。如果我通过调用 setAdapter(adpater) 重置适配器,listView 原来是空的,(但是调用了 getCount 方法,也没有调用 getView 方法。)有人可以帮我吗?

代码 :

public class SelectDistrictView extends ViewGroup {

private ListView districtList = null;
private ListView sectionList = null;
private SelectDistrictAdapter districtAdapter = null;
private ArrayAdapter<String> sectionAdapter = null;

private List<String> districtStrList = null;
private int selectedDist = 2;

private ImageButton dismissBtn = null;
private PopupWindow pop = null;


public SelectDistrictView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    districtList = new ListView(context);
    sectionList = new ListView(context);
    districtList.setLayoutParams(params);
    sectionList.setLayoutParams(params);        

    districtStrList = new ArrayList<String>();
    districtStrList.add("区域1");
    districtStrList.add("区域2");
    districtStrList.add("区域3");
    districtAdapter = new SelectDistrictAdapter(context);
    districtList.setAdapter(districtAdapter);

    districtList.setBackgroundColor(Color.rgb(238, 238, 238));
    // districtList.setCacheColorHint(Color.argb(0, 0, 0, 0));
    // districtList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    // districtList.setSelector(R.drawable.bg_select_district);

    final Context _context= context;
    districtList.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            selectedDist = arg2;
            districtList.setSelection(arg2);
            //districtStrList.remove(0);
            //UpdateList task = new UpdateList();
            //SherlockActivity a = (SherlockActivity)_context;
            //a.runOnUiThread(task);
            districtAdapter.notifyDataSetChanged();
            //districtList.setAdapter(districtAdapter);
            Toast.makeText(_context, "" + arg2, Toast.LENGTH_SHORT).show();
        }
    });
    //districtList.setDivider(getResources().getDrawable(R.color.select_district_delive_color));
    //districtList.setSelector(R.drawable.select_district_list_selector);

    sectionAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1);
    sectionAdapter.add("商圈1");
    sectionAdapter.add("商圈2");
    sectionAdapter.add("商圈3");
    sectionAdapter.add("商圈4");
    sectionAdapter.add("商圈5");
    sectionAdapter.add("商圈3");
    sectionAdapter.add("商圈4");
    sectionAdapter.add("商圈5");
    sectionAdapter.add("商圈3");
    sectionAdapter.add("商圈4");
    sectionAdapter.add("商圈5");
    sectionList.setAdapter(sectionAdapter);
    sectionList.setBackgroundColor(Color.rgb(220, 220, 220));
    //sectionList.setSelector(Color.rgb(169, 207, 56));

    dismissBtn = new ImageButton(context);
    //dismissBtn.setBackgroundResource(R.drawable.select_district_dismiss);
    dismissBtn.setImageResource(R.drawable.select_district_dismiss);
    dismissBtn.setBackgroundDrawable(null);
    //dismissBtn.setGravity(Gravity.CENTER);
    dismissBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (null != pop && pop.isShowing()) {
                pop.dismiss();
            }
        }
    });
}

public PopupWindow getPop() {
    return pop;
}

public void setPop(PopupWindow pop) {
    this.pop = pop;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // TODO Auto-generated method stub
    boolean containDist = false, containSect = false, containDismiss = false;
    for(int i = 0; i < this.getChildCount(); i++) {
        View cView = this.getChildAt(i);
        if (cView == districtList) {
            containDist = true;
            continue;
        }

        if (cView == sectionList) {
            containSect = true;
            continue;
        }

        if (cView == dismissBtn) {
            containDismiss = true;
            continue;
        }

    }

    int lWidth = CommonFunction.dp2px(getContext(), 120);
    int fHeight = CommonFunction.dp2px(getContext(), 48);
    if (!containDist) {
        districtList.layout(0, 0, lWidth, b - t - fHeight);
        addViewInLayout(districtList, 0, null, true);
    }

    if (!containSect) {
        sectionList.layout(lWidth, 0, r - l, b - t - fHeight);
        addViewInLayout(sectionList, 0, null, true);
    }

    if (!containDismiss) {
        dismissBtn.layout(0, b - t - fHeight, r - l, b - t);
        addViewInLayout(dismissBtn, 0, null, true);
    }
    invalidate();
}

public static PopupWindow popSelector(Context context, View parent) {
    SelectDistrictView view = new SelectDistrictView(context);
    PopupWindow pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    pop.setFocusable(true);
    pop.setOutsideTouchable(true);
    pop.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.bg_select_district));
    //pop.showAtLocation(parent, Gravity.CENTER, 0, 0);
    view.setPop(pop);
    pop.showAsDropDown(parent, 0, 0);
    return pop;
}

private final class SelectDistrictAdapter extends BaseAdapter {

    private Context context = null;
    private LayoutInflater inflater = null;

    public SelectDistrictAdapter(Context context) {
        super();
        this.context = context;

        inflater = LayoutInflater.from(this.context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        Log.v("reflesh", "all:" + districtStrList.size());
        return districtStrList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return districtStrList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.v("reflesh", "position:" + position);
        ViewHolder holder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.select_district_list_item, null);

            holder = new ViewHolder();
            holder.tvName = (TextView)convertView.findViewById(R.id.select_district_list_item_name);
            holder.viewSelect = convertView.findViewById(R.id.select_district_list_item_selected_underline);
            holder.viewSelectTriangle = (ImageView)convertView.findViewById(R.id.select_district_list_item_selected_triangle);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.tvName.setText(districtStrList.get(position));
        if(selectedDist == position) {
            //convertView.setBackgroundColor(Color.rgb(220, 220, 220));
            holder.viewSelect.setVisibility(View.VISIBLE);
            holder.viewSelectTriangle.setVisibility(View.VISIBLE);
        } else {
            //convertView.setBackgroundColor(Color.rgb(238, 238, 238));
            holder.viewSelect.setVisibility(View.GONE);
            holder.viewSelectTriangle.setVisibility(View.GONE);
        }

        return convertView;
    }
    private class ViewHolder {
        TextView tvName;
        View viewSelect;
        ImageView viewSelectTriangle;
    }
}

private class UpdateList implements Runnable {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        districtAdapter.notifyDataSetChanged();
    }

}

}

在我看到问题BaseAdapter notifyDatasetChanged() 被调用但 getView() 从未被调用

事实证明,我的 getView() 没有被调用的问题是因为它不可见。我的布局 xml 具有上部 TextView,其高度为 fill_parent。因此,整个视图只有一个 TextView 可见。

因此解决方案是:检查相关布局的图形视图以确保 ListView 可见。

我认为这可能是问题所在,但我不知道如何解决它。

项目布局xml:

<?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="48dp"
android:orientation="vertical" >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minWidth="120dp" >

    <TextView
        android:id="@+id/select_district_list_item_name"
        android:textIsSelectable="false"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:padding="12dp"
        android:textColor="#333333"
        android:textSize="16sp" />

    <View
        android:id="@+id/select_district_list_item_selected_underline"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_alignBottom="@id/select_district_list_item_name"
        android:background="#A9CF38" />

    <ImageView
        android:id="@+id/select_district_list_item_selected_triangle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/select_district_list_item_selected_underline"
        android:layout_alignParentRight="true"
        android:contentDescription="@string/app_name"
        android:src="@drawable/select_district_selected_triangle" />
</RelativeLayout>
</LinearLayout>
4

1 回答 1

0

已解决:这个问题很奇怪。当我想刷新 listView 时,我调用adapter.notifyDataSetChanged()方法,然后调用ListView.requestFocusFromTouch()方法。这对我有用。

这是代码:

adapter.notifyDataSetChanged();
listView.requestFocusFromTouch();
backBtn.requestFocusFromTouch();// it is requred too. backBtn is other view in your layout

谁能解释一下?

于 2013-06-28T05:32:13.550 回答