在Android中,我想构建一个ListView,其中包含以下骨架等所有项目:
Soundname 部分(textview)根据加载的字符串数组(声音名称列表)而变化。 两个 ImageButtons 始终具有相同的图标,必须播放或选择与所点击的项目对应的声音。
目前我只知道如何使用一个Textview构建ListView,并通过加载String的数组的适配器对其进行充气。
你能给我一些关于如何实现这个目标的示例代码吗?谢谢
答案 0 :(得分:0)
通过扩展BaseAdapter
创建自定义适配器并将其设置为列表的适配器。在线查找教程this one。
答案 1 :(得分:0)
Follow these steps
1-Create main xml file with listview
2-Create another xml file with name list_itmes with textview, and with both image button
3-Now Write code in your Activity
a)create listview object
b)Here we need to use BaseAdapter it has some methods inside getView() inflate another xml file as follows
public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
public CustomBaseAdapter(Context context, List<RowItem> items) {
this.context = context;
this.rowItems = items;
}
/*private view holder class*/
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = (RowItem) getItem(position);
holder.txtDesc.setText(rowItem.getDesc());
holder.txtTitle.setText(rowItem.getTitle());
holder.imageView.setImageResource(rowItem.getImageId());
return convertView;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
}
答案 2 :(得分:0)
使用以下布局
<RelativeLayout
android:id="@+id/relInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:ellipsize="end"
android:maxLines="1"
android:text="SoundName"
android:textSize="16dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imgOne"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:scaleType="centerCrop"
android:src="@drawable/images" />
<ImageView
android:id="@+id/imgTwo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="@+id/imgOne"
android:scaleType="centerCrop"
android:src="@drawable/images" />
</RelativeLayout>