text-view滑出list-view android的row-item

时间:2014-01-03 06:12:33

标签: android listview android-listview

我正在开发一个项目,其中有一个扩展List View的类。在get-view方法中有线性布局,其中我在几个位置添加一些文本视图,直到它工作正常但是当我开始滚动列表视图时,文本视图开始滑出行项目和随机位置和多次。我怎么解决这个问题?

在get-view方法中,我使用以下代码添加text-view

                        TableRow.LayoutParams paramsExample = new TableRow.LayoutParams(
                                LayoutParams.WRAP_CONTENT,
                                LayoutParams.MATCH_PARENT, 0);
                        paramsExample.setMargins(5, 5, 5, 5);
                        TextView tv = new TextView(activity);
                        tv.setLayoutParams(paramsExample);
                        tv.setTextColor(Color.BLACK);
                        tv.setMaxLines(1);
                        tv.setTextSize(FontClass.fontSize);
                        tv.setEllipsize(TruncateAt.END);
                        tv.setText(eventList.get(i).getEventTitle() + "");
                        l.addView(tv);

适配器代码......

private class MyCustomAdapter extends BaseAdapter {
        private static final int TYPE_ITEM = 0;
        private static final int TYPE_SEPARATOR = 1;
        private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;

        private ArrayList<String> mDataDate = new ArrayList<String>();
        private ArrayList<String> mDataMonth = new ArrayList<String>();
        private LayoutInflater mInflater;

        private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

        // String Text,UpperText;

        public MyCustomAdapter() {
            mInflater = (LayoutInflater) getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
        }

        public void addItem(final String item1, final String item2) {
            mDataDate.add(item1);
            mDataMonth.add(item2);
            notifyDataSetChanged();
        }

        public void addSeparatorItem(final String item1, final String item2) {
            mDataDate.add(item1);
            mDataMonth.add(item2);
            // save separator position
            mSeparatorsSet.add(mDataDate.size() - 1);
            notifyDataSetChanged();
        }

        @Override
        public int getItemViewType(int position) {
            return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR
                    : TYPE_ITEM;
        }

        @Override
        public int getViewTypeCount() {
            return TYPE_MAX_COUNT;
        }

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

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

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

        @SuppressWarnings("deprecation")
        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            ViewHolder holder = null;
            int type = getItemViewType(position);
            if (convertView == null) {
                holder = new ViewHolder();
                switch (type) {
                case TYPE_ITEM:
                    convertView = mInflater.inflate(R.layout.item3, null);
                    holder = new ViewHolder();
                    holder.textView = (TextView) convertView
                            .findViewById(R.id.text);

                    holder.textViewR = (TextView) convertView
                            .findViewById(R.id.txtData);


                    holder.textView.setTextSize(15);
                    holder.textViewR.setTextSize(25);

                    holder.imageViewHoliday = (ImageView) convertView
                            .findViewById(R.id.imageViewHoliday);

                    Typeface ttf = Typeface
                            .createFromAsset(activity.getAssets(),
                                    "fonts/dk_crayon_crumble.ttf");
                    holder.textView.setTypeface(ttf);

                    // TODO: For Set View Line Programattically
                    holder.layout = (LinearLayout) convertView
                            .findViewById(R.id.ll);
                    View v = new View(getContext());
                    v.setLayoutParams(new LayoutParams(1,
                            LayoutParams.FILL_PARENT));
                    v.setBackgroundColor(0xFF000000);


                    holder.layout.addView(v);
                    holder.layoutRight = (LinearLayout) convertView
                            .findViewById(R.id.linearRight);
                    holder.textView.setTextColor(Color.parseColor("#828282"));
                    break;

                case TYPE_SEPARATOR:
                    convertView = mInflater.inflate(R.layout.item4, null);
                    holder = new ViewHolder();
                    holder.textView = (TextView) convertView
                            .findViewById(R.id.textSeparator);
                    holder.textViewR = (TextView) convertView
                            .findViewById(R.id.txtData);
                    holder.layout = (LinearLayout) convertView
                            .findViewById(R.id.linearlayoutSeperator);
                    holder.imageViewHoliday = (ImageView) convertView
                            .findViewById(R.id.imageViewHoliday);

                    Typeface ttfSeparator = Typeface
                            .createFromAsset(activity.getAssets(),
                                    "fonts/dk_crayon_crumble.ttf");
                    holder.textView.setTypeface(ttfSeparator);

                    holder.textView.setTextColor(Color.parseColor("#111111"));
                    holder.layoutRight = (LinearLayout) convertView
                            .findViewById(R.id.linearRight);
                    break;
                }
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.textView.setText(mDataDate.get(position));

            // TODO TEXT SIZE CHANGING
            holder.textView.setTextSize(25);

            if (TYPE_ITEM == getItemViewType(position)) {
                // holder.textView.setTextSize(35);
                SharedPreferences sharePer = getContext().getSharedPreferences("SettingFontSizeMode", 0);

                String textSize = sharePer.getString("SettingFontSizeValue", "Medium");

                if(textSize.equals("Small"))
                {
                    FontClass.fontSize = 14;
                }

                if(textSize.equals("Medium"))
                {
                    FontClass.fontSize = 18;
                }
                if(textSize.equals("Large"))
                {
                    FontClass.fontSize = 22;
                }

                holder.textViewR.setTextSize(FontClass.fontSize);
            }

            // Holiday set work
            String Date = dayReplacement(mDataDate.get(position).replace("\n",""))+ " , " + mDataMonth.get(position);
            String[] str = Date.toString().split(" ");
            Holiday_Value = str[0] + " " + str[3].substring(0, 3);
            setHolidayList(Holiday_Value, holder.textViewR,holder.imageViewHoliday);
            // End of Holiday set work

            // Existing event set work
            String readEventDate = dayReplacement(mDataDate.get(position).replace("\n", "")) + " , " + mDataMonth.get(position);
            Log.i("position :", ""+position+" string : "+readEventDate);


            setReadCalendarEvents(readEventDate, holder.layoutRight, 0 );
            // End of Existing event set work

            holder.textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    try {
                        String Date = dayReplacement(mDataDate.get(position)
                                .replace("\n", ""))
                                + " , "
                                + mDataMonth.get(position);
                        eventSetDialog(Date);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            holder.layoutRight.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    String Date = dayReplacement(mDataDate.get(position)
                            .replace("\n", ""))
                            + " , "
                            + mDataMonth.get(position);
                    eventSetDialog(Date);
                }
            });
            return convertView;
        }
    }

setReadEventCalendar代码......

public void setReadCalendarEvents(String date, LinearLayout l, int count) {
        String[] passingDate = date.split(" ");

        if (passingDate[0].equals("") || passingDate[1] == ",") 
        {


        } 
        else {
            d = Integer.parseInt(passingDate[0]);
            m = 0;
            if (passingDate[3].equals("January")) {
                m = 0;
            } else if (passingDate[3].equals("February")) {
                m = 1;
            } else if (passingDate[3].equals("March")) {
                m = 2;
            } else if (passingDate[3].equals("April")) {
                m = 3;
            } else if (passingDate[3].equals("May")) {
                m = 4;
            } else if (passingDate[3].equals("June")) {
                m = 5;
            } else if (passingDate[3].equals("July")) {
                m = 6;
            } else if (passingDate[3].equals("August")) {
                m = 7;
            } else if (passingDate[3].equals("September")) {
                m = 8;
            } else if (passingDate[3].equals("October")) {
                m = 9;
            } else if (passingDate[3].equals("November")) {
                m = 10;
            } else if (passingDate[3].equals("December")) {
                m = 11;
            }

            y = Integer.parseInt(passingDate[4]);

            for (int i = 0; i < eventList.size(); i++) 
            {
                String[] str = eventList.get(i).getEventStartDate().split(" ");

                int aaSize = eventList.size();

                String comStr;
                if(m<9)
                {
                    if(d<10)
                    {
                        comStr = y+"-0"+(m+1)+"-0"+d;
                    }
                    else 
                    {
                        comStr = y+"-0"+(m+1)+"-"+d;
                    }
                }
                else
                {
                    if(d<10)
                    {

                        comStr = y+"-"+(m+1)+"-0"+d;
                    }
                    else 
                    {
                        comStr = y+"-"+(m+1)+"-"+d;
                    }

                }


                if (comStr.equals(str[0])) {
                    if (count < 6) {
                        count++;
                        TableRow.LayoutParams paramsExample = new TableRow.LayoutParams(
                                LayoutParams.WRAP_CONTENT,
                                LayoutParams.MATCH_PARENT, 0);
                        paramsExample.setMargins(5, 5, 5, 5);
                        TextView tv = new TextView(activity);
                        tv.setLayoutParams(paramsExample);
                        tv.setTextColor(Color.BLACK);
                        if(count == 0)
                        {
                            tv.setId(R.id.text1);
                        }
                        else if (count == 1)
                        {
                            tv.setId(R.id.text2);
                        }
                        tv.setMaxLines(1);
                        tv.setTextSize(FontClass.fontSize);
                        tv.setEllipsize(TruncateAt.END);
                        tv.setText(eventList.get(i).getEventTitle() + "");
                        l.setVisibility(View.VISIBLE);
                        l.addView(tv);
                    }
                    break;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

基于适配器的视图是他们回收他们的视图。我建议只需将额外的TextView添加到xml中,并在getView方法中设置它的可见性。这样,列表通常可以回收它的视图,而不会搞砸。

如果无法使用xml,请在values文件夹中创建ids.xml。为动态创建的TextView分配一个id,以便获取指向它的指针。只需将额外TextView一次添加到TableLayout,并记住在getView方法中更新其可见性。