0

My Gridview items are behaving pretty strange, if I scroll down and then up again some Items are smaller than the others, you'll see in the image.

griditem

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    android:background="#fff"
    android:padding="15dp"
    android:id="@+id/ll1">
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:visibility="visible">

    <TextView
        android:id="@+id/Fach"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="@color/colorAccent"
        android:text="Fach"
        android:textSize="13sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/Raum"
        android:textColor="@color/colorAccent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="5dp"
        android:gravity="center"
        android:text="Seminarraum" />
</LinearLayout>

Custom Adapter

    private class ViewHolder {
        TextView tvFach, tvRaum;
        LinearLayout ll;
    }

    public ImageAdapter(Context context, ArrayList<StundenEigenschaften> mobileValues) {
        this.context = context;
        this.mobileValues = mobileValues;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.stundenplan_griditem, null);

            holder = new ViewHolder();
            convertView.setTag(holder);

            // set value into textview
            holder.tvFach = (TextView) convertView.findViewById(R.id.Fach);
            holder.ll = (LinearLayout) convertView.findViewById(R.id.ll);
            holder.tvRaum = (TextView) convertView.findViewById(R.id.Raum);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvRaum.setText(String.valueOf(position));

        Log.e("Val", mobileValues.get(position).getFach() + "\n" + mobileValues.get(position).getRaum());
        if (!mobileValues.get(position).isStunde()) {
            holder.tvFach.setText(mobileValues.get(position).getFach());
            holder.tvRaum.setText(String.valueOf(mobileValues.get(position).getRaum() + "---" + String.valueOf(position)));
            for (Faecher f : fächerAll) {
                if (f.getFach().equals(mobileValues.get(position).getFach())) {
                    String[] splitted = f.getColor().split("\\s+");
                    holder.ll.setBackgroundColor(Color.rgb(Integer.valueOf(splitted[0]), Integer.valueOf(splitted[1]), Integer.valueOf(splitted[2])));
                }
            }
            holder.ll.setVisibility(View.VISIBLE);
        } else {
            //holder.ll.setVisibility(View.INVISIBLE);
        }


        return convertView;
    }

    @Override
    public int getCount() {
        return mobileValues.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}

The image how it looks after scrolling down and up...: enter image description here

Ribisl
  • 80
  • 1
  • 6
  • What is set in two first item before you scroling? Its a "Fach 1" and "Fach 2"? – M. Wiśnicki Sep 14 '16 at 16:10
  • 1
    @Ribisl, I know this isn't the exact answer you are looking for but Please don't use ListView. Use RecyclerView instead which is recommended by Google. In recyclerView you can use the GridLayoutManager with 5 columns given in your example. http://inducesmile.com/android/android-gridlayoutmanager-with-recyclerview-in-material-design/ – Adithya Upadhya Sep 14 '16 at 16:11
  • @mwisnicki Yes, the same just a bit higher – Ribisl Sep 14 '16 at 16:12
  • @oathkeeper what is the advantage of a RecyclerView? – Ribisl Sep 14 '16 at 16:14
  • @Ribisl , RecyclerView is more flexible and allows more customization. I would certainly recommend that strongly. Check this http://stackoverflow.com/questions/28525112/android-recyclerview-vs-listview-with-viewholder – Adithya Upadhya Sep 14 '16 at 16:16

2 Answers2

1
       private class ViewHolder {
        TextView tvFach, tvRaum;
        LinearLayout ll;
    }

    public ImageAdapter(Context context, ArrayList<StundenEigenschaften> mobileValues) {
        this.context = context;
        this.mobileValues = mobileValues;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater vi = (LayoutInflater) getActivity().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.stundenplan_griditem, null);

            holder = new ViewHolder();
            convertView.setTag(holder);

            // set value into textview
            holder.tvFach = (TextView) convertView.findViewById(R.id.Fach);
            holder.ll = (LinearLayout) convertView.findViewById(R.id.ll);
            holder.tvRaum = (TextView) convertView.findViewById(R.id.Raum);


        holder.tvRaum.setText(String.valueOf(position));

        Log.e("Val", mobileValues.get(position).getFach() + "\n" + mobileValues.get(position).getRaum());
        if (!mobileValues.get(position).isStunde()) {
            holder.tvFach.setText(mobileValues.get(position).getFach());
            holder.tvRaum.setText(String.valueOf(mobileValues.get(position).getRaum() + "---" + String.valueOf(position)));
            for (Faecher f : fächerAll) {
                if (f.getFach().equals(mobileValues.get(position).getFach())) {
                    String[] splitted = f.getColor().split("\\s+");
                    holder.ll.setBackgroundColor(Color.rgb(Integer.valueOf(splitted[0]), Integer.valueOf(splitted[1]), Integer.valueOf(splitted[2])));
                }
            }
            holder.ll.setVisibility(View.VISIBLE);
        } else {
            //holder.ll.setVisibility(View.INVISIBLE);
        }


        return convertView;
    }

    @Override
    public int getCount() {
        return mobileValues.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

}

Try with this code

M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28
  • this didn't solve my problem, but I will try it now with a recycler view, but thank you anyways – Ribisl Sep 14 '16 at 16:21
  • I had a similar problem , but my item disappeared . I solved it just by not checking the item in my adapter. RecyclerViewer is recommended as someone mentioned above . – M. Wiśnicki Sep 14 '16 at 16:24
1

In the grid item root linear layout, you have given height as wrap content.

Due to this the larger item are wrapping up the content.

Define the size of the item in dp. Then all the items will have same size.

For the larger texts - You can check if the item text length exceeds a limit, you can decrease the text size of that item. By doing that all the items will be even and all text will be visible.

Hope it helps.

Vishal Puri
  • 823
  • 8
  • 15