从drawable添加图像并在FlappyListView中显示

时间:2015-10-05 03:25:14

标签: android listview imageview

我想从drawable中显示很多图像并使用FlappyListView显示它们,但我不知道如何使用它。我已经这样表现出来了:

enter image description here

但我想展示不同的图像,而不是1张图像。

这是我的代码:

MainActivity.java:

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends ListActivity {
    private static final int NUM_LIST_ITEM = 10;
    private ListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        List<String> items = getListItems();
        mAdapter = new ListAdapter(this,items);
        setListAdapter(mAdapter);
        getListView().setSelection(items.size()/2);
    }

    private ArrayList<String> getListItems() {
        ArrayList<String> list = new ArrayList<String>();
        for(int i=0;i<NUM_LIST_ITEM;i++){
            list.add("Item"+i);
        }
        return list;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

ListAdapter.java:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.jpardogo.android.flabbylistview.lib.FlabbyLayout;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class ListAdapter extends ArrayAdapter<String>{
    private Context mContext;
    private List<String> mItems;
    //private Random mRandomizer = new Random();

    public ListAdapter(Context context, List<String> items) {
        super(context,0,items);
        mContext = context;
        mItems = items;
    }



    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public String getItem(int position) {
        return mItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //int color = Color.argb(255, mRandomizer.nextInt(256), mRandomizer.nextInt(256), mRandomizer.nextInt(256));
        //((FlabbyLayout)convertView).setFlabbyColor(color);
        holder.imageView.setImageResource(R.drawable.rain_forest);
        ((FlabbyLayout)convertView).setFlabbyColor(R.drawable.rain_forest);
        ((FlabbyLayout)convertView).setAlpha(20);
        return convertView;
    }

    static class ViewHolder {
        @InjectView(R.id.imageView)
        ImageView imageView;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}

item_list.xml:

<?xml version="1.0" encoding="utf-8"?>

<com.jpardogo.android.flabbylistview.lib.FlabbyLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/apk/tools"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:padding="2dp"
        android:scaleType="fitCenter"/>

</com.jpardogo.android.flabbylistview.lib.FlabbyLayout>

activity_main.xml中:

<com.jpardogo.android.flabbylistview.lib.FlabbyListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@android:color/transparent"/>

1 个答案:

答案 0 :(得分:0)

我假设您有名为image1, image2,....image10

的图片

现在您的getListItems()应该像

private ArrayList<Integer> getListItems() {
    ArrayList<Integer> list = new ArrayList<Integer>();
    for(int i=0;i<NUM_LIST_ITEM;i++){
        list.add(getResources().getIdentifier("image"+i, "id", getPackageName()););
    }
    return list;
}

并在你的适配器

public class ListAdapter extends ArrayAdapter<Integer>{
private Context mContext;
private List<Integer> mItems;
//private Random mRandomizer = new Random();

public ListAdapter(Context context, List<Integer> items) {
    super(context,0,items);
    mContext = context;
    mItems = items;
}

...

你的getItem()应该是

@Override
public Integer getItem(int position) {
    return mItems.get(position);
}

和你的

holder.imageView.setImageResource(R.drawable.rain_forest);

应该是

holder.imageView.setImageResource(getItem(position));