【问题标题】:CustomAdapter different gradient list itemCustomAdapter 不同渐变列表项
【发布时间】:2016-08-27 00:09:46
【问题描述】:

我正在尝试显示带有彩色背景项目的ListView。每行应该有不同的渐变背景。我已经搜索了一段时间,但无法解决我的问题。现在每一行都有相同的背景 - 最后保存的配置文件。此外,我未能将渐变设置为使用rounded.xml 作为背景的TextView 的背景。感谢您的帮助。

这是我的CustomAdapter

public class CustomAdapterProfiles extends ArrayAdapter<Profile> {

    private static final String TAG = "MyActivity";
    ArrayList<Profile> myArrayList = null;
    PaintDrawable paint;

    int[] arrColors;
    int numColors;
    float[] result;

    Profile i;

    CustomAdapterProfiles(Context context, ArrayList<Profile> menuAdapter){
        super(context, R.layout.customrow , menuAdapter);
        this.myArrayList = menuAdapter;
    }


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

        LayoutInflater listInflater = LayoutInflater.from(getContext());
        View customView = listInflater.inflate(R.layout.customrow, parent, false);

        i = myArrayList.get(position);
        String singleItem = i.getObjectName();
        TextView mobileText = (TextView) customView.findViewById(R.id.listID);
        mobileText.setText(singleItem);

        numColors = i.getArrayList().size();
        arrColors = new int[i.getArrayList().size()];

        if (numColors>1) {

            //positions of colors defined by user
            result = new float[numColors];
            for (int a = 0; a < numColors; a++) {
                result[a] = (float) i.getGradients().get(a);
            }

            //make sure user didnt write error values (not fixed yet)
            result[0]=0;
            result[numColors - 1] = 1;

            //colors
            for (int j = 0; j < numColors; j++) {
                arrColors[j] = Integer.parseInt(i.getArrayList().get(j).toString(), 16) + 0xFF000000;
            }

            ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
                @Override
                public Shader resize(int width, int height) {
                    LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
                            arrColors, //pouzity array farieb
                            result,
                            Shader.TileMode.REPEAT);
                    return linearGradient;
                }
            };
            paint = new PaintDrawable();
            paint.setShape(new RectShape());
            paint.setShaderFactory(shaderFactory);

            mobileText.setBackgroundDrawable((Drawable) paint);
        }
        else {
            //cant set shaderFactory becouse it needs 2 or more colors
            mobileText.getBackground().setColorFilter(Color.parseColor("#" + i.getArrayList().get(0).toString()), PorterDuff.Mode.SRC_ATOP);
        }

        return customView;
    }
}

【问题讨论】:

    标签: android listview background gradient


    【解决方案1】:

    从列表项的布局中删除背景集。从您的代码中,我看到您为每个列表项使用的布局是customrow.xml。您可能在那里有rounded.xml 背景。删除该行。

    现在关于为每个列表项显示正确的颜色...

    从图片中我可以看到您正在设置一些渐变,所以我想您可以正确生成渐变。

    现在我可以从您的代码中看到,您正在为ListView 的每个项目设置相同的颜色。所以我猜你误解了getView() 函数的行为。所以在这里我要澄清这个想法。

    一旦ListView 的每个项目显示在屏幕上,就会调用getView()。假设您的列表中有 20 个项目。现在,当第一次加载列表时,让我们猜测前 7 个项目显示在屏幕上,您必须滚动才能看到其他项目。

    下面是ListView 如何重新循环已生成的视图。 ListView 不会一次填充所有 20 个项目。相反,它会填充屏幕中显示的前 7 个。所以第一次,getView() 函数被调用 7 次来填充屏幕中可见的每个项目。当您滚动列表时,将再次为列表中的每个新可见项调用 getView() 函数。

    希望您从解释中有所了解。现在,这是解决问题的方法。

    让我们获取一个由用户定义的颜色数组。

    int[] arrColors = {/* ..get the user input and populate the colour array outside of the adapter. */};
    int numColors = 10; // I've just set a default value
    

    现在这是您的getView 函数的伪代码。

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        LayoutInflater listInflater = LayoutInflater.from(getContext());
        View customView = listInflater.inflate(R.layout.customrow, parent, false);
    
        // ... Set the text
        // position of colors defined by user
        // ... Get the user defined colour here. 
        Color color = arrColors[position];
    
        // Now modify the colour as you wish
        Paint paint = prepareTheBackground();
    
        // Now set the colour as background
        mobileText.setBackgroundDrawable((Drawable) paint);
    
        return customView;
    }
    

    【讨论】:

    • 感谢您的回复! :) 使用rounded.xml 作为customrow.xml 的背景的要点——我想让最终的结果是带有渐变背景颜色的四舍五入的textview。但这不是大问题。您发布的伪代码基本上是我拥有的代码。它为每个项目设置相同的渐变背景。我希望它是独一无二的:/ 每个项目中都有包含渐变颜色的对象。添加新项目后,所有项目都有上次添加的背景。
    • 关键是为每个列表项设置背景对吧?从您的代码中我看到,您为所有项目设置了相同的背景颜色。我的想法是先用所需的颜色填充一个数组,然后在getView() 函数中获取列表视图的position 并从该数组中获取确切的颜色。请再次检查您的代码。
    【解决方案2】:

    最大的问题(我猜是这个)是使用 ArrayAdapter 而不是 BaseAdapter。我已经尝试过(作为noob android 程序员)很多东西和教程,但是在我尝试了这个之后:enter link description here 它工作了。另外,如您所见,我找到了圆角文本视图的解决方案(在“---”下面的代码中标记)。行项目的名称设置为“”,因此您看不到名称。

    enter image description here

    public class CustomListAdapter extends BaseAdapter {
    private Context context; //context
    private ArrayList<Profile> items; //data source of the list adapter
    
    //public constructor
    public CustomListAdapter(Context context, ArrayList<Profile> items) {
        this.context = context;
        this.items = items;
    }
    
    @Override
    public int getCount() {
        return items.size(); //returns total of items in the list
    }
    
    @Override
    public Object getItem(int position) {
        return items.get(position); //returns list item at the specified position
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    public void updateResults(ArrayList<Profile> results) {
        items = results;
        //Triggers the list update
        notifyDataSetChanged();
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
    
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.customrow, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
    
        // get current item to be displayed
        Profile currentItem = (Profile) getItem(position);
        viewHolder.itemName.setText(currentItem.getObjectName());
    
    
        int numColors = currentItem.getArrayList().size();
    
    
        if (numColors > 1) {
    
            int[] arrColors = new int[numColors];
    
            //positions of colors defined by user
            final float[] result = new float[numColors];
            for (int a = 0; a < numColors; a++) {
                result[a] = (float) currentItem.getGradients().get(a);
            }
    
            //make sure user didnt write error values (not fixed yet)
            result[0] = 0;
            result[numColors - 1] = 1;
    
            //colors
            for (int j = 0; j < numColors; j++) {
                arrColors[j] = Integer.parseInt(currentItem.getArrayList().get(j).toString(), 16) + 0xFF000000;
            }
    
            final int[] finalArrColors = arrColors;
    
            ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
                @Override
                public Shader resize(int width, int height) {
                    LinearGradient linearGradient = new LinearGradient(0, 0, width, height,
                            finalArrColors, //pouzity array farieb
                            result,
                            Shader.TileMode.REPEAT);
                    return linearGradient;
                }
            };
    
            // --- rounded textView !
            PaintDrawable paint = new PaintDrawable();
            paint.setShape(new RectShape());
            paint.setShaderFactory(shaderFactory);
    
            paint.setCornerRadius(100);
            // --- end of rounded textView code
    
            viewHolder.itemName.setBackgroundDrawable(paint);
        }
        else if (numColors == 1) {
            //not important
        }
        else {
            viewHolder.itemName.setText("empty object");
        }
    
        return convertView;
    }
    
    private class ViewHolder {
        TextView itemName;
    
        public ViewHolder(View view) {
            itemName = (TextView) view.findViewById(R.id.listID);
        }
    }
    

    }

    调用 BaseAdapter:

    CustomListAdapter adapter = new CustomListAdapter(this, profiles); ListView menuListView = (ListView) findViewById(R.id.listViewHS); menuListView.setAdapter(adapter); adapter.updateResults(profiles);

    个人资料类别:

    public class Profile implements Serializable {
    
    private String objectName;
    private ArrayList<String> arrayColorList;
    private ArrayList<Float> gradients;
    
    public Profile(String objectName, ArrayList<String> arrayList, ArrayList<Float> gradients){
        this.objectName=objectName;
        this.arrayColorList=arrayList;
        this.gradients=gradients;
    }
    
    public String getObjectName() {
        return objectName;
    }
    
    public ArrayList<String> getArrayList() {
        return arrayColorList;
    }
    
    public ArrayList<Float> getGradients() {
        return gradients;
    }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多