- 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/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!