如何同时滚动不同片段中的3个列表视图?

时间:2017-09-15 09:57:51

标签: android

我在一个布局中有3个ListView,但有3个不同的片段。我希望,当我向下滚动时,首先加载第一个列表视图和另外两个列表视图以同时滚动背景时1更改片段相同位置     显示在第二和第三个片段我从数据库加载listview。 Pic of 1st fragment

//first fragment code

public class Quran extends Fragment {

    ListView quranlst;
    Parcelable state;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        return inflater.inflate(R.layout.activity_quran, container, false);
    }


    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        quranlst = (ListView) getActivity().findViewById(R.id.quranlist);
        DatabaseAccess db = DatabaseAccess.getInstance(getActivity().getApplicationContext());

        Intent it = getActivity().getIntent();




        int p = it.getIntExtra("paya", 0);
        int i = it.getIntExtra("saya", 0);

        if (i != 0) {


            List<String> lstsa;
            db.open();
            lstsa = db.getsaya(i);

            db.close();



            MyCustomAdapter adapter = new MyCustomAdapter(lstsa, Quran.this.getActivity());



            quranlst.setAdapter(adapter);

        } else if (p != 0) {

            List<String> lstpa;
            db.open();
            lstpa = db.getpaya(p);
            db.close();



            MyCustomAdapter adapter = new MyCustomAdapter(lstpa, Quran.this.getActivity());

            //handle listview and assign adapter

            quranlst.setAdapter(adapter);

        }


    }



}

pic of Second fragment      这是我的第二个片段代码

public class englisht extends Fragment {

    ListView el;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


        return inflater.inflate(R.layout.activity_englisht, container, false);}




    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        el = (ListView) view.findViewById(R.id.englishlst);


        DatabaseAccess db = DatabaseAccess.getInstance(getActivity().getApplicationContext());

        Intent it = getActivity().getIntent();




        int i = it.getIntExtra("saya", 0);
        int p = it.getIntExtra("paya", 0);

        if (i != 0) {
            List<String> lstenglishsurah;
            db.open();
            lstenglishsurah = db.geteaya(i);
            db.close();


            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),  R.layout.mytextview, lstenglishsurah);
            el.setAdapter(adapter);


        } else if (p != 0) {

            List<String> lstenglishparah;
            db.open();
            lstenglishparah = db.paraheng(p);
            db.close();


            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),  R.layout.mytextview, lstenglishparah);
            el.setAdapter(adapter);


        }
    }
}



**that is my third fragment code**
public class urdut extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        //Returning the layout file after inflating
        //Change R.layout.tab1 in you classes
        return inflater.inflate(R.layout.activity_urdut, container, false);}




    ListView urdulst;




    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        urdulst = (ListView) view.findViewById(R.id.urdulst);


        DatabaseAccess db = DatabaseAccess.getInstance(getActivity().getApplicationContext());

        Intent it = getActivity().getIntent();


        //SurahAyat

        int i = it.getIntExtra("saya", 0);
        int p = it.getIntExtra("paya", 0);

        if (i!=0) {
            List<String> lsturdusurah;
            db.open();
            lsturdusurah = db.getuaya(i);
            db.close();


            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.mytextview, lsturdusurah);
            urdulst.setAdapter(adapter);

        }
        else if (p!=0) {


            List<String> lsturduparah;
            db.open();
            lsturduparah = db.parahurdu(p);
            db.close();


            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),  R.layout.mytextview, lsturduparah);
            urdulst.setAdapter(adapter);

        }

    }

}

2 个答案:

答案 0 :(得分:0)

首先使用此代码获取列表视图的滚动位置

查看c = listview.getChildAt(0); int no = -c.getTop()+ listview.getFirstVisiblePosition()* c.getHeight();

然后将listview滚动位置设置为其他片段

getListView().setSelection(no);

不是:像1,2,3 ....

答案 1 :(得分:0)

您可以使用smoothScrollToPosition和smoothScrollToPositionFromTop方法滚动列表(其他列表)相对于您要滚动的第一个列表