java - 使用 RecyclerView 和 GridLayoutManager 向布局添加动态按钮

标签 java android button dynamic android-recyclerview

我使用 recyclerview 和 gridlayoutmanager 将动态图像按钮添加到 fragment 中的布局中。这些按钮是在用户执行操作时添加的,因此它们并非全部在 onCreateView() 期间创建。

当我的应用程序启动时,我似乎必须使用空图像按钮初始化我的回收器 View 和适配器 onCreateView() 。所以我这样做了,但它创建了一个如下图所示的小灰色方 block 。

enter image description here

然后,当用户执行操作并创建我想要的真实按钮时,该正方形仍然存在于刚刚创建的图像按钮上,就像您在下面的新图像中看到的那样。

enter image description here

有人知道如何让初始的空灰色图像按钮在启动时或“DriveDroid”图像按钮上不出现吗?

我尝试将图像按钮的初始背景设置为透明(使用背景颜色 = #00000000),但这似乎在我的 DriveDroid 按钮上创建了一个透明图像按钮,因此我的 DriveDroid 的 onClick 监听器不再工作。

以下是我初始化回收器 View 的方法:

public class MyFragment extends Fragment {

private GridLayoutManager lLayout;



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.my_fragment, container, false);


    // Create an empty list to initialize the adapter (or else get nullPointerException error)
    List<ItemObject> myList = new ArrayList<ItemObject>();


    lLayout = new GridLayoutManager(getActivity(), 4, GridLayoutManager.HORIZONTAL, false);

    RecyclerView rView = (RecyclerView)view.findViewById(R.id.recycler_view);

    rView.setHasFixedSize(true);
    rView.setLayoutManager(lLayout);

    RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(getActivity(),myList);
    rView.setAdapter(rcAdapter);


    return view;
}

这是我的布局 my_fragment

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:layout_margin="5dp"
    android:id="@+id/my_fragment"
    >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="horizontal" />


            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/new_app_button"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/new_app_name"
                android:layout_below="@+id/new_app_button"
                android:layout_alignStart="@+id/new_app_button"
                android:layout_alignLeft="@+id/new_app_button"
                android:gravity="center"
                />

</RelativeLayout>

以下是我如何更改布局以使其正常工作的方法(如果对其他人有帮助的话)!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:layout_marginTop="15dp"
    android:id="@+id/my_fragment"
    >


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scrollbars="horizontal" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/new_app_button"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/new_app_name"
        android:gravity="center"
        android:layout_below="@+id/new_app_button"

        />


</RelativeLayout>


</LinearLayout>

最佳答案

正如评论中提到的,您的控件彼此重叠,您应该将它们包装在线性布局中或添加属性以使它们相对于彼此定位。

关于灰色按钮,这应该与“new_app_button”相关

关于java - 使用 RecyclerView 和 GridLayoutManager 向布局添加动态按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150455/

相关文章:

android - 更改在android中单击的按钮的图库 View

ios - 自定义返回 UIBarButton 与原始标题相同?

java - OSGi 中的动态类加载

java - 如何维护 JSONObject 的顺序

java - Android 处理日历的按钮

android - 如何让用户在 Android 应用内购买中反复购买相同的产品

html - 按钮超链接问题 HTML/CSS

java - Quartz 在特定时间启动并以特定时间间隔运行

Java - 运行方法中的 Thread.sleep

android - 什么是 android GestureDetector 长按超时?