1

我有一个列表视图并为其设置 BaseAdapter,行布局只有文本视图。

我通过拥有视图对象来跟踪以前的 textview,现在当我滚动 listview 视图时,previoustextview obj 不起作用。

如何知道之前选择了哪个子视图。

public View getView(final int position, final View convertView, final ViewGroup parent) {
        TextView txtview = (TextView) convertView;            
        boolean flag = false;
        final Resources res = getResources();
        final Drawable actbtnDoneDraw = res.getDrawable(R.drawable.action_done_button);
        final Drawable actbtnSelectedDraw = res.getDrawable(R.drawable.action_button_selected);
        final Drawable actbtnDraw = res.getDrawable(R.drawable.action_button);

        if (null == convertView) {

                if (DeviceDetail.isGalaxyTabOriginal()) {
                    txtview = (TextView) inflater.inflate(R.layout.list_txtview_gtab0, parent,
                            true);
                } else {

                    txtview = (TextView) inflater.inflate(R.layout.list_txtview, parent, false);

                }

            } else {
                txtview = (TextView) convertView;
                if (selectedposition != position) {
                    txtview.setBackgroundDrawable(actbtnDraw);
                    txtview.setSelected(false);

                } else {

                    txtview.setBackgroundDrawable(actbtnSelectedDraw);
                    txtview.setSelected(true);

                }
            }
            final Activity activity = activities.get(position);
            txtview.setText(activity.name);
            txtview.setTag(Integer.valueOf(activity.id));

            txtview.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(final View v) {
                    selectedposition = position;
                    Log.i("Onclick position = " + position);
                    modifyButtonView(v, position);
                }
            });

        }

public void modifyButtonView(final View view, final int position) {

        final TextView v = (TextView) view;
        final TextView pv = (TextView) previousview;

        final Resources res = getResources();
        final Drawable actbtnSelectedDraw = res.getDrawable(R.drawable.action_button_selected);
        final Drawable actbtnDraw = res.getDrawable(R.drawable.action_button);



        if (NoSelection) {
            v.setSelected(true);
            v.setBackgroundDrawable(actbtnSelectedDraw);


        } else {
            if (previousposition != position) {

                v.setSelected(true);
                pv.setBackgroundDrawable(actbtnDraw);
                v.setBackgroundDrawable(actbtnSelectedDraw);
                pv.setSelected(false);
            }
        }            

        previousview = v;
        previousposition = position;
        NoSelection = false;
        mListener.onActivityButtonClicked(v);
    }
}
4

2 回答 2

0

如何知道之前选择了哪个子视图。

将两个字段变量添加到您的 Adapter 类,调用它们public int previousChoicecurrentChoice. 在您的onItemClick()oronListItemClick()方法中,使用第三个参数更新此值 position

mAdapter.previousChoice = mAdapter.currentChoice;
mAdapter.currentChoice = position;
于 2012-12-26T23:09:08.680 回答
0

// 获取上一个视图,取决于位置,.... method(view,position)

            final int wantedPosition = previousposition;
            final int firstPosition = listview.getFirstVisiblePosition()
                    - listview.getHeaderViewsCount();
            final int wantedChild = wantedPosition - firstPosition;
            if (!(wantedChild > firstPosition && wantedPosition < listview
                    .getLastVisiblePosition())) {

// 如果你有 textview 仅作为行视图而不是

                pv = (TextView) listview.getChildAt(wantedChild);
                if (pv == null) {
                    pv = (TextView) previousview;
                }
            }
            if (previousposition != position) {
                // perform the operation on the pv view or current v view
            }
        }
        previousview = v;
        previousposition = position;            
于 2012-12-27T17:31:25.640 回答