Maintain the state of View Flipper in List View

六月ゝ 毕业季﹏ 提交于 2020-02-04 04:30:07

问题


I have view flipper as List View Item, and I load items to the View Flipper at runtime.

Problem : My problem is when I flip views to a particular row, and when I scroll the listview, the view which was set to a particular position after flipping to right, is lost.

On Scrolling the view flipper in all the rows are re-initialized to the 0th state.

Here is my Base Adapter:

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:


I don't know what a TypeOneDataClass is, but I'd suggest adding an extra field to it, such as:

int lastDisplayed;

Then you can change your buttons to save the current state:

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();
    }
});

Finally, you need to restore that state when the row comes back into view.

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


来源:https://stackoverflow.com/questions/20272244/maintain-the-state-of-view-flipper-in-list-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!