在列表视图中维护View Flipper的状态

时间:2013-11-28 17:54:52

标签: android listview viewflipper

我将视图翻转器视为列表视图项,并且我在运行时将项加载到View Flipper。

问题:我的问题是当我将视图翻转到特定行时,当我滚动列表视图时,在向右翻转后设置到特定位置的视图将丢失。

在滚动时,所有行中的视图翻板都会重新初始化为第0个状态。

这是我的基本适配器:

public class ListViewAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<TypeOneDataClass> typeOneList;
    private LayoutInflater inflater;
    public static ArrayList<ViewPager> myPagers = new ArrayList<ViewPager>();
    private ArrayList<Integer> myInnerList;
    //public static ArrayList<ArrayList<Integer>> myList = new ArrayList<ArrayList<Integer>>();
    private ArrayList<FlipperPosition> myFlipList = new ArrayList<FlipperPosition>();

    public ListViewAdapter(Context context,
            ArrayList<TypeOneDataClass> typeOneList) {
        this.context = context;
        this.typeOneList = typeOneList;
        inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return typeOneList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return typeOneList.get(position);
    }

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

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return IGNORE_ITEM_VIEW_TYPE;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        FlipperPosition flipperPosition;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item, null, false);
            holder.viewFlipper = (ViewFlipper)convertView.findViewById(R.id.myFlipper);
            for(int i=0;i<typeOneList.get(position).getDetailsList().size();i++){
                View v = inflater.inflate(R.layout.pager_item, null);
                holder.viewFlipper.addView(v);

                final TextView tv_name = (TextView)v.findViewById(R.id.tv_name);
                final TextView tv_fathersName = (TextView)v.findViewById(R.id.tv_fathersname);
                Button btn_right = (Button)v.findViewById(R.id.btn_right);
                Button btn_left = (Button)v.findViewById(R.id.btn_left);

                final ViewHolder vHolder = holder;

                btn_left.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        vHolder.viewFlipper.setInAnimation(context, R.anim.slide_in_left);
                        vHolder.viewFlipper.setOutAnimation(context, R.anim.slide_out_right);
                        vHolder.viewFlipper.showPrevious();
                        Toast.makeText(context, "Row"+position+" "+vHolder.viewFlipper.indexOfChild(vHolder.viewFlipper.getCurrentView()), 0).show();
                    }
                });

                btn_right.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        vHolder.viewFlipper.setInAnimation(context, R.anim.slide_in_right);
                        vHolder.viewFlipper.setOutAnimation(context, R.anim.slide_out_left);
                        vHolder.viewFlipper.showNext();
                        int indexofFliper = vHolder.viewFlipper.indexOfChild(vHolder.viewFlipper.getCurrentView());
                        Toast.makeText(context, "Row"+position+" "+indexofFliper, 0).show();
                    }
                });

                tv_name.setText(typeOneList.get(position).getName());
                tv_fathersName.setText(typeOneList.get(position).getDetailsList().get(myFlipList.get(position).getFlipTermSet()).getFathersname());
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }
    class ViewHolder {
        ViewFlipper viewFlipper;
    }

1 个答案:

答案 0 :(得分:2)

我不知道TypeOneDataClass是什么,但我建议为其添加一个额外的字段,例如:

int lastDisplayed;

然后您可以更改按钮以保存当前状态:

btn_left.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        vHolder.viewFlipper.setInAnimation(context, R.anim.slide_in_left);
        vHolder.viewFlipper.setOutAnimation(context, R.anim.slide_out_right);
        vHolder.viewFlipper.showPrevious();
        // SAVE STATE
        typeOneList.get(position).lastDisplayed = vHolder.viewFlipper.getDisplayedChild();
    }
});

btn_right.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        vHolder.viewFlipper.setInAnimation(context, R.anim.slide_in_right);
        vHolder.viewFlipper.setOutAnimation(context, R.anim.slide_out_left);
        vHolder.viewFlipper.showNext();
        // SAVE STATE
        typeOneList.get(position).lastDisplayed = vHolder.viewFlipper.getDisplayedChild();
    }
});

最后,当行重新进入视图时,您需要恢复该状态。

    convertView.setTag(holder);
    // RESTORE STATE
    holder.viewFlipper.setDisplayedChild(typeOneList.get(position).lastDisplayed);
} else {
    holder = (ViewHolder) convertView.getTag();
}
return convertView;