1

Hi i got the viewpager :contains one fragment with two different views inside [A] and [B]. both views contain a list view. when I scroll , list view [A] and switch to second fragment I want list view [B] also be scrolled with the same amount. I've searched that I can set onTouchListener to the listview.please find below the code i used.

viewpager adapter

public class ViewPagerAdapter extends FragmentStatePagerAdapter implements
        CurrencyScrollListener {

    private String nBankName;
    private List<Currency> list;
    private String mycode;
    private CurrencyViewPager[] mFragments = new CurrencyViewPager[2];
    private int mCurrentScrollPosition;

    @Override
    public int getItemPosition(Object object) {
        // TODO Auto-generated method stub
        return super.getItemPosition(object);

    }

    public CurrencyViewPager getCurrencyFragment(int index) {
        CurrencyViewPager pager = mFragments[index];
        CurrencyViewPager other = mFragments[1-index];

        if (pager == null) {
            if (index == 0) {
                pager = new CurrencyViewPager(list, nBankName, true, mycode,
                        this, 0);
            } else {
                pager = new CurrencyViewPager(list, nBankName, false, mycode,
                        this, 1);
            }
            mFragments[index] = pager;

        }

        if(other!=null && other.getCurrentPosition() != pager.getCurrentPosition()) {
            pager.scrollTo( other.getCurrentPosition() );
        }

        return pager;
    }

    public ViewPagerAdapter(FragmentManager fm, List<Currency> currencyList,
            String name, String code) {
        super(fm);
        this.list = currencyList;
        this.nBankName = name;
        this.mycode = code;

    }

    @Override
    public Fragment getItem(int item) {
        Log.i("ViewPagerAdapter","Currency Fragment #"+item);
        return getCurrencyFragment(item);

    }

    @Override
    public int getCount() {

        return 2;
    }





    }

}

And single fragment containing list view

public class CurrencyViewPager extends BaseFragment {

    private ListView mcurrencyListview;
    private GeoCurrencyService mCurrency = GeoCurrencyService.getInstance();
    ButtonPressListener buttonListener;
    private CurrencyAdapter mCurrencyAdapter;
    private String bankName;
    private boolean checkFlag;
    private ProgressBar mCurrencyProgress;
    private TextView mCurrencyLoading;
    private List<Currency> mList;
    private String recCode;
    private static int Firstposition;
    private final CurrencyScrollListener listener;
    private final int mListViewIndex;



    // constructor
    public CurrencyViewPager(List<Currency> myList, String name, boolean flag,String code, CurrencyScrollListener listener, int listViewIndex) {
        super();
        this.mList = myList;
        this.bankName = name;
        this.checkFlag = flag;
        this.recCode = code;
        this.listener = listener;
        this.mListViewIndex = listViewIndex;
    }

    // interface

    public interface ButtonPressListener {
        public void onListItemPressed(Currency object, String name,String code);
    }

    // attach on listener

    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            buttonListener = (ButtonPressListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement onButtonPressed");

        }
    }

    // creating the main View.

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        View view = inflater.inflate(R.layout.listbuy, container, false);
        mcurrencyListview = (ListView) view.findViewById(R.id.BuyList);



        mCurrencyAdapter = new CurrencyAdapter(getActivity(),
                R.layout.currency_row, mList, checkFlag, getLanguage());
        mcurrencyListview.setAdapter(mCurrencyAdapter);






        // calling method getCurrency which take bank code as a parameter
        /*getCurrency(code,null);*/

        // reference to the list view of the corresponding layout

        mcurrencyListview.setOnItemClickListener(new OnItemClickListener() {

            // onClick on the List item

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {

                // getting Currency object according to the position
                Currency currencyObject = mCurrencyAdapter.getItem(position);
                // sending the object to the mainActivity
                buttonListener.onListItemPressed(currencyObject, bankName,recCode);


            }
        });

        return view;
    }



}
VenushkaT
  • 1,152
  • 2
  • 19
  • 42

1 Answers1

0

First off, you should be using FragmentPagerAdapter instead of FragmentStatePagerAdapter (Look here for more information).

Now, to the question at hand. Use the following to get the current position of ListView [A]

int index = mListA.getFirstVisiblePosition();
View v = mListA.getChildAt(0);
int top = (v == null) ? 0 : v.getTop();

And use this to set the position of ListView [B]:

mListB.setSelectionFromTop(index, top);

Look at this SO post for more information.

Community
  • 1
  • 1
Kent Hawkings
  • 2,793
  • 2
  • 25
  • 30
  • the problem is I have single fragment with the single list view. only views of the fragment is changed dynamically accordingly to which position I am in the view pager – user3314123 Mar 05 '14 at 09:52