Adapter的getItem(position)和setBackgroundResource

时间:2015-01-23 13:23:23

标签: java android gridview adapter

基本上我有一个View-pager,我可以使用适配器在1-6张图片之间滑动。

为了“跟踪”图片,我实现了一个选择器,它由使用适配器充气的图像视图表示。

接下来,我正在使用setOnPageChangeListener,更确切地说是onPageSelected(int position)方法,以查看当前Image-view的内容并更改其背景,以便可以通过其余部分区分它们。但这不起作用,虽然它针对正确的图像,但资源不会改变。

以下是一些更好理解的代码:

这是我设置适配器大小的地方

int poop = profile.getImages().size();

                for (int i = 0; i < poop; i++) {
                    ImageView image = new ImageView(v.getContext());
                    imageList.add(image);
                }

                selectorAdapter = new SelectorAdapter(imageList, (android.support.v4.app.FragmentActivity) v.getContext());
                selector_view.setAdapter(selectorAdapter);

这是适配器:

public class SelectorAdapter extends BaseAdapter {

    private List<ImageView> mArray = new ArrayList<ImageView>();
    private FragmentActivity mContext;

    public SelectorAdapter(List<ImageView> mArray, FragmentActivity mContext) {
        this.mArray = mArray;
        this.mContext = mContext;
    }

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

    @Override
    public Object getItem(int position) {
        return mArray.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.selector_item, null);
        }

        return convertView;
    }
}

适配器的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/selector_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/p_photo_unmarked"
        android:layout_marginLeft="5dp"/>

最后魔术应该发生在哪里:

profilePic.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (selectorAdapter.getItem(position) instanceof ImageView) {
                    ((ImageView) selectorAdapter.getItem(position)).setBackgroundResource((R.drawable.p_photo_marked));
                    Toast.makeText(getActivity().getApplicationContext(), "position" + position, Toast.LENGTH_SHORT).show();
                }

1 个答案:

答案 0 :(得分:0)

更改行代码

((ImageView) selectorAdapter.getItem(position)).setBackgroundResource((R.drawable.p_photo_marked));

((ImageView) selectorAdapter.getItem(position)).setBackground(getResources().getDrawable(R.drawable.p_photo_marked))

编辑:

((ImageView)((SimpleTabPagerAdapter) selector_view.getAdapter()).getItem(position)).setBackgroundResource((R.drawable.p_photo_marked));