使用RecyclerView和GridLayoutManager将动态按钮添加到布局

时间:2016-05-10 23:17:17

标签: java android button dynamic android-recyclerview

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

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

enter image description here

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

enter image description here

是否有人知道我如何获得初始的空白灰色图像按钮,而不是在启动时或在我的“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>

1 个答案:

答案 0 :(得分:1)

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

关于灰色按钮,这应与&#34; new_app_button&#34;

相关