在viewpager中切换片段时的listview同步

时间:2014-03-04 20:10:12

标签: android listview android-listview android-fragments

我在我的应用中查看了寻呼机。在viewpager中有两个片段[1]和[2]都有[A]和[B]中的列表视图。当我滚动listview [A]并切换片段[2]时,我希望listview [B]自动滚动相同的数量。请帮忙

这是我的代码段

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

        Log.i("TAG","onCreateView");
        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);
        mcurrencyListview.setSelection(Firstposition);


                // 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;
    }


    // method getCurrency which takes bank code as a parameter and
    // sets the adapter to the list on onPostExecute.



}

和viewpager的片段适配器

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 getFCurrencyFragment(int index) {
        CurrencyViewPager pager = mFragments[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;
        }


        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("TAG","getItem");
        return getFCurrencyFragment(item);


    }

    @Override
    public int getCount() {

        return 2;
    }


}

0 个答案:

没有答案