两个带有单个List的ListView

时间:2014-06-10 05:37:39

标签: android list listview android-listview

我的Activity中有两个ListView。我想将单个List中的项目显示到这些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;
}

}

2 个答案:

答案 0 :(得分:0)

假设您的所有数据现在都存储在ArrayList中,请创建两个ArrayLists(每个列表一个)。

对于每个ArrayList,填充值并将它们传递给适配器。

for(int x : list)
{
if(x%2 == 0)
leftList.add(x);
else
rightList.add(x);
}

list:包含完整数据的ArrayList

leftList, rightList:包含特定数据的ArrayLists

答案 1 :(得分:0)

只需创建2个不同值的ArrayList。比如,如果你有1,2,3,4,5,6,7,8,9,10的价值。然后如下所示:

for(int i=0;i<mainArray;i++){
            if(i%2==0){
                evenArray.add(mainArray.get(i));
            }else{
                oddArray.add(mainArray.get(i));
            }
        }

使用上面的代码,你将有两个不同的arraylist,然后使用它。