Центрировать представления ViewGroup с помощью Android-DraggableGridViewPager - PullRequest
0 голосов
/ 26 сентября 2019

Прежде всего я хочу поблагодарить zzhouj, но я пытаюсь использовать его Android-DraggableGridViewPager с некоторыми ошибками.это его класс:

Android-DraggableGridViewPager RAW

Это мой макет:

<?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>

Я использую его для создания простогоgridview с перетаскиваемым макетом, но если я уменьшу размер этой ViewGroup, элемент не будет оставаться в центре, потому что элементы идут слева.

У вас есть идеи?Заранее спасибо!

РЕДАКТИРОВАТЬ

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();
    }
}

РЕДАКТИРОВАТЬ

Я установил адаптер в MainActivity:

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