2

コンテキスト アクション バーで使用する Android の複数選択モーダル モードでリスト行またはグリッド項目を強調表示する (またはチェックボックスを表示する) Java または XML コードを実装するにはどうすればよいですか?

コンテキスト アクション バーと ListView/GridView を実装しており、それらを選択して、選択したアイテムに対して関数を実行できますが、リストの行/アイテムを長押ししたときにリストの行/アイテムが簡単に強調表示される以外に、視覚的なフィードバックはありません。離すと消えます。

私の最初の考えは、アダプターの行/アイテムの背景色を設定することでしたが、うまくいかないようです。この質問の受け入れられた回答によって提案された解決策も試しまし

私は主に、マテリアル デザインのガイドラインおよび/または最も一般的な方法に従って、これを行う標準的な方法を知りたいと思っています。アドバイスや解決策を事前にありがとうございます。

編集

私はレッドマンの答えを試しました(実際には、コンテキストアクションモードと複数選択リスナーを使用しているため、それに似たものです)が、結果は得られませんでした。リスナーで行ったことは次のとおりです。

public void onItemCheckedStateChanged(ActionMode actionMode, int i, long id, boolean checked) {
            if (checked) {
                selectedItems.add(listAdapter.getItem(i));
                ((CheckBox) listAdapter.getView(i,null,listView).findViewById(R.id.listCheckBox)).setChecked(true);
            }
            else {
                selectedItems.remove(listAdapter.getItem(i));
                ((CheckBox) listAdapter.getView(i,null,listView).findViewById(R.id.listCheckBox)).setChecked(false);
            }
        }

エラーなしで実行されますが、チェックボックスには何もしないので、何が問題なのかわかりません。どんな助けでも本当に感謝しています。

4

5 に答える 5

1

「私は主に、マテリアル デザイン ガイドラインおよび/または最も一般的な方法に従って、これを行う標準的な方法を知りたいと思っています。」

私の答えがマテリアルデザインか一般的かはわかりませんが、Googleの「写真」アプリでの選択の処理に基づいてGridViewを作成しました。私はそれをテストし、強調表示された境界線と「チェックボックス」の両方が含まれることを知っています.

**免責事項**

セレクターの使用を検討しましたが、ビューがスクロールする場合に問題があるようです (これを参照)。また、この. また、この回答は基本的に縦向きモードの電話を処理するだけです。さまざまな構成 (画面サイズ、向きなど) を処理するには、さらに追加する必要があります。

ここにはたくさんありますが、私が言ったように、私はそれをテストしました. プロジェクト全体はGitHubに投稿されています。

1 つ目は、MainActivity onCreate() で

mList = new ArrayList<>();

// fill your list here

mAdapter = new GridAdapter(getApplicationContext(), R.layout.grid_item, mList);
GridView gridView = (GridView)findViewById(R.id.grid_view);
gridView.setAdapter(mAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Log.d(TAG, "item clicked = " + i);
        boolean isSelected = mList.get(i).isSelected();
        mList.get(i).setSelected(!isSelected);

        mAdapter.notifyDataSetChanged();
    }
});

次に、アダプターで getView() をオーバーライドします (私は ArrayAdapter を使用しています)。

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

    RelativeLayout itemLayout;

    GridItem gridItem = (GridItem)getItem(position);

    // use existing Views when we can
    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)
                getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        itemLayout = (RelativeLayout) inflater.inflate(R.layout.grid_item, null);
    } else {
        itemLayout = (RelativeLayout) convertView;
    }

    // get your bitmap or list item etc. here
    BitmapDrawable bitmap = gridItem.getBitmap();
    ImageView imageView = (ImageView)itemLayout.findViewById(R.id.image_view);
    imageView.setBackground(bitmap);

    if(gridItem.isSelected()) {
        final int PADDING = 16;

        Log.d(TAG, "position " + position + " is selected");
        itemLayout.findViewById(R.id.image_frame).setPadding(PADDING, PADDING, PADDING, PADDING);
        itemLayout.findViewById(R.id.custom_check_box)
                .setBackgroundResource(R.drawable.custom_check_box_selected);
    } else {
        Log.d(TAG, "postion " + position + " is NOT selected");
        itemLayout.findViewById(R.id.image_frame).setPadding(0,0,0,0);
        itemLayout.findViewById(R.id.custom_check_box)
                .setBackgroundResource(R.drawable.custom_check_box_unselected);
    }
    return itemLayout;
}

これが GridItem クラスのコアで、ゲッターとセッターは省略されています。

public class GridItem {

    private BitmapDrawable bitmap = null;
    private boolean isSelected = false;

    public GridItem(BitmapDrawable bitmap) {
        this.bitmap = bitmap;
    }
}

Javaについては以上です。いくつかのxmlについて

grid_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/colorAccent"
    >
    <FrameLayout
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:id="@+id/image_frame"
        >
        <!-- view for the main image -->
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="center"
            android:background="@android:color/white"
            android:id="@+id/image_view"
            />
    </FrameLayout>

    <!-- view for the 'checkbox' in upper left corner -->
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:src="@drawable/custom_check_box_unselected"
        android:id="@+id/custom_check_box"
        />
</RelativeLayout>

そして、これは content_main.xml や activity_main.xml などになります。

<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="2"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="20dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
    android:id="@+id/grid_view"
    >
</GridView>

そして今、ドローアブル フォルダー用に 2 つのファイルがあります。

custom_check_box_unselected.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="20dp"
    android:width="20dp"

    android:viewportWidth="400"
    android:viewportHeight="400">

    <!-- the outside box -->
    <!-- top line & top left corner -->
    <path android:pathData="M 340 30 H 62 c -20 0 -35 15 -35 35 "
        android:strokeColor="#000000" android:strokeWidth="20" />

    <!-- left line & bottom left corner -->
    <path android:pathData="M 27 64 v271 c0 20 15 35 35 35 "
        android:strokeColor="#000000" android:strokeWidth="20" />

    <!-- bottom line & bottom right corner -->
    <path android:pathData="M 60 370 h275 c20 0 35 -15 35 -35"
        android:strokeColor="#000000" android:strokeWidth="20" />

    <!-- right line & top right corner -->
    <path android:pathData="M 370 336 v -271 c0 -20 -15 -35 -35  -35"
        android:strokeColor="#000000" android:strokeWidth="20" />
</vector>

custom_check_box_selected.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height="20dp"
    android:width="20dp"

    android:viewportWidth="400"
    android:viewportHeight="400">

    <!-- the outside box -->
    <path android:pathData="M 340 30 H 62 c -20 0 -35 15 -35 35
            v271 c0 20 15 35 35 35
            h275 c20 0 35 -15 35 -35
            v -271 c0 -20 -15 -35 -35  -35 "
        android:fillColor="#FDD835"  android:strokeColor="#000000" android:strokeWidth="20" />

    <!-- the check mark -->
    <path android:pathData="M 140 320 l -100 -100 25 -30
            l 75 75 l 190 -190
            l 25 30 l -190 190"
        android:fillColor="#000000"  android:strokeColor="#000000" android:strokeWidth="2" />
</vector>
于 2017-01-09T15:23:14.897 に答える