我想在文本字段中的单行显示2个项目,如何在listview的同一行中放置2个项目?
我想要显示这样的内容:
item 1
item 2|item3
item 4
item 5 | item 6
我的代码:
public class MultipleItemsList extends ListActivity {
private MyCustomAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new MyCustomAdapter();
int count=0;
for (int i = 1; i < 50; i++) {
if(count==0){
mAdapter.addItem("item " + i);
count++;
}
else if (count == 1) {
mAdapter.addSeparatorItem("item " + i);
count++;
}
else if(count==2){
mAdapter.addSeparatorItem1("item "+i);
count=0;
}
}
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_SEPARATOR1=2;
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>();
private TreeSet<Integer> mSeparatorSet1=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();
}
public void addSeparatorItem1(final String item){
mData.add(item);
mSeparatorSet1.add(mData.size()-1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
if(mSeparatorsSet.contains(position)){
return TYPE_SEPARATOR;
}
else if(mSeparatorSet1.contains(position)){
return TYPE_SEPARATOR1;
}
else
return 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);
holder.textView = (TextView)convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.item2, null);
holder.textView = (TextView)convertView.findViewById(R.id.textSeparator);
break;
case TYPE_SEPARATOR1:
convertView=mInflater.inflate(R.layout.item2,null);
holder.textView=(TextView)convertView.findViewById(R.id.textSeparator1);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
public static class ViewHolder {
public TextView textView;
}
}
答案 0 :(得分:0)
要在一行中显示两个项目,必须使用LinearLayout作为权重1的父项。线性布局的方向必须是水平的。 在线性布局中添加两个重量为0.5的子项。 绝对不是唯一的方法,但它会做你要求的。 希望它有所帮助。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="text" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="text" />
</LinearLayout>
答案 1 :(得分:0)
ListView并不真正支持这种操作。 ListView中的项目必须垂直堆叠。而ListView的内部实现将阻止任何简单的尝试来说服它以不同方式放置项目。
查看兼容性库中的GridLayout控件。这很容易涵盖你想要做的事情。