Emily Di-Lee Emily Di-Lee - 6 months ago 13
Java Question

buttons don't show up or remove each other in GridLayout

Here is my problem: When some of the images get placed in the GridLayout the other app images around them disappear. I've noticed that the image that seem to "push out" the other images from the view have really big IMAGES

But still some of my images get removed from the GridLayout when certain images are retrieved (as if the new image takes up too much space).

I've tried getting rid of this error, but I can't seem to figure out how, because I am initializing my adapter and my RecyclerView in onCreateView.

EDIT:

Here is the xml for how the buttons are added to the gridlayout:

<?xml version="1.0" encoding="utf-8"?>

<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:id="@+id/home_fragment"
>


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

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


<ImageButton
android:layout_width="90dp"
android:layout_height="90dp"
android:id="@+id/new_button"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/new_app_name"
android:layout_below="@+id/new_button"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
/>


</RelativeLayout>
</LinearLayout>


Here is everything going on in HomeFragment regarding button creation initialization.
RecyclerViewAdapter and GridLayout are initialized:

RecyclerViewAdapter rcAdapter;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

List<ItemObject> myList = new ArrayList<>();
rcAdapter = new RecyclerViewAdapter(getActivity(),myList);


}
private GridLayoutManager lLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment, container, false);

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

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

rView.setHasFixedSize(true);
rView.setLayoutManager(lLayout);
rView.setAdapter(rcAdapter);
return view;
}


Here is where another activity passes the drawable and string to create the button (this is in HomeFragment)

public void createButton (Drawable d, String Name){
rcAdapter.addItem(new ItemObject(Name, d));
}


Here is RecyclerViewAdapter (rcAdapter)

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders>{

private List<ItemObject> itemList;
Context context;



public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {


this.itemList = itemList;
this.context = context;
}

@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_fragment, parent,false);
RecyclerViewHolders holder = new RecyclerViewHolders(layoutView);

return holder;
}

@Override
public void onBindViewHolder(RecyclerViewHolders holder, int position) {
holder.Name.setText(itemList.get(position).getName());
holder.btn.setImageDrawable(itemList.get(position).getPhoto());
holder.btn.setBackgroundColor(Color.TRANSPARENT);
}

@Override
public int getItemCount() {
return this.itemList.size();
}


public void addItem(ItemObject item) {
itemList.add(item);
notifyItemInserted(itemList.size() - 1);
}

}

Answer

I will outline some areas in the XML that may be causing you some problems. First, you need an XML layout file that looks something like the following that will hold your top-level layout. This will be in its own file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

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

Notice that the RecyclerView stands alone within the LinearLayout. You will next need a separate XML file that will hold the layout for the ImageButton and TextView for each cell of your layout. This will fit into each cell of the grid in your RecyclerView. I used android:scaleType="centerCrop" to fit large images into the ImageButton.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp">

    <ImageButton
        android:id="@+id/new_app_button"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_centerHorizontal="true"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop" />

    <TextView
        android:id="@+id/new_app_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/new_app_button"
        android:layout_centerInParent="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" />

</RelativeLayout>

onCreateViewHolder() of RecyclerViewHolder will look like the following assuming "grid_cell.xml" is the name of the XML file above.

@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.grid_cell, parent, false);
    RecyclerViewHolders holder = new RecyclerViewHolders(layoutView);
    return holder;
}

Give this a try. I think that everything else is OK. I hope it helps you.

Comments