【发布时间】:2014-06-10 05:37:38
【问题描述】:
我的 Activity 中有两个 ListView。我想将单个列表中的项目显示到这些列表视图中。我想把备用物品放进去。我有一个自定义 ListAdapter。
即。 LeftListView 应该包含 List 项 0,2,4,6,8…… RightListView 应该有列表项 1,3,5,7,9.......
这可以实现吗?
这是我的 xml。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<ListView
android:id="@+id/list_view_left"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:paddingRight="5dp"
android:scrollbars="none" >
</ListView>
<ListView
android:id="@+id/list_view_right"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:paddingLeft="5dp"
android:scrollbars="none" >
</ListView>
</LinearLayout>
ItemsAdapter.java
public class ItemsAdapter extends ArrayAdapter<Integer>{
Context context;
LayoutInflater inflater;
int layoutResourceId;
float imageWidth;
public ItemsAdapter(Context context, int layoutResourceId, Integer[] items) {
super(context, layoutResourceId, items);
this.context = context;
this.layoutResourceId = layoutResourceId;
float width = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
float margin = (int)convertDpToPixel(10f, (Activity)context);
// two images, three margins of 10dips
imageWidth = ((width - (3 * margin)) / 2);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
FrameLayout row = (FrameLayout) convertView;
ItemHolder holder;
Integer item = getItem(position);
if (row == null) {
holder = new ItemHolder();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = (FrameLayout) inflater.inflate(layoutResourceId, parent, false);
ImageView itemImage = (ImageView)row.findViewById(R.id.item_image);
holder.itemImage = itemImage;
} else {
holder = (ItemHolder) row.getTag();
}
row.setTag(holder);
setImageBitmap(item, holder.itemImage);
return row;
}
public static class ItemHolder
{
ImageView itemImage;
}
// resize the image proportionately so it fits the entire space
private void setImageBitmap(Integer item, ImageView imageView){
Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), item);
float i = ((float) imageWidth) / ((float) bitmap.getWidth());
float imageHeight = i * (bitmap.getHeight());
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) imageView.getLayoutParams();
params.height = (int) imageHeight;
params.width = (int) imageWidth;
imageView.setLayoutParams(params);
imageView.setImageResource(item);
}
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi/160f);
return px;
}
}
【问题讨论】:
-
是的,在两个不同的数组列表中获取值并使用 sam 适配器将其设置为这些列表视图
-
发布您的适配器代码..
-
@kalyan 我已经发布了适配器代码
-
在传递这个 Integer[] items 数组之前,将它分成两个数组并传递给 Adapter 构造函数..
标签: android list listview android-listview