112

だから私はカスタム ListView オブジェクトを持っています。リスト アイテムには 2 つのテキストビューが重なり合っており、さらに、実際に何かを行うまで隠しておきたい水平のプログレス バーがあります。右端には、ユーザーがデータベースの更新をダウンロードする必要がある場合にのみ表示するチェックボックスがあります。可視性を Visibility.GONE に設定してチェックボックスを無効にすると、リスト項目をクリックできます。チェックボックスが表示されている場合、リスト内のチェックボックス以外はクリックできません。いろいろ調べてみましたが、現在の状況に当てはまるものは見つかりませんでした。この質問を見つけましたしかし、ArrayLists を使用してデータベースのリストを内部に格納しているため、オーバーライドされた ArrayAdapter を使用しています。Tom のように、LinearLayout ビューを取得して onClickListener を追加するだけでよいのでしょうか? わからない。

リストビューの行レイアウト XML は次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_weight="1"
        android:layout_height="fill_parent">
        <TextView
            android:id="@+id/UpdateNameText"
            android:layout_width="wrap_content"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:textSize="18sp"
            android:gravity="center_vertical"
            />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_weight="1"
            android:id="@+id/UpdateStatusText"
            android:singleLine="true"
            android:ellipsize="marquee"
            />
        <ProgressBar android:id="@+id/UpdateProgress" 
                     android:layout_width="fill_parent" 
                     android:layout_height="wrap_content"
                     android:indeterminateOnly="false" 
                     android:progressDrawable="@android:drawable/progress_horizontal" 
                     android:indeterminateDrawable="@android:drawable/progress_indeterminate_horizontal" 
                     android:minHeight="10dip" 
                     android:maxHeight="10dip"                    
                     />
    </LinearLayout>
    <CheckBox android:text="" 
              android:id="@+id/UpdateCheckBox" 
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content" 
              />
</LinearLayout>

ListActivity を拡張するクラスは次のとおりです。明らかに、それはまだ開発中であるため、不足している、または放置されている可能性のあるものを許してください。

public class UpdateActivity extends ListActivity {

    AccountManager lookupDb;
    boolean allSelected;
    UpdateListAdapter list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lookupDb = new AccountManager(this);
        lookupDb.loadUpdates();

        setContentView(R.layout.update);
        allSelected = false;

        list = new UpdateListAdapter(this, R.layout.update_row, lookupDb.getUpdateItems());
        setListAdapter(list);

        Button btnEnterRegCode = (Button)findViewById(R.id.btnUpdateRegister);
        btnEnterRegCode.setVisibility(View.GONE);

        Button btnSelectAll = (Button)findViewById(R.id.btnSelectAll);
        btnSelectAll.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                allSelected = !allSelected;

                for(int i=0; i < lookupDb.getUpdateItems().size(); i++) {
                    lookupDb.getUpdateItem(i).setSelected(!lookupDb.getUpdateItem(i).isSelected());
                }

                list.notifyDataSetChanged();
                // loop through each UpdateItem and set the selected attribute to the inverse 

            } // end onClick
        }); // end setOnClickListener

        Button btnUpdate = (Button)findViewById(R.id.btnUpdate);
        btnUpdate.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
            } // end onClick
        }); // end setOnClickListener

        lookupDb.close();
    } // end onCreate


    @Override
    protected void onDestroy() {
        super.onDestroy();

        for (UpdateItem item : lookupDb.getUpdateItems()) {
            item.getDatabase().close();        
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        UpdateItem item = lookupDb.getUpdateItem(position);

        if (item != null) {
            item.setSelected(!item.isSelected());
            list.notifyDataSetChanged();
        }
    }

    private class UpdateListAdapter extends ArrayAdapter<UpdateItem> {
        private List<UpdateItem> items;

        public UpdateListAdapter(Context context, int textViewResourceId, List<UpdateItem> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = null;

            if (convertView == null) {
                LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = li.inflate(R.layout.update_row, null);
            } else {
                row = convertView;
            }

            UpdateItem item = items.get(position);

            if (item != null) {
                TextView upper = (TextView)row.findViewById(R.id.UpdateNameText);
                TextView lower = (TextView)row.findViewById(R.id.UpdateStatusText);
                CheckBox cb = (CheckBox)row.findViewById(R.id.UpdateCheckBox);

                upper.setText(item.getName());
                lower.setText(item.getStatusText());

                if (item.getStatusCode() == UpdateItem.UP_TO_DATE) {
                    cb.setVisibility(View.GONE);
                } else {
                    cb.setVisibility(View.VISIBLE);
                    cb.setChecked(item.isSelected());
                }

                ProgressBar pb = (ProgressBar)row.findViewById(R.id.UpdateProgress);
                pb.setVisibility(View.GONE);
            }
            return row;
        }

    } // end inner class UpdateListAdapter
}

編集:私はまだこの問題を抱えています。テキストビューに onClick ハンドラーをだまして追加していますが、チェックボックスをクリックしていないときに onListItemClick() 関数がまったく呼び出されていないのは非常にばかげているようです。

4

4 に答える 4

243

問題は、Android では、フォーカス可能な要素を持つリスト項目を選択できないことです。リスト項目のチェックボックスを次のような属性に変更しました。

android:focusable="false"

チェックボックスを含むリスト項目 (ボタンでも機能します) は、伝統的な意味で「選択可能」です (点灯し、リスト項目のどこでもクリックでき、「onListItemClick」ハンドラーが起動するなど)。

編集:更新として、コメント投稿者は「ボタンの可視性を変更した後、プログラムでフォーカスを再度無効にする必要があることに注意してください。」と述べました。

于 2009-10-13T20:53:14.363 に答える
44

descendantFocusabilityリスト アイテム内に ImageButton がある場合は、ルート リスト アイテム要素で値を「blocksDescendants」に設定する必要があります。

android:descendantFocusability="blocksDescendants"

そして、focusableInTouchModeフラグがビューに表示さtrueImageButtonます。

android:focusableInTouchMode="true"
于 2013-10-01T09:59:36.747 に答える
13

同様の問題が発生したことがあり、ListView では CheckBox がかなり厄介であることがわかりました。何が起こるかというと、ListItem 全体にその意志を課し、onListItemClick をオーバーライドします。そのためのクリック ハンドラーを実装し、TextViews を使用する代わりに、CheckBox のテキスト プロパティも設定することができます。

このViewオブジェクトも調べてください。CheckBoxよりもうまくいくかもしれません

チェックされたテキストビュー

于 2009-07-14T13:37:20.910 に答える
9

リスト項目のルート ビューでこの行を使用します

android:descendantFocusability="blocksDescendants"

于 2014-05-28T18:48:39.167 に答える