android - 使用 Android-DraggableGridViewPager 居中 ViewGroup 视图
问题描述
我首先要感谢 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>
我正在使用它来创建一个带有可拖动布局的简单网格视图,但是如果我减小该 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);
解决方案
推荐阅读
- html - 在 CSS/HTML 中居中图像网格的方法?
- setuid - 尝试使用 checkinstall 或 porg 了解 LD_PRELOAD 和 SUID/SGID
- c# - 使用 MVVM 从 ViewModel 中的窗口资源中选择 DataTemplate
- ruby-on-rails - Factory Bot 与命名空间模型故障的关联
- sql - ORACLE SQL检查表的行号是否为n,然后执行join
- javascript - 在Javascript中独立定位while循环的每个元素?
- git - 使用“git revert”和冲突。这是怎么发生的?
- jquery - 提交前验证字段
- python - 无法在本地使用 PySpark 从 AWS S3 路径读取 Avro 文件
- postman - 无法访问 Postman 预请求脚本中的 XML 响应