美文网首页
ListFragment的用法

ListFragment的用法

作者: 名字_都被占了 | 来源:发表于2018-04-04 19:26 被阅读0次

    在ListActivity.class文件中

    public class ListActivity extends AppCompatActivity {
        private SuiPian suiPian;
        private ShiPeiQi shiPeiQi;
        private ArrayList<String>arrayList;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listactivity);
            suiPian=SuiPian.getInstance();
            arrayList=new ArrayList<>();
            arrayList.add("第一个");
            arrayList.add("第二个");
            arrayList.add("第三个");
            shiPeiQi=new ShiPeiQi(arrayList);
            suiPian.setListAdapter(shiPeiQi);
            FragmentUtils.add(getSupportFragmentManager(),suiPian,R.id.f1);
        }
        class ShiPeiQi extends BaseAdapter{
            private ArrayList<String>arrayList;
            public ShiPeiQi(ArrayList<String>arrayList) {
                this.arrayList=arrayList;
            }
    
            @Override
            public int getCount() {
                return arrayList.size();
            }
    
            @Override
            public Object getItem(int position) {
                return null;
            }
    
            @Override
            public long getItemId(int position) {
                return 0;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView textView=new TextView(parent.getContext());
                textView.setText(arrayList.get(position));
                return textView;
            }
        }
    }
    

    在listactivity.xml文件中

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="horizontal"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
    
        <FrameLayout
            android:id="@+id/f1"
            android:layout_width="200dp"
            android:layout_height="match_parent">
    
        </FrameLayout>
        <FrameLayout
            android:id="@+id/f2"
            android:background="@color/colorPrimary"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        </FrameLayout>
    </LinearLayout>
    

    在SuiPian.java文件中

    public class SuiPian extends ListFragment {
        private static SuiPian suiPian;
        private FirstFragment firstFragment;
        private SecondFragment secondFragment;
        private ThridFragment thridFragment;
    
        public static SuiPian getInstance() {
            if (suiPian == null) {
                suiPian = new SuiPian();
            }
            return suiPian;
        }
    
        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            Log.d("SuiPian", "position:" + position);
            switch (position) {
                case 0:
                    if (FragmentUtils.findFragment(getFragmentManager(), SecondFragment.class) != null)
                        FragmentUtils.hide(secondFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), ThridFragment.class) != null)
                        FragmentUtils.hide(thridFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), FirstFragment.class) == null)
                        FragmentUtils.add(getFragmentManager(), firstFragment, R.id.f2);
                    else
                        FragmentUtils.show(firstFragment);
                    break;
                case 1:
                    if (FragmentUtils.findFragment(getFragmentManager(), FirstFragment.class) != null)
                        FragmentUtils.hide(firstFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), ThridFragment.class) != null)
                        FragmentUtils.hide(thridFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), SecondFragment.class) == null)
                        FragmentUtils.add(getFragmentManager(), secondFragment, R.id.f2);
                    else
                        FragmentUtils.show(secondFragment);
                    break;
                case 2:
                    if (FragmentUtils.findFragment(getFragmentManager(), SecondFragment.class) != null)
                        FragmentUtils.hide(secondFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), FirstFragment.class) != null)
                        FragmentUtils.hide(firstFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), ThridFragment.class) == null)
                        FragmentUtils.add(getFragmentManager(), thridFragment, R.id.f2);
                    else
                        FragmentUtils.show(thridFragment);
                    break;
            }
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.suipian, container, false);
            return view;
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            firstFragment = FirstFragment.getInstance();
            secondFragment = SecondFragment.getInstance();
            thridFragment = ThridFragment.getInstance();
        }
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
        }
    }
    
    在suipian.xml文件中,这个文件里面有注意点
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
    
        <ListView
            android:id="@android:id/list"//必须要加一个ListView,并且id必须是@android:id/list
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
    

    在FirstFragment.java文件中

    public class FirstFragment extends Fragment {
        private static FirstFragment firstFragment;
        public static FirstFragment getInstance(){
            if(firstFragment==null){
                firstFragment=new FirstFragment();
            }
            return firstFragment;
        }
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View view=inflater.inflate(R.layout.first,container,false);
            return view;
        }
    }
    

    另外一种写法,效果和上面的一样,只是改动了两个文件中的内容

    在ListActivity.java文件中

    public class ListActivity extends AppCompatActivity {
        private SuiPian suiPian;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.listactivity);
            suiPian=SuiPian.getInstance();
            FragmentUtils.add(getSupportFragmentManager(),suiPian,R.id.f1);
        }
    }
    

    在SuiPian.java文件中

    public class SuiPian extends ListFragment {
        private static SuiPian suiPian;
        private FirstFragment firstFragment;
        private SecondFragment secondFragment;
        private ThridFragment thridFragment;
        private ShiPeiQi shiPeiQi;
        private ArrayList<String>arrayList;
    
        public static SuiPian getInstance() {
            if (suiPian == null) {
                suiPian = new SuiPian();
            }
            return suiPian;
        }
    
        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            Log.d("SuiPian", "position:" + position);
            switch (position) {
                case 0:
                    if (FragmentUtils.findFragment(getFragmentManager(), SecondFragment.class) != null)
                        FragmentUtils.hide(secondFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), ThridFragment.class) != null)
                        FragmentUtils.hide(thridFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), FirstFragment.class) == null)
                        FragmentUtils.add(getFragmentManager(), firstFragment, R.id.f2);
                    else
                        FragmentUtils.show(firstFragment);
                    break;
                case 1:
                    if (FragmentUtils.findFragment(getFragmentManager(), FirstFragment.class) != null)
                        FragmentUtils.hide(firstFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), ThridFragment.class) != null)
                        FragmentUtils.hide(thridFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), SecondFragment.class) == null)
                        FragmentUtils.add(getFragmentManager(), secondFragment, R.id.f2);
                    else
                        FragmentUtils.show(secondFragment);
                    break;
                case 2:
                    if (FragmentUtils.findFragment(getFragmentManager(), SecondFragment.class) != null)
                        FragmentUtils.hide(secondFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), FirstFragment.class) != null)
                        FragmentUtils.hide(firstFragment);
                    if (FragmentUtils.findFragment(getFragmentManager(), ThridFragment.class) == null)
                        FragmentUtils.add(getFragmentManager(), thridFragment, R.id.f2);
                    else
                        FragmentUtils.show(thridFragment);
                    break;
            }
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.suipian, container, false);
            return view;
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            firstFragment = FirstFragment.getInstance();
            secondFragment = SecondFragment.getInstance();
            thridFragment = ThridFragment.getInstance();
            arrayList=new ArrayList<>();
            arrayList.add("第一个");
            arrayList.add("第二个");
            arrayList.add("第三个");
            shiPeiQi=new ShiPeiQi(arrayList);
            setListAdapter(shiPeiQi);
            FragmentUtils.add(getFragmentManager(),firstFragment,R.id.f2);
        }
        class ShiPeiQi extends BaseAdapter {
            private ArrayList<String> arrayList;
            public ShiPeiQi(ArrayList<String>arrayList) {
                this.arrayList=arrayList;
            }
    
            @Override
            public int getCount() {
                return arrayList.size();
            }
    
            @Override
            public Object getItem(int position) {
                return null;
            }
    
            @Override
            public long getItemId(int position) {
                return 0;
            }
    
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView textView=new TextView(parent.getContext());
                textView.setText(arrayList.get(position));
                return textView;
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:ListFragment的用法

          本文链接:https://www.haomeiwen.com/subject/kmdlhftx.html