在SpinnerAdapter getView&getDropDownView的区别(Differe

2019-06-21 04:53发布

当你实现SpinnerAdapter你getDropDownView ,它是如何从不同getView ,你有,当你需要扩展BaseAdapter

Answer 1:

如果我们看一下下面的代码,我们在getView和getDropDownView名称和值数组。

private void initView() {
    SpinnerDropDownAdapter sddadapter = new SpinnerDropDownAdapter(this);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, sddadapter.name);

    Spinner getViewSP = (Spinner) findViewById(R.id.getview_sp);
    getViewSP.setAdapter(adapter);

    Spinner getViewWDropDownSP = (Spinner) findViewById(R.id.getview_w_drop_down_sp);
    getViewWDropDownSP.setAdapter(sddadapter);
}

static class SpinnerDropDownAdapter extends BaseAdapter implements
        SpinnerAdapter {
    Context context;

    SpinnerDropDownAdapter(Context ctx) {
        context = ctx;
    }

    String[] name = { " One", " Two", " Three", " Four", " Five", " Six",
            " Seven", " Eight" };
    String[] value = { " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8" };

    @Override
    public int getCount() {
        return name.length;
    }

    @Override
    public String getItem(int pos) {
        // TODO Auto-generated method stub
        return name[pos];
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView text = new TextView(context);
        text.setTextColor(Color.BLACK);
        text.setText(name[position]);
        return text;
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        TextView text = new TextView(context);
        text.setTextColor(Color.BLACK);
        text.setText(value[position]);
        return text;
    }
}

如果getDropDownView方法未实现,下拉弹出将得到getView的看法。 因此,将只显示名字。

当两个getView和getDropDownView实现,前者获得的名称 ,后者获得价值 ,在休息的微调将getview得到的姓名和下拉弹出将获得价值



Answer 2:

该接受的答案似乎有点复杂,所以为了简单起见,

  • getView -在用户之前所说的纺丝器中示出的值按下旋转器,其中,每个值视图可以与convertView参数被调整

  • getDropDownView -值的列表中的用户按下旋转器,其中,在所述列表中的每个值可以用convertView参数被适配之后,用户可以选择



Answer 3:

getViewgetDropDownView可以显示不同的布局。

我创建一个微调和使用class ImageTextAdapter extends ArrayAdapter<String>作为一个适配器。 我重写getView ,以便它可以同时显示ImageViewTextView 。 不过,我不会覆盖getDropDownView

这是截图按下微调前

和之后的微调被按下。

我初始化用下面的代码微调。

String[] spinnerTexts = {"Tanzania", "Thailand"};
int[] spinnerImages = {R.drawable.tanzania, R.drawable.thailand};

ArrayAdapter<String> adapter = new ImageTextAdapter(this, spinnerTexts, spinnerImages);

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);

这里是ImageTextAdapter.java

public class ImageTextAdapter extends ArrayAdapter<String> {

    int[] images;

    public ImageTextAdapter(Context ctx, String[] texts, int[] images) {
        super(ctx, android.R.layout.simple_spinner_item, texts);
        this.images = images;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = getLayoutInflater().inflate(R.layout.row, parent, false);

        TextView textView = (TextView) row.findViewById(R.id.textView);
        textView.setText(getItem(position));

        ImageView imageView = (ImageView) row.findViewById(R.id.imageView);
        imageView.setImageResource(images[position]);

        return row;
    }
}


Answer 4:

我们可以用SpinnerAdapter就象这样:

public class FolderSpinnerAdpater extends BaseAdapter implements SpinnerAdapter {
@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    return null;
}
}

在BaseAdapter.java

public View getDropDownView(int position, View convertView, ViewGroup parent) {
    return getView(position, convertView, parent);
}

覆盖getView()getDropDownView()返回不同的看法,你会发现差异。



文章来源: Difference between getView & getDropDownView in SpinnerAdapter