如何使用pulltorefresh类在Gridview中显示图像

时间:2015-02-05 20:48:46

标签: android gridview pull-to-refresh

我可以正常显示gridview中的图片。但是,我想使用pulltorefresh功能,我找到了一个库来获取功能。我很困惑如何整合我的图像。

现在,gridview只是在数组中显示 imagelinks 。如何使用 imageAdapter 代替 arrayAdapter 来显示图片?

非常感谢。

这是活动类:

public final class PullToRefreshGridActivity extends Activity {

static final int MENU_SET_MODE = 0;

private LinkedList<String> mListItems;
private PullToRefreshGridView mPullRefreshGridView;
private GridView mGridView;
private ArrayAdapter<String> mAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ptr_grid);

    mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
    mGridView = mPullRefreshGridView.getRefreshableView();

    // Set a listener to be invoked when the list should be refreshed.
    mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {

        @Override
        public void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {
            Toast.makeText(PullToRefreshGridActivity.this, "Pull Down!", Toast.LENGTH_SHORT).show();
            new GetDataTask().execute();
        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {
            Toast.makeText(PullToRefreshGridActivity.this, "Pull Up!", Toast.LENGTH_SHORT).show();
            new GetDataTask().execute();
        }

    });

    mListItems = new LinkedList<String>();

    TextView tv = new TextView(this);
    tv.setGravity(Gravity.CENTER);
    tv.setText("Empty View, Pull Down/Up to Add Items");
    mPullRefreshGridView.setEmptyView(tv);

    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
    mGridView.setAdapter(mAdapter);
}

private class GetDataTask extends AsyncTask<Void, Void, String[]> {

    @Override
    protected String[] doInBackground(Void... params) {
        // Simulates a background job.
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        return mStrings;
    }

    @Override
    protected void onPostExecute(String[] result) {
        mListItems.addFirst("Added after refresh...");
        mListItems.addAll(Arrays.asList(result));
        mAdapter.notifyDataSetChanged();

        // Call onRefreshComplete when the list has been refreshed.
        mPullRefreshGridView.onRefreshComplete();

        super.onPostExecute(result);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, MENU_SET_MODE, 0,
            mPullRefreshGridView.getMode() == Mode.BOTH ? "Change to MODE_PULL_DOWN"
                    : "Change to MODE_PULL_BOTH");
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem setModeItem = menu.findItem(MENU_SET_MODE);
    setModeItem.setTitle(mPullRefreshGridView.getMode() == Mode.BOTH ? "Change to MODE_PULL_FROM_START"
            : "Change to MODE_PULL_BOTH");

    return super.onPrepareOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case MENU_SET_MODE:
            mPullRefreshGridView
                    .setMode(mPullRefreshGridView.getMode() == Mode.BOTH ? Mode.PULL_FROM_START
                            : Mode.BOTH);
            break;
    }

    return super.onOptionsItemSelected(item);
}

private String[] mStrings = {"https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
        "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
        "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
        "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s1024/Antelope%252520Butte.jpg",
        "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s1024/Antelope%252520Hallway.jpg",
        "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s1024/Antelope%252520Walls.jpg",
        "https://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg",
        "https://lh3.googleusercontent.com/-s-AFpvgSeew/URquc6dF-JI/AAAAAAAAAbs/Mt3xNGRUd68/s1024/Backlit%252520Cloud.jpg",
        "https://lh5.googleusercontent.com/-bvmif9a9YOQ/URquea3heHI/AAAAAAAAAbs/rcr6wyeQtAo/s1024/Bee%252520and%252520Flower.jpg",
        "https://lh5.googleusercontent.com/-n7mdm7I7FGs/URqueT_BT-I/AAAAAAAAAbs/9MYmXlmpSAo/s1024/Bonzai%252520Rock%252520Sunset.jpg",
        "https://lh6.googleusercontent.com/-4CN4X4t0M1k/URqufPozWzI/AAAAAAAAAbs/8wK41lg1KPs/s1024/Caterpillar.jpg",
        "https://lh3.googleusercontent.com/-rrFnVC8xQEg/URqufdrLBaI/AAAAAAAAAbs/s69WYy_fl1E/s1024/Chess.jpg",
        "https://lh5.googleusercontent.com/-WVpRptWH8Yw/URqugh-QmDI/AAAAAAAAAbs/E-MgBgtlUWU/s1024/Chihuly.jpg",};
}

1 个答案:

答案 0 :(得分:0)

创建自定义适配器,刷新每个单元格。创建一个包含String和Image属性的对象,在GetDataTask中创建所有这些新对象的ArrayList并刷新适配器。

下面是适配器和对象的简单示例

public class CustomStepsAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<CustomObject> details;
    private LayoutInflater mInflater;

    public CustomStepsAdapter(Context aContext, ArrayList<CustomObject> data) {
        this.details = data;
        this.mContext = aContext;

        mInflater = LayoutInflater.from(aContext);
    }


    public int getCount() {
        return details.size();
    }

    public CustomObject getItem(int position) {
        return details.get(position);
    }

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

    public View getView() {
        return mInflater.inflate(R.layout.custom_step_options_cell, null);
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        final CustomObject item = getItem(position);

        CustomStepCell cell = null;
        if (convertView == null) {
            convertView = getView();

            cell = new CustomStepCell();

            cell.itemName = (TextView) convertView.findViewById(R.id.option_name);
            cell.image = (ImageView) convertView.findViewById(R.id.green_checkmark);

            convertView.setTag(cell);
        }
        else
        {
            cell = (CustomStepCell) convertView.getTag();
        }

        cell.itemName.setText(item.getItemName());
        cell.image = item.getImage();


        return convertView;
    }

    public boolean isShowPrices() {
        return showPrices;
    }

    public void setShowPrices(boolean showPrices) {
        this.showPrices = showPrices;
    }

    public static class CustomStepCell
    {
        public TextView itemName;
        public ImageView image;
    }
}


public class CustomObject
{
    private String itemName;
    private ImageView image;

    public CustomObject()
    {
        this.itemName = "";
    }

    public void setImage(ImageView anImage)
    {
        this.image = anImage;
    }

    public void setItemName(String anItemName)
    {
        this.itemName = anItemName;
    }

    public ImageView getImage()
    {
        return this.image;
    }

    public String getItemName()
    {
        return this.itemName;
    }

}
相关问题