ImageButton 打开图像列表 select 来自

ImageButton opens list of images to select from

我正在使用 Navigation Drawer 创建我的第一个 Android 应用程序,我的大部分页面都是片段。每个页面将包含几个 ImageButtons。

我的目标是让用户单击 ImageButton,然后显示一个 "popup" 图像列表及其各自的名称。当用户选择图像时,ImageButton 的 src 应该成为该图像。

关于如何实现这一目标的任何提示?谢谢!

这是我的片段之一:

public class FragmentAnubis extends Fragment {

View myView;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    myView = inflater.inflate(R.layout.god_anubis, container, false);
    return myView;
}

这是它对应的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="100">

        <ImageButton
            android:id="@+id/imageStarterItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="7dp"
            android:background="@null"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/ah_puch" />

        <ImageButton
            android:id="@+id/imageRelic1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="7dp"
            android:background="@null"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/ah_muzen_cab" />

        <ImageButton
            android:id="@+id/imageRelic2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="7dp"
            android:background="@null"
            android:scaleType="fitCenter"
            app:srcCompat="@drawable/agni" />
    </LinearLayout>

</LinearLayout>

首先,删除不必要的根LinearLayout。

我猜你使用 DialogFragment 来显示图像列表。在 onclick 处理程序中获取图像源并将其发送给事件总线 - Rx 事件总线或 GreenRobots 事件总线 https://github.com/greenrobot/EventBus

在源代码中 activity 您可以捕获所有事件并将源代码设置为第一个按钮