0

I want to thanks zzhouj first of all, but I'm trying to use his Android-DraggableGridViewPager with some errors. this is his class:

Android-DraggableGridViewPager RAW

This is my layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">

    <TextClock
        android:gravity="center"
        android:textColor="@color/colorBlack"
        android:id="@+id/text_clock"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="15"
        android:fontFamily="sans-serif-black"
        android:text="20:15"
        android:textSize="50sp"
        android:textStyle="bold" />

    <TextView
        android:gravity="center"
        android:id="@+id/text_date"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:fontFamily="sans-serif-thin"
        android:text="09/09/09"
        android:textColor="@color/colorBlack"
        android:textSize="20sp" />

    <com.kangel.hybridlauncher.adapters.AppDraggableGridViewPager
        android:foregroundGravity="center"
        android:id="@+id/draggable_grid_view_pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="75"
        android:background="@color/colorAccent"
        android:gravity="center" />

</LinearLayout>

I'm using it to create a simple gridview with draggable layout in, but if I reduce size of that ViewGroup, the element wont stay in center, because elements go on the left.

Do you have any ideas? Thanks in advance!

EDIT

public class AppArrayAdapter extends ArrayAdapter<AppObject> {

    private Context context;

    public AppArrayAdapter(@NonNull Context context, int resource) {
        super(context, resource);
        this.context = context;
    }

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

    @NonNull
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.gridview_app_layout, parent, false);
        } else {
            v = convertView;
        }

        LinearLayout layout = v.findViewById(R.id.single_item_core);
        ImageView icon = layout.findViewById(R.id.app_layout);
        TextView label = layout.findViewById(R.id.app_label);


        final AppObject appObject = this.getItem(position);
        icon.setImageDrawable(appObject.getIcon());
        label.setText(appObject.getName());

        return v;
    }


    @Override
    public void add(@Nullable AppObject object) {
        super.add(object);
        notifyDataSetChanged();
    }

    @Override
    public void addAll(@NonNull Collection<? extends AppObject> collection) {
        super.addAll(collection);
        notifyDataSetChanged();
    }

    @Override
    public void addAll(AppObject... items) {
        super.addAll(items);
        notifyDataSetChanged();
    }

    @Override
    public void insert(@Nullable AppObject object, int index) {
        super.insert(object, index);
        notifyDataSetChanged();
    }

    @Override
    public void remove(@Nullable AppObject object) {
        super.remove(object);
        notifyDataSetChanged();
    }

    @Override
    public void clear() {
        super.clear();
        notifyDataSetChanged();
    }
}

EDIT

I set adapter in MainActivity:

DraggableGridViewPager mDraggableGridViewPager = mViewAppsScroll.findViewById(R.id.draggable_grid_view_pager);
mAppAdapter = new AppArrayAdapter(this, 0);
mAppAdapter.addAll(getList());
mDraggableGridViewPager.setAdapter(mAppAdapter);
Kangel
  • 61
  • 9

0 Answers0