在机器人制作动态布局的每一行中的ListView [英] Making dynamic layout for each row in ListView in android
问题描述
予有其中包含的ListView的每一行基本布局(其是realtive布局和里面具有TextView中)。一个xml文件
我想改变这种布局的属性的ListView的每一行就像不同的布局宽度和每行的高度。我想动态设置宽度和高度的值。
有没有办法周围做到这一点?
我想改变,身高和体重动态,为每个视图我的XML文件
< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_height =WRAP_CONTENT
机器人:ID =@ + ID / ITEM1
机器人:layout_width =WRAP_CONTENT>
<的TextView
机器人:ID =@ + ID /文
机器人:layout_height =WRAP_CONTENT
机器人:重力=center_vertical
机器人:文本=文本
机器人:能见度=看见
机器人:layout_width =WRAP_CONTENT
机器人:文字颜色=#FF200010
机器人:后台=#FFFCCCFF/>
< / LinearLayout中>
和我的全部文件
包com.test.list;
进口的java.util.ArrayList;
进口java.util.TreeSet中;
进口android.app.ListActivity;
进口android.content.Context;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.BaseAdapter;
进口android.widget.LinearLayout;
进口android.widget.TextView;
公共类MultipleItemsList扩展ListActivity {
私人MyCustomAdapter mAdapter;
公共语境上下文= getApplicationContext();
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
mAdapter =新MyCustomAdapter();
的for(int i = 1; I< 50;我++){
mAdapter.addItem(项目+ I);
如果(I%4 == 0){
mAdapter.addSeparatorItem(分隔符+ I);
}
}
setListAdapter(mAdapter);
}
私有类MyCustomAdapter扩展了BaseAdapter {
私有静态最终诠释TYPE_ITEM = 0;
私有静态最终诠释TYPE_SEPARATOR = 1;
私有静态最终诠释TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
私人的ArrayList<字符串> MDATA =新的ArrayList<字符串>();
私人LayoutInflater mInflater;
私人TreeSet的<整数GT; mSeparatorsSet =新TreeSet的<整数GT;();
公共MyCustomAdapter(){
mInflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
公共无效的addItem(最后弦乐项目){
mData.add(项目);
notifyDataSetChanged();
}
公共无效addSeparatorItem(最后弦乐项目){
mData.add(项目);
//保存分离器位置
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
@覆盖
公众诠释getItemViewType(INT位置){
返回mSeparatorsSet.contains(位置)? TYPE_SEPARATOR:TYPE_ITEM;
}
@覆盖
公众诠释getViewTypeCount(){
返回TYPE_MAX_COUNT;
}
@覆盖
公众诠释getCount将(){
返回mData.size();
}
@覆盖
公共字符串的getItem(INT位置){
返回mData.get(位置);
}
@覆盖
众长getItemId(INT位置){
返回的位置;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ViewHolder支架=无效;
整型= getItemViewType(位置);
的System.out.println(getView+位置++ convertView +型=+型);
如果(convertView == NULL){
持有人=新ViewHolder();
开关(类型){
案例TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1,NULL);
Log.e(Ronak,Here1);
TextView的T =(TextView中)convertView.findViewById(R.id.text);
t.setWidth(100);
t.setHeight(600);
t.setText(这是看第一类);
holder.textView =(TextView中)convertView.findViewById(R.id.text);
Log.e(Ronak,达here3);
打破;
案例TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item1,NULL);
TextView的T2 =(TextView中)convertView.findViewById(R.id.text);
t2.setWidth(200);
t2.setHeight(500);
t2.setText(这是第二种类型的视图);
holder.textView =(TextView中)convertView.findViewById(R.id.text);
打破;
}
convertView.setTag(保持器);
} 其他 {
支架=(ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(位置));
返回convertView;
}
}
公共静态类ViewHolder {
公众的TextView TextView的;
}
}
您可以实施 ViewHolder
模式为您的适配器,并为每个位置,抬高自己的布局。
要做到这一点,覆盖 getView
是这样的:
公开查看getView(INT位置,查看convertView,ViewGroup中父){
//一个ViewHolder不断提及孩子的意见,以避免不必要的来电
//到每一行上findViewById()。
ViewHolder持有人;
//当convertView不为空,我们可以直接重复使用它,也没有必要
//为reinflate它。我们只吹一个新的视图时,convertView提供
//通过的ListView为空。
如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.sample,NULL);
//创建一个ViewHolder和存储引用的两个孩子意见
//我们希望将数据绑定到。
持有人=新ViewHolder();
holder.name =(TextView中)convertView.findViewById(R.id.text);
holder.icon =(ImageView的)convertView.findViewById(R.id.icon);
convertView.setTag(保持器);
} 其他 {
//获取ViewHolder回得到快速访问的TextView
//和ImageView的。
支架=(ViewHolder)convertView.getTag();
}
//与支架有效地绑定的数据。
holder.name.setText(myElements.get(ID));
holder.icon.setImageBitmap(mIcon1);
返回convertView;
}
了解更多有关这对Android开发者网站这里
编辑:
要更改每行的宽度和高度,你可以绑定数据部分使用类似下面的每一行
RelativeLayout的RL =(RelativeLayout的)findViewById(R.id.yourId);
。rl.getLayoutParams()高度= 100;
。rl.getLayoutParams()宽= 100;`
I have a xml file which contains basic layout for each row of ListView(which is a realtive layout and has TextView inside it).
I want to change the attributes of this layout for each row of ListView like different layout width and height of each row. I want to set the values of width and height dynamically.
Is there any way around to do this?
My xml file which I want to change, height and weight dynamically, for each view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:id="@+id/item1"
android:layout_width="wrap_content">
<TextView
android:id="@+id/text"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="text"
android:visibility="visible"
android:layout_width="wrap_content"
android:textColor="#FF200010"
android:background="#FFFCCCFF" />
</LinearLayout>
And my full file is
package com.test.list;
import java.util.ArrayList;
import java.util.TreeSet;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MultipleItemsList extends ListActivity {
private MyCustomAdapter mAdapter;
public Context context =getApplicationContext();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MyCustomAdapter();
for (int i = 1; i < 50; i++) {
mAdapter.addItem("item " + i);
if (i % 4 == 0) {
mAdapter.addSeparatorItem("separator " + i);
}
}
setListAdapter(mAdapter);
}
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<String> mData = new ArrayList<String>();
private LayoutInflater mInflater;
private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSeparatorItem(final String item) {
mData.add(item);
// save separator position
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
holder = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.item1,null);
Log.e("Ronak", "Here1");
TextView t= (TextView)convertView.findViewById(R.id.text);
t.setWidth(100);
t.setHeight(600);
t.setText("This is first type of view");
holder.textView = (TextView)convertView.findViewById(R.id.text);
Log.e("Ronak","reached here3");
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item1, null);
TextView t2= (TextView)convertView.findViewById(R.id.text);
t2.setWidth(200);
t2.setHeight(500);
t2.setText("This is second type of view");
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
}
You can implement a ViewHolder
pattern for your adapter, and for each position, inflate your own layout.
To do that, override getView
like this:
public View getView(int position, View convertView, ViewGroup parent) {
// A ViewHolder keeps references to children views to avoid unneccessary calls
// to findViewById() on each row.
ViewHolder holder;
// When convertView is not null, we can reuse it directly, there is no need
// to reinflate it. We only inflate a new View when the convertView supplied
// by ListView is null.
if (convertView == null) {
convertView = mInflater.inflate(R.layout.sample, null);
// Creates a ViewHolder and store references to the two children views
// we want to bind data to.
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
// Get the ViewHolder back to get fast access to the TextView
// and the ImageView.
holder = (ViewHolder) convertView.getTag();
}
// Bind the data efficiently with the holder.
holder.name.setText(myElements.get(id));
holder.icon.setImageBitmap( mIcon1 );
return convertView;
}
Read more about this on the Android developer site here
EDIT:
To change the width and height of each row, you can use something like the following, for each row on bind data section :
RelativeLayout rl = (RelativeLayout) findViewById(R.id.yourId);
rl.getLayoutParams().height = 100;
rl.getLayoutParams().width = 100;`
这篇关于在机器人制作动态布局的每一行中的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!