1

listView を含む ViewGroup を拡張します。リスト項目をクリックすると、notifyDataChanged メソッドを呼び出しますが、正しく動作しませんでした。getCount メソッドが呼び出されますが、getCount メソッドが >0 を返しても 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 には、高さが fill_parent の上部 TextView があります。したがって、ビュー全体には、その 1 つの 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