我正在开发照片编辑应用程序,其中我在回收站视图中显示贴纸。 我正在动态设置膨胀视图的高度和宽度,如代码所示,它在除了nougat之外的所有android os版本中工作正常。在牛轧糖中,parent.getHeight()返回0.我不想设置高度和宽静,我不知道我做错了什么
package com.demo.makefunnyface.Adapters;
import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.demo.makefunnyface.R;
import com.demo.makefunnyface.Utils.PojoSticker;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.util.ArrayList;
import java.util.List;
/**
* Created by hp on 9/5/2017.
*/
public class AdapterSticker extends RecyclerView.Adapter<AdapterSticker.MyHolder> {
Context context;
List<PojoSticker> stickersList = new ArrayList<>();
OnSelectListener onSelectListener;
DisplayImageOptions displayImageOptions;
ImageLoader imageLoader;
ImageLoaderConfiguration imageLoaderConfiguration;
public AdapterSticker(Context context, List<PojoSticker> stickersList) {
this.context = context;
this.stickersList.addAll(stickersList);
displayImageOptions = new DisplayImageOptions.Builder().cacheOnDisk(true).cacheInMemory(true).build();
imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(context).defaultDisplayImageOptions(displayImageOptions).build();
imageLoader = ImageLoader.getInstance();
imageLoader.init(imageLoaderConfiguration);
}
@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_sticker_layout, parent, false);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) view.getLayoutParams();
layoutParams.height = parent.getHeight();
layoutParams.width = parent.getHeight();
//nougat show zero in log
Log.e("height", "" + parent.getHeight());
view.setLayoutParams(layoutParams);
return new MyHolder(view);
}
@Override
public void onBindViewHolder(MyHolder holder, int position) {
imageLoader.displayImage(stickersList.get(position).getPath(), holder.imgSticker);
}
@Override
public int getItemCount() {
return stickersList.size();
}
class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView imgSticker;
public MyHolder(View itemView) {
super(itemView);
imgSticker = (ImageView) itemView.findViewById(R.id.imgSticker);
imgSticker.setOnClickListener(this);
}
@Override
public void onClick(View v) {
PojoSticker pojoSticker = stickersList.get(getAdapterPosition());
if (onSelectListener != null) {
onSelectListener.onSelected(pojoSticker);
}
}
}
public interface OnSelectListener {
void onSelected(PojoSticker pojoSticker);
}
public void setSelectListener(OnSelectListener onSelectListener) {
this.onSelectListener = onSelectListener;
}
public int dpToPx(int dp) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int tmp = (int) ((dp * displayMetrics.density) + 0.5);
Log.e("tmp", "" + tmp);
return tmp;
}
}
答案 0 :(得分:2)
问题不在于onCreateViewHolder,是将数据添加到recyclerview(触发对象创建)的地方,因为在onCreate上你的视图没有完成测量,你必须计算它延迟,例如onResume。
答案 1 :(得分:0)
这是因为在创建时您的视图不存在。试试你的onBindViewHolder
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Log.e("height", "" + holder.itemView.getHeight());
}
答案 2 :(得分:0)
为我工作
尝试将RecyclerView放在XML里面没有任何其他视图的布局中,例如以下示例:
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<fr.semantic.ecare.app.android.components.utils.CustomTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center"
android:id="@+id/collect_period" />
<!-- IMPORTANT TO DO -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view_list"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</android.support.v7.widget.RecyclerView>
</RelativeLayout> .....
.........
</LinearLayout>
以及您的活动或片段中:
@Override
public CollectGlycaemiaViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.YOUR_XML_LAYOUT, parent, false);
int height = parent.getMeasuredHeight() / 3 ; // if you want to devise the height exactly
int width = parent.getMeasuredWidth();
view.setLayoutParams(new RecyclerView.LayoutParams(width, height));
return new CollectGlycaemiaViewHolder(view);
}
希望对将来的开发人员有帮助