gpt4 book ai didi

Android 以编程方式添加带有 ConstraintLayout 的 ImageView

转载 作者:行者123 更新时间:2023-12-02 17:31:37 26 4
gpt4 key购买 nike

我想使用 ConstraintLayout 以编程方式将多个 ImageView 插入到 RecyclerView 中。

要使用水平偏差以等距方式插入图像,每个图像的值计算如下:

float biasedValue = (1f / (flightPlanWeather.weather.size () + 1f)) * (i + 1f);

代码如下

public class FlightPlanWeatherRecyclerAdapter extends RecyclerView.Adapter<FlightPlanWeatherRecyclerAdapter.MyViewHolder> {
private static final String TAG = FlightPlanWeatherRecyclerAdapter.class.getName();
private List<FlightPlanWeather> dataSet;
private LayoutInflater mInflater;
Context mContext;


// Provide a suitable constructor (depends on the kind of dataset)
// data is passed into the constructor
public FlightPlanWeatherRecyclerAdapter(Context context, List<FlightPlanWeather> data) {
this.mInflater = LayoutInflater.from(context);
this.dataSet = data;
this.mContext = context;
}

// inflates the row layout from xml when needed
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.row_item_flightplan_weather, parent, false);
return new MyViewHolder(view);
}


@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.bind(dataSet.get(position));
FlightPlanWeather flightPlanWeather = dataSet.get(position);

holder.from_img.setImageResource(flightPlanWeather.from.getImg_resource());
holder.from_label.setText(flightPlanWeather.from.getLabel());

holder.to_img.setImageResource(flightPlanWeather.to.getImg_resource());
holder.to_label.setText(flightPlanWeather.to.getLabel());


int dimensionInPixel = 256;

ConstraintLayout.LayoutParams lp =
new ConstraintLayout.LayoutParams(/*ConstraintLayout.LayoutParams.WRAP_CONTENT,*/ dimensionInPixel,
/*ConstraintLayout.LayoutParams.WRAP_CONTENT*/ dimensionInPixel);



//weather
for(int i=0; i<flightPlanWeather.weather.size(); i++ ){
ConstraintSet constraintSet = new ConstraintSet();

holder.mConstraintSetList.add(constraintSet);
ImageView imageView = new ImageView(mContext);
imageView.setId(View.generateViewId());
imageView.setImageResource(flightPlanWeather.weather.get( i ));
imageView.setLayoutParams(lp);
imageView.requestLayout();
holder.weather.add(imageView);
holder.mConstraintLayout.addView( imageView/*, lp */);

//}
constraintSet.clone(holder.mConstraintLayout);

float biasedValue = (1f/ (flightPlanWeather.weather.size()+1f)) * (i+1f);
Log.d(TAG, Float.toString( biasedValue ) +"i:"+ Integer.toString( i )+"size:"+ flightPlanWeather.weather.size());
constraintSet.setHorizontalBias(imageView.getId(), biasedValue);
constraintSet.connect(imageView.getId(), ConstraintSet.START, holder.mConstraintLayout.getId(), ConstraintSet.START, 0);
constraintSet.connect(imageView.getId(), ConstraintSet.END, holder.mConstraintLayout.getId(), ConstraintSet.END, 0);
constraintSet.connect(imageView.getId(), ConstraintSet.TOP, holder.mConstraintLayout.getId(), ConstraintSet.TOP, 0);
constraintSet.connect(imageView.getId(), ConstraintSet.BOTTOM, holder.mConstraintLayout.getId(), ConstraintSet.BOTTOM, 0);
constraintSet.applyTo(holder.mConstraintLayout);
holder.mConstraintLayout.requestLayout();
}

}

// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return dataSet == null ? 0 : dataSet.size();
}


// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
// Static inner class to initialize the views of rows
static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

TextView from_label;
TextView to_label;
ImageView from_img;
ImageView to_img;
ImageView line;
ConstraintLayout mConstraintLayout;

List<ConstraintSet> mConstraintSetList = new ArrayList<>( );
List<ImageView> weather = new ArrayList<>( );

public MyViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);

mConstraintLayout = (ConstraintLayout) itemView.findViewById(R.id.mainConstraintLayout);
from_label = (TextView) itemView.findViewById(R.id.from_label);
to_label = (TextView) itemView.findViewById(R.id.to_label);
from_img = (ImageView) itemView.findViewById(R.id.from_img);
to_img = (ImageView) itemView.findViewById(R.id.to_img);
line = (ImageView) itemView.findViewById(R.id.line);



}

public void bind(Object object){


}

@Override
public void onClick(View view) {
Log.d("onclick", "onClick " + getLayoutPosition() + " " /*+ /*item.getText()*/);
}
}

}

问题是天气的水平偏差无法正常工作 screenshot

View 应该是这样的: screenshot

最佳答案

问题在于您正在为每个 View 重用布局参数。由于布局参​​数托管布局信息,因此您创建并设置布局信息的每个新 ImageView 都会清除先前的布局信息。

为每个ImageView创建一组新的布局参数,它应该可以正常工作。将以下内容移至循环中:

ConstraintLayout.LayoutParams lp =
new ConstraintLayout.LayoutParams(/*ConstraintLayout.LayoutParams.WRAP_CONTENT,*/ dimensionInPixel,
/*ConstraintLayout.LayoutParams.WRAP_CONTENT*/ dimensionInPixel);

关于Android 以编程方式添加带有 ConstraintLayout 的 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59355357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com