- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在做一个已经有一些代码的项目。我想更改我的适配器项的显示方式。
现在每行有 2 个项目。 我想做的是每行只有 1 个项目。
每行显示 2 个项目,而我只需要显示 1 个即可。
这是xmls上的代码
项目 xml
<com.inthessaloniki.cityguide.view.SelectorRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_poi_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@drawable/selector_clickable_item_bg_inverse">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/fragment_poi_list_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"/>
</RelativeLayout>
列表 xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true">
<LinearLayout
android:id="@+id/container_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/fragment_poi_list_recycler"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scrollbars="vertical"
android:background="@color/global_bg_front" />
<com.google.android.gms.ads.AdView
android:id="@+id/fragment_poi_list_adview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:adUnitId="@string/admob_unit_id_poi_list"
app:adSize="BANNER" />
</LinearLayout>
<include
layout="@layout/placeholder_progress"
android:id="@+id/container_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<include
layout="@layout/placeholder_offline"
android:id="@+id/container_offline"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<include
layout="@layout/placeholder_empty"
android:id="@+id/container_empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
ListAdapter代码
public class SubCategoryListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
public static final int VIEW_TYPE_POI = 1;
public static final int VIEW_TYPE_IMAGE_FOOTER = 2;
public static final int VIEW_TYPE_FOOTER = 3;
private List<CategoryModel> mPoiList;
private List<Object> mFooterList;
private PoiViewHolder.OnItemClickListener mListener;
private int mGridSpanCount;
private Location mLocation;
private boolean mAnimationEnabled = true;
private int mAnimationPosition = -1;
private ImageLoader mImageLoader = ImageLoader.getInstance();
private DisplayImageOptions mDisplayImageOptions;
private ImageLoadingListener mImageLoadingListener;
//Ares
private long mCategory;
public SubCategoryListAdapter(List<CategoryModel> poiList, List<Object> footerList, PoiViewHolder.OnItemClickListener listener, int gridSpanCount, Location location, long category)
{
mPoiList = poiList;
mFooterList = footerList;
mListener = listener;
mGridSpanCount = gridSpanCount;
mLocation = location;
//Ares
mCategory = category;
// image caching options
mDisplayImageOptions = new DisplayImageOptions.Builder()
.showImageOnLoading(android.R.color.transparent)
.showImageForEmptyUri(R.drawable.placeholder_photo)
.showImageOnFail(R.drawable.placeholder_photo)
.cacheInMemory(true)
.cacheOnDisk(true)
.displayer(new SimpleBitmapDisplayer())
.build();
mImageLoadingListener = new AnimateImageLoadingListener();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
// inflate view and create view holder
View view;
if(viewType== VIEW_TYPE_POI)
{
if(mCategory == -4) {
view = inflater.inflate(R.layout.fragment_poi_list_item_home, parent, false);
}
else
{
view = inflater.inflate(R.layout.fragment_poi_list_item, parent, false);
}
return new PoiViewHolder(view, mListener, mImageLoader, mDisplayImageOptions, mImageLoadingListener, mCategory);
}
else if(viewType==VIEW_TYPE_IMAGE_FOOTER)
{
view = inflater.inflate(R.layout.footer_image_layout, parent, false);
return new ImageFooterViewHolder(view, mListener);
}
else if(viewType==VIEW_TYPE_FOOTER)
{
view = inflater.inflate(R.layout.fragment_poi_list_footer, parent, false);
return new FooterViewHolder(view);
}
else
{
throw new RuntimeException("There is no view type that matches the type " + viewType);
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position)
{
// bind data
if(viewHolder instanceof PoiViewHolder)
{
// entity
CategoryModel poi = mPoiList.get(getPoiPosition(position));
// render view
if(poi != null)
{
((PoiViewHolder) viewHolder).bindData(poi, mLocation, mCategory);
}
}
else if(viewHolder instanceof FooterViewHolder)
{
// entity
Object object = mFooterList.get(getFooterPosition(position));
// render view
if(object != null)
{
((FooterViewHolder) viewHolder).bindData(object);
}
}
// set item margins
setItemMargins(viewHolder.itemView, position);
// set animation
setAnimation(viewHolder.itemView, position);
}
@Override
public int getItemCount()
{
int size = 0;
if(mPoiList !=null) size += mPoiList.size();
if(mFooterList!=null) size += mFooterList.size();
if(mFooterList!=null) size += 1; //ImageFooter
return size;
}
@Override
public int getItemViewType(int position)
{
int pois = mPoiList.size();
int footers = mFooterList.size();
int imageFooter = 1;
if(position < pois) return VIEW_TYPE_POI;
else if(position < pois+imageFooter) return VIEW_TYPE_IMAGE_FOOTER;
else if(position < pois+imageFooter+footers) return VIEW_TYPE_FOOTER;
else return -1;
}
public int getPoiCount()
{
if(mPoiList !=null) return mPoiList.size();
return 0;
}
public int getFooterCount()
{
if(mFooterList!=null) return mFooterList.size();
return 0;
}
public int getPoiPosition(int recyclerPosition)
{
return recyclerPosition;
}
public int getFooterPosition(int recyclerPosition)
{
return recyclerPosition - getPoiCount();
}
public int getRecyclerPositionByPoi(int poiPosition)
{
return poiPosition;
}
public int getRecyclerPositionByFooter(int footerPosition)
{
return footerPosition + getPoiCount();
}
public void refill(List<CategoryModel> poiList, List<Object> footerList, PoiViewHolder.OnItemClickListener listener, int gridSpanCount, Location location)
{
mPoiList = poiList;
mFooterList = footerList;
mListener = listener;
mGridSpanCount = gridSpanCount;
mLocation = location;
notifyDataSetChanged();
}
public void stop()
{
}
public void setLocation(Location location)
{
mLocation = location;
}
public void setAnimationEnabled(boolean animationEnabled)
{
mAnimationEnabled = animationEnabled;
}
private void setAnimation(final View view, int position)
{
if(mAnimationEnabled && position>mAnimationPosition)
{
view.setScaleX(0f);
view.setScaleY(0f);
view.animate()
.scaleX(1f)
.scaleY(1f)
.setDuration(300)
.setInterpolator(new DecelerateInterpolator());
mAnimationPosition = position;
}
}
private void setItemMargins(View view, int position)
{
int height = 0;
if(mCategory != -4)
height = (int) CityGuideApplication.getContext().getResources().getDimension(R.dimen.fragment_poi_list_recycler_item_size);
int marginTop = 0;
if(position<mGridSpanCount)
{
TypedArray a = CityGuideApplication.getContext().obtainStyledAttributes(null, new int[]{android.R.attr.actionBarSize}, 0, 0);
marginTop = (int) a.getDimension(0, 0);
a.recycle();
if(mCategory != -4)
height += marginTop;
}
ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
marginParams.setMargins(0, marginTop, 0, 0);
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
if(mCategory != -4)
layoutParams.height = height;
}
public static final class PoiViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
private TextView nameTextView;
private TextView distanceTextView;
private ImageView imageView;
private OnItemClickListener mListener;
private ImageLoader mImageLoader;
private DisplayImageOptions mDisplayImageOptions;
private ImageLoadingListener mImageLoadingListener;
public interface OnItemClickListener
{
public void onItemClick(View view, int position, long id, int viewType);
}
public PoiViewHolder(View itemView, OnItemClickListener listener, ImageLoader imageLoader, DisplayImageOptions displayImageOptions, ImageLoadingListener imageLoadingListener, long category)
{
super(itemView);
mListener = listener;
mImageLoader = imageLoader;
mDisplayImageOptions = displayImageOptions;
mImageLoadingListener = imageLoadingListener;
// set listener
itemView.setOnClickListener(this);
// find views
if(category == -4)
{
Log.d("Category Status: ", String.valueOf(category));
}
else
{
nameTextView = (TextView) itemView.findViewById(R.id.fragment_poi_list_item_name);
distanceTextView = (TextView) itemView.findViewById(R.id.fragment_poi_list_item_distance);
distanceTextView.setVisibility(View.GONE);
}
imageView = (ImageView) itemView.findViewById(R.id.fragment_poi_list_item_image);
}
@Override
public void onClick(View view)
{
mListener.onItemClick(view, getPosition(), getItemId(), getItemViewType());
}
public void bindData(CategoryModel subCategory, Location location, long category)
{
if(category == -4)
{
}
else
{
nameTextView.setText(subCategory.getName());
}
mImageLoader.displayImage(subCategory.getImage2(), imageView, mDisplayImageOptions, mImageLoadingListener);
// if(location!=null)
// {
// LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude());
// LatLng poiLocation = new LatLng(poi.getLatitude(), poi.getLongitude());
// String distance = LocationUtility.getDistanceString(LocationUtility.getDistance(myLocation, poiLocation), LocationUtility.isMetricSystem());
// distanceTextView.setText(distance);
// distanceTextView.setVisibility(View.VISIBLE);
// }
// else
// {
// distanceTextView.setVisibility(View.GONE);
// }
}
}
public static final class ImageFooterViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private PoiViewHolder.OnItemClickListener mListener;
public interface OnItemClickListener {
public void onItemClick(View view, int position, long id, int viewType);
}
public ImageFooterViewHolder(View itemView, PoiViewHolder.OnItemClickListener listener) {
super(itemView);
mListener = listener;
// set listener
itemView.setOnClickListener(this);
}
@Override
public void onClick(View view) {
mListener.onItemClick(view, getPosition(), getItemId(), getItemViewType());
}
public void bindData(PoiModel poi, Location location) {
}
}
public static final class FooterViewHolder extends RecyclerView.ViewHolder
{
public FooterViewHolder(View itemView)
{
super(itemView);
}
public void bindData(Object object)
{
// do nothing
}
}
}
砂砾布局
public class GridSpacingItemDecoration extends RecyclerView.ItemDecoration
{
private int mSpacing;
public GridSpacingItemDecoration(int spacingPixelSize)
{
mSpacing = spacingPixelSize;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView recyclerView, RecyclerView.State state)
{
super.getItemOffsets(outRect, view, recyclerView, state);
int position = recyclerView.getChildPosition(view);
int spanCount = getSpanCount(recyclerView);
int itemCount = recyclerView.getAdapter().getItemCount();
// top offset
if(position < spanCount)
{
outRect.top = mSpacing;
}
else
{
outRect.top = mSpacing/2;
}
// bottom offset
if(itemCount%spanCount == 0 && position >= itemCount - spanCount)
{
outRect.bottom = mSpacing;
}
else if(itemCount%spanCount != 0 && position >= itemCount - itemCount%spanCount)
{
outRect.bottom = mSpacing;
}
else
{
outRect.bottom = mSpacing/2;
}
// left offset
if(position%spanCount == 0)
{
outRect.left = mSpacing;
}
else
{
outRect.left = mSpacing/2;
}
// right offset
if(position%spanCount == spanCount-1)
{
outRect.right = mSpacing;
}
else
{
outRect.right = mSpacing/2;
}
}
private int getSpanCount(RecyclerView recyclerView)
{
if(recyclerView.getLayoutManager() instanceof GridLayoutManager)
{
GridLayoutManager gridLayoutManager = (GridLayoutManager) recyclerView.getLayoutManager();
return gridLayoutManager.getSpanCount();
}
else
{
throw new IllegalStateException(this.getClass().getSimpleName() + " can only be used with a " + GridLayoutManager.class.getSimpleName());
}
}
}
我不认为问题出在 XML 文件中。它必须在代码中的某处。我只是无法追踪并修改它...:S
我已经搜索了几个小时。
提前致谢!!!
最佳答案
您还没有将设置 LayoutManager
的代码发布到您的 RecyclerView
。
您似乎正在使用 GridLayoutManager
:
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(context, 2);
myRecyclerView.setLayoutManager(mLayoutManager);
尝试将其更改为 LinearLayoutManager
:
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(context);
myRecyclerView.setLayoutManager(mLayoutManager);
关于android - RecyclerView.Adapter 在一行中显示 2 个项目(如何更改),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38171737/
我正在制作一个 android 应用程序,它允许用户在 editText 中输入关键字,当他们点击提交时,下面的 recyclerview 将显示来自 API 请求的结果。 我的 recyclerVi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我想在我的 Svelte Kit 应用程序中使用 @sveltejs/adapter-static(想把它变成一个 SPA)。 我使用 npm i @sveltejs/adapter-static 安
我有一个简单的表单,在提交重定向到 AuthController 内的 processAction 之后,在这个 action 中我想要创建一个简单的 table bar。 已编辑: 引用Zend f
当我使用 ListView 时,我扩展了 ArrayAdapter 并将数据(一个列表)存储在 ArrayAdapter 中,因此当数据更改时,我调用: adapter.add(item).; 还有另
我有一个使用 Webpack 捆绑的 commonjs 浏览器应用程序( typescript )。它使用 webrtc,所以我想使用 webrtc-adapter package from npm
在 “DesignPatterns: Elements of Reusable Object-Oriented Software” 一书中谈到 C++ 实现 Adapter 模式时,它是这样的这个:
只需要帮助理解 ZF2 中的一些简单数据库查询。在 ZF1 中,我有这样简单的方法: public function recordset() { // listing of all records $
当使用Spring的Java配置时,你一定会看到像WebMvcConfigurerAdapter这样的类& HandlerInterceptorAdapter它们实现单个接口(interface),并
我将 doctrine2 与 ZF2 一起使用,我的一些库与 Zend\Db\Adapter\Adapter 一起使用,其他的与 doctrine2 一起使用。现在,他们两次连接到数据库。是否可以在原
过去几天我一直在工作灯上工作,现在正在尝试制作连接到数据库的应用程序。以下是截图: 1) Worklight\server\conf\worklight.properties 2) mySQLAdap
几年前,我在 this tutorial 之后开始学习 Zend 框架。 .在那里,它显示映射器是使用 Zend\Db\Adapter\Adapter 创建的。类来获取数据库连接,这就是我使用数据库的
我正在学习 RecyclerView 并在 developer 中的站点 Adapter 类扩展 RecyclerView.Adapter 。实现显示: public class MyAdapter
查看文档:http://docs.spring.io/spring-integration/reference/html/ip.html#tcp-adapters我知道适配器仅用于单向通信。但是,这里
当我移动到另一个 fragment 时,我如何恢复分页适配器项状态?我试过下面的文章,但没有用。 https://medium.com/@florina.muntenescu private fun
我正在使用 capistrano 并收到此错误消息: Please install the pg adapter: `gem install activerecord-pg-adapter` (can
我正在尝试在本地主机上安装 Magento 2,当我想连接数据库时出现错误。 错误是: “Magento\Framework\DB\Adapter\Pdo\MysqlFactory”生成的源类“\Ma
我的 Rails 服务器通过正常的开发脚本运行良好,我计划使用 Netbeans for Rails。当我使用 Netbeans 启动我现有的项目并重新启动我的 Webrick 服务器时,它显示错误
运行 Ruby on Rails (RoR) 应用程序或使用 ActiveRecord 框架的 Ruby 代码,您会收到错误消息: Please install the postgresql adap
我在运行 Redmine2.3-stable 时遇到这个错误,随后无法成功安装 mysql2 适配器。 这是错误: Please install the mysql2 adapter: `ge
我是一名优秀的程序员,十分优秀!