Android动态布局和视图
我想展示产品颜色,比如 基于服务器响应。单击颜色时,可用大小将根据颜色显示Android动态布局和视图,android,android-layout,android-studio,Android,Android Layout,Android Studio,我想展示产品颜色,比如 基于服务器响应。单击颜色时,可用大小将根据颜色显示 最好的解决方案是使用RecylcerView 您希望布局管理器是水平的 以下是一个例子: LinearLayoutManager layoutmanager = new LinearLayoutManager(this, LinearLayoutManager.Horizontal, false) RecyclerView recyclerView = (RecyclerView) findViewById(R.id
RecylcerView
LinearLayoutManager layoutmanager = new LinearLayoutManager(this, LinearLayoutManager.Horizontal, false)
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(layoutManager)
ProductAdapter adapter = new ProductAdapter();
recyclerView.setAdapter(adapter);
ProductAdapter
,该类将扩展RecyclerView.Adapter
,该类将在RecylcerView
ProductAdapter
中,您将指定希望RecyclerView
具有的单个RowView
RecyclerView
的适配器LinearLayoutManager layoutmanager = new LinearLayoutManager(this, LinearLayoutManager.Horizontal, false)
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(layoutManager)
ProductAdapter adapter = new ProductAdapter();
recyclerView.setAdapter(adapter);
Adapter
将如下所示:
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.MyViewHolder>{
private List<Product> productList;
public class MyViewHolder extends RecyclerView.ViewHolder{
public ImageView productImage;
public TextView color;
public MyViewholder(View v){
super(view);
productImage = (ImageView) view.fincdViewById(R.id.product_image);
color = (TextView) view.fincdViewById(R.id.product_color);
}
}
public ProductAdapter(List<Product> productList){
this.productList = productList;
}
// Here below you will inflate the individual row view
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_view, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position){
holder.productImage.setImageURI(productList.get(position).getUri());
holder.color.setText(productList.get(position).getColor())
}
}
公共类ProductAdapter扩展了RecyclerView.Adapter{
私有列表产品列表;
公共类MyViewHolder扩展了RecyclerView.ViewHolder{
公众形象;产品形象;
公共文本视图颜色;
公共MyViewholder(视图v){
超级(视图);
productImage=(ImageView)view.fincdViewById(R.id.product\u image);
color=(TextView)view.fincdViewById(R.id.product\u color);
}
}
公共ProductAdapter(列表productList){
this.productList=productList;
}
//在下面,您将为单个行视图充气
@凌驾
公共MyViewHolder onCreateViewHolder(视图组父级,int-viewType){
View itemView=LayoutFlater.from(parent.getContext()).flate(R.layout.row\u视图,parent,false);
返回新的MyViewHolder(itemView);
}
@凌驾
公共无效onBindViewHolder(MyViewHolder,int位置){
holder.productImage.setImageURI(productList.get(position.getUri());
holder.color.setText(productList.get(position.getColor())
}
}
productList
将包含您希望将其图像显示为列表的产品productList
变量中截击
对于颜色,请遵循@Jai answer,对于获取尺寸,请使用以下代码 在xml中创建方向垂直的线性布局 然后获取json数组的大小响应
JSONObject lJsonobjData ;
JSONArray lJsonArrData ;
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.linearLayout);
for (int i =0; i<lJsonArrData.length(); i++){
lJsonobjData = lJsonArrData.getJSONObject(i);
String size=lJsonobjData.getString("size");
TextView tv;
tv = new TextView(this);
tvdata.setText(size);
}
JSONObject ljsonobjectdata;
JSONArray lJsonArrData;
LinearLayout LinearLayout=(LinearLayout)findViewById(R.id.LinearLayout);
对于(int i=0;iAd一些代码请查看您到目前为止都做了些什么。我不知道。我已经附上了示例图像我刚刚尝试了一些方法。但是我没有得到预期的输出,使用水平回收器视图会有所帮助。谢谢。但是当单击颜色时,与颜色相关的大小将显示出来。以前我从服务器获得了大小是的,可以显示与颜色相关的尺寸…如果答案回答了您的问题,您能否对我的答案进行投票并将其标记为正确,以便其他人知道正确的解决方案