【问题标题】:Using RecyclerView with GridLayoutManager to add dynamic buttons to a layout使用带有 GridLayoutManager 的 RecyclerView 向布局添加动态按钮
【发布时间】:2016-09-06 03:15:28
【问题描述】:

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

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

然后当用户执行操作并创建我想要的真实按钮时,正方形仍然存在于我刚刚创建的图像按钮上,如下面的新图像所示。

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

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

这是我初始化回收站视图的方法:

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>

【问题讨论】:

  • 在您的 my_fragment 中,3 个元素(RecyclerView、ImageButton、TextView)相互重叠,要么将它们放在 LinearLayout 中,要么将它们放在彼此之后。 id 为“new_app_button”的 ImageButton 是出现的灰色图像按钮,更改其背景,它应该可以工作。
  • 谢谢!我添加了一个线性布局来让它工作!您可以回答我的问题,如果您愿意,我会对其进行评价。另外,如果您能回答,我还有一个问题要发布:stackoverflow.com/questions/37150975/…
  • 我已经添加了答案,请标记为已接受的答案。关于另一个问题,我会检查一下。

标签: java android button dynamic android-recyclerview


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    相关资源
    最近更新 更多