如何在RecyclerView中设置相等的行高?

我有JSON数据文件,它具有不同大小的项目和行大小显示根据JSON数据,但我想在RecyclerView相等的行大小。如何在RecyclerView中设置相等的行高?

如何实现我只发现GridLayoutManager但它不符合我的要求,有什么帮助?

public class UserMsgAdpater extends RecyclerView.Adapter<UserMsgAdpater.MyViewHolder> { 

public ArrayList<UserMessage> selected_usersList=new ArrayList<>();

private List<UserMessage> userList;

Context ctx;

TextView textview;

private SparseBooleanArray mSelectedItemsIds;

public UserMsgAdpater(Context context, List<UserMessage> data,ArrayList<UserMessage> selectedList) {

this. userList = data;

this. ctx = context;

this.selected_usersList = selectedList;

mSelectedItemsIds = new SparseBooleanArray();

}

public class MyViewHolder extends RecyclerView.ViewHolder {

public TextView userName;

public CardView cardView;

public MyViewHolder(View view) {

super(view);

userName = (TextView) view.findViewById(R.id.user_temp);

}

}

@Override

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View itemView = LayoutInflater.from(parent.getContext())

.inflate(R.layout.user_temp_list, parent, false);

return new MyViewHolder(itemView);

}

@Override

public void onBindViewHolder(MyViewHolder holder, int position)

{

holder.userName.setText(userList.get(position).getMessage());

holder.itemView

.setBackgroundColor(mSelectedItemsIds.get(position) ?Color.GRAY

: Color.TRANSPARENT);

}

public void toggleSelection(int position) {

selectView(position, !mSelectedItemsIds.get(position));

}

public void removeSelection() {

mSelectedItemsIds = new SparseBooleanArray();

notifyDataSetChanged();

}

//Put or delete selected position into SparseBooleanArray

public void selectView(int position, boolean value) {

if (value)

mSelectedItemsIds.put(position, value);

else

mSelectedItemsIds.delete(position);

notifyDataSetChanged();

}

//Get total selected count

public int getSelectedCount() {

return mSelectedItemsIds.size();

}

//Return all selected ids

public SparseBooleanArray getSelectedIds() {

return mSelectedItemsIds;

}

@Override

public int getItemCount() {

return userList.size();

}

public UserMessage get(int i) {

return userList.get(i);

}}

我的活动我正在使用此代码进行物品装饰请看看。

mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 

mRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, LinearLayoutManager.VERTICAL));

mRecyclerView.addItemDecoration(new EqualSpacingItemDecoration(16,EqualSpacingItemDecoration.VERTICAL));

回答:

您可以设置每个项目的高度,你ViewHolder构造内:

TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, mHeight); 

itemView.setLayoutParams(params);

其中mHeight是所需的项目高度的整数值。

为您的代码,它会是这样的:

private int mHeight = 100; 

public class MyViewHolder extends RecyclerView.ViewHolder {

public TextView userName;

public CardView cardView;

public MyViewHolder(View view) {

super(view);

userName = (TextView) view.findViewById(R.id.user_temp);

TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, mHeight);

view.setLayoutParams(params);

}

}

以上是 如何在RecyclerView中设置相等的行高? 的全部内容, 来源链接: utcz.com/qa/263117.html

回到顶部