67

展開可能なリストビューに関して、グループアイコンインジケーターが左側に配置されていますが、インジケーターを移動して右側に配置できますか?ありがとう。

編集済み:ビューを拡張したくないので、幅を動的に取得するというこの回避策を取得しました。私のソリューションを共有するだけです。

newDisplay = getWindowManager()。getDefaultDisplay();を表示します。
int width = newDisplay.getWidth();
newListView.setIndicatorBounds(width-50、width);
4

16 に答える 16

71

setIndicatorBounds(int、int)は、Android4.3では正しく機能しません。彼らは、4.3で問題なく動作する新しいメソッドsetIndicatorBoundsRelative(int、int)を導入しました。

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
       mExpandableListView.setIndicatorBounds(myLeft, myRight);
    } else {
       mExpandableListView.setIndicatorBoundsRelative(myLeft, myRight);
    }
}
于 2013-08-02T09:53:30.530 に答える
46

すべてのXMLソリューション! 私はこの問題に取り組むためのより良い方法を見つけました。これには、表示メトリックをいじる必要はなく、プログラムでハッキングする必要もありません。

これはあなたがする必要があることです:

1)レイアウトの方向を右から左に設定します

<ExpandableListView
...
android:layoutDirection="rtl" />

2)リストアイテムのレイアウトにこれが含まれていることを確認します(はい、カスタムレイアウトが必要です):

android:gravity="left" //to adjust the text to align to the left again
android:layoutDirection="ltr"  //OR this line, based on your layout

そして、あなたは行ってもいいです!

于 2016-06-28T14:52:58.330 に答える
39

これを行うための最良の方法は以下のとおりですが、すべてのAndroidバージョンに対応しているわけではありません。したがって、以下に指定されている2番目または3番目の方法を使用してください。

ViewTreeObserver vto = mExpandableListView.getViewTreeObserver();

    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
                 mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
        }
    });

またはこれ:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
} 

デバイス上でもタブ上でも、そのインジケーターをリストビューの右側に移動します。

または、遅延後のスレッドでこれを行うことができます。

 (new Handler()).post(new Runnable() {

        @Override
        public void run() {
              mExpandableListView.setIndicatorBounds(mExpandableListView.getRight()- 40, mExpandableListView.getWidth());
        }
    });
于 2012-02-13T11:01:22.847 に答える
20

のソースコードによるとExpandableListView、インジケーターを右側に移動する唯一の方法は、ExpandableListView.setIndicatorBounds()メソッドを使用して境界を変更することです。onSizeChanged()たとえば、バウンドインメソッドを計算できます。

于 2011-04-27T07:09:20.180 に答える
11

setIndicatorBoundsとsetIndicatorBoundsRelativeを使用しようとしましたが、アイコンが期待どおりに表示されません。だから私は以下のように私のコードを変更します:

グループレイアウトを作成します:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@drawable/header_selector" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:contentDescription="@string/desc_list_item_icon"
        android:src="@drawable/ic_home" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/icon"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:paddingRight="40dp"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="@color/list_item_title" />

    <TextView
        android:id="@+id/counter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:background="@drawable/counter_bg"
        android:textColor="@color/counter_text_color" />

    <ImageView
        android:id="@+id/icon_expand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="200dp"
        android:layout_toRightOf="@+id/counter"
        android:contentDescription="@string/desc_list_item_icon"
        android:src="@drawable/navigation_expand"
        android:visibility="visible" />

    <ImageView
        android:id="@+id/icon_collapse"
        android:layout_width="25dp"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="200dp"
        android:layout_toRightOf="@+id/counter"
        android:contentDescription="@string/desc_list_item_icon"
        android:src="@drawable/navigation_collapse"
        android:visibility="gone" /> 

</RelativeLayout>

getGroupViewメソッド内のアダプタクラスでこのレイアウトを使用します。

 @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

        NavDrawerItem itemHeader = (NavDrawerItem) getGroup(groupPosition);

        LayoutInflater inflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View view = null;
        if (convertView == null) {
            view = (View) inflater.inflate(R.layout.drawer_header_item, null);
        } else {
            view = convertView;
        }

        ImageView icon = (ImageView) view.findViewById(R.id.icon);

        if (itemHeader.isIconVisible()) {
            icon.setImageResource(itemHeader.getIcon());
        } else {
            icon.setVisibility(View.GONE);
        }

        TextView textTitle = (TextView) view.findViewById(R.id.title);
        textTitle.setText(" " + itemHeader.getTitle());

        TextView textCount = (TextView) view.findViewById(R.id.counter);

        if (itemHeader.getCounterVisibility()) {
            textCount.setText(itemHeader.getCount());
        } else {
            textCount.setVisibility(View.GONE);
        }

        ImageView iconExpand = (ImageView) view.findViewById(R.id.icon_expand);
        ImageView iconCollapse = (ImageView) view
                .findViewById(R.id.icon_collapse);

        if (isExpanded) {
            iconExpand.setVisibility(View.GONE);
            iconCollapse.setVisibility(View.VISIBLE);
        } else {
            iconExpand.setVisibility(View.VISIBLE);
            iconCollapse.setVisibility(View.GONE);
        }

        if (getChildrenCount(groupPosition) == 0) {
            iconExpand.setVisibility(View.GONE);
            iconCollapse.setVisibility(View.GONE);
        }

        return view;
    }

この方法を使用すると、展開/折りたたみアイコンの位置を適切に調整できます。それが役に立てば幸い。

于 2013-12-05T08:16:44.313 に答える
5

拡張可能なリストビューは、グループアイコンインジケータを右に移動します

setIndicatorBounds(int left, int right)、展開可能なリストビューのグループビューのインジケーター境界を設定するために使用されます。

explvList.setIndicatorBounds(width-GetDipsFromPixel(35), width-GetDipsFromPixel(5));
Here width means device width.

/Convert pixel to dip 
public int GetDipsFromPixel(float pixels)
{
        // Get the screen's density scale
        final float scale = getResources().getDisplayMetrics().density;
        // Convert the dps to pixels, based on density scale
        return (int) (pixels * scale + 0.5f);
} 

参考:幅は、setBoundsメソッドで指定された幅と同じです。ここで私のスニペットでは35です。それ以外の場合はアイコンが乱れます。詳細については、こちらをご覧ください。

于 2015-06-08T08:49:48.110 に答える
3

このコードをExpandableBaseアダプターに記述します。

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    ImageView imgs;

    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_group, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
     imgs = (ImageView) convertView
            .findViewById(R.id.img_list);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);
    if (isExpanded) {
  imgs.setImageResource(R.drawable.up_arrow);
    }
    else {
        imgs.setImageResource(R.drawable.downs_arrow);
    }

    return convertView;
}
  • リストアイテム
于 2017-08-02T07:24:23.997 に答える
2
FIX: 

彼らは、API 18以降に、setIndicatorBoundsRelative(int、int)というメソッドを導入しました。Androidのバージョンを確認し、APIでそれぞれのメソッドを使用する必要があります。

expListView = (ExpandableListView) v.findViewById(R.id.laptop_list);

metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;

if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
    expListView.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));

} else {
    expListView.setIndicatorBoundsRelative(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));

}
于 2015-07-03T13:31:22.600 に答える
2

フラグメントにある場合-onViewCreated()

    ViewTreeObserver vto = Expnlist.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new      ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Expnlist.setIndicatorBounds(Expnlist.getMeasuredWidth() - 80, Expnlist.getMeasuredWidth());
        }
    });

その作品は私のために!乾杯!

于 2016-03-31T11:43:04.857 に答える
2

まず、xmlからグループインジケーターを削除する必要があります

<ExpandableListView
            android:id="@+id/expandableListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/nav_header_height"
            android:background="@android:color/white"
            android:dividerHeight="0dp"
            android:focusable="false"
            android:groupIndicator="@null" />

次に、カスタムアダプタで次のようにします

public View getGroupView(int groupPosition, boolean isExpanded,
    View convertView, ViewGroup parent) {

if (isExpanded) {
    groupHolder.img.setImageResource(R.drawable.group_down);
} else {
    groupHolder.img.setImageResource(R.drawable.group_up);
}}
于 2020-08-13T12:27:31.227 に答える
0

setIndicatorBounds(int left, int right)、のグループビューのインジケーター境界を設定するために使用されますExpandableListView

explvList.setIndicatorBounds(width-GetDipsFromPixel(35), width-GetDipsFromPixel(5));

ここで、widthはデバイスの幅を意味します。

ピクセルをディップに変換します:

public int GetDipsFromPixel(float pixels){
    // Get the screen's density scale
    final float scale = getResources().getDisplayMetrics().density;
    // Convert the dps to pixels, based on density scale
    return (int) (pixels * scale + 0.5f);
} 
于 2017-08-02T07:26:45.223 に答える
0

このサンプルに触発された次の短い部分を使用しています。これには、フォルダーに2つのドローアブルが必要です。

if (isExpanded) {
    ListHeader.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.up, 0);
} else {
    ListHeader.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.down, 0);
} 

とにかく、デフォルトの矢印を「取り除く」ために、goodKodeのソリューションも採用する必要がありました。

したがって、最もミニマリストで正確なアプローチであるため、以前に提供された単純なソリューションを使用することをお勧めします。

于 2017-12-13T04:18:57.277 に答える
0
     DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels; 

 mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
 mExpandableList.setIndicatorBounds(width - GetPixelFromDips(50), width - GetPixelFromDips(10));  

   public int GetPixelFromDips(float pixels) {
    // Get the screen's density scale 
    final float scale = getResources().getDisplayMetrics().density;
    // Convert the dps to pixels, based on density scale
    return (int) (pixels * scale + 0.5f);
}

これは私のために働いた

于 2018-01-30T11:13:09.347 に答える
0

まず、以下のように拡張可能なリストビューにandroid:groupIndicator ="@null"を設定します。

次に、ヘッダーレイアウトで次のようにします。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/screen_margin"
    android:paddingRight="@dimen/screen_margin"
    android:paddingTop="@dimen/dot_margin"
    android:paddingBottom="@dimen/dot_margin"
    android:orientation="vertical">




    <TextView
        android:id="@+id/tvQuestion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="what is jodi?"
        android:textColor="@color/textColor"
        android:textSize="@dimen/font_large"
        app:customFont="@string/font_toolbar"
        android:layout_toLeftOf="@+id/imgDropDown"
        android:layout_marginRight="@dimen/margin"
       />


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imgDropDown"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/right_arrow"/>

</RelativeLayout>

最後に、これをgetGroupViewメソッドのアダプタに追加します。以下を追加します。

TextView lblListHeader = (TextView) convertView.findViewById(R.id.tvQuestion);

lblListHeader.setText(headerTitle);

ImageView img=convertView.findViewById(R.id.imgDropDown);

if (isExpanded) {
  img.setImageResource(R.drawable.down_arrow);
    lblListHeader.setTextColor(_context.getResources().getColor(R.color.colorPrimary));
} else {
 img.setImageResource(R.drawable.right_arrow);
    lblListHeader.setTextColor(_context.getResources().getColor(R.color.textColor));
}
于 2018-04-12T11:46:40.877 に答える
0

手遅れだとは思いますが、プログラムでインジケーターを右側に配置するための解決策は、シンプルで使いやすいです:

expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);

        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        Resources r = getResources();
        int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                50, r.getDisplayMetrics());
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
            expandableListView.setIndicatorBounds(width - px, width);
        } else {
            expandableListView.setIndicatorBoundsRelative(width - px, width);
        }

あなたはどこexpandableListViewにいますかExpandableListview

于 2018-04-23T13:11:25.853 に答える
-2

これを試してください...このコードは、ExpandableListグループインジケーターをビューの右側に調整するためのものです。

private ExpandableListView expandableListView;
DisplayMetrics metrics;
int width;
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
width = metrics.widthPixels;
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView1);
expandableListView.setIndicatorBounds(width - GetDipsFromPixel(50), width - GetDipsFromPixel(10));

そして、このメソッドを呼び出します。

public int GetDipsFromPixel(float pixels)
{
 // Get the screen's density scale
 final float scale = getResources().getDisplayMetrics().density;
 // Convert the dps to pixels, based on density scale
 return (int) (pixels * scale + 0.5f);
}

結果は.. ExpandableList右側のグループインジケーター

于 2014-02-13T12:09:58.967 に答える