簡體   English   中英

使用RecyclerView和GridLayoutManager將動態按鈕添加到布局

[英]Using RecyclerView with GridLayoutManager to add dynamic buttons to a layout

我正在使用帶有gridlayoutmanager的recyclerview將動態imagebuttons添加到Fragment中的布局中。 這些按鈕是在用戶執行操作時添加的,因此它們並不是在onCreateView()期間全部創建的。

當我的應用程序啟動時,我似乎必須使用一個空的imagebutton初始化回收器視圖和適配器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>

如評論中所述,您的控件彼此重疊,應將它們包裝在線性布局中或添加屬性以使它們相對於彼此放置。

關於灰色按鈕,這應該與“ new_app_button”有關

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM