嵌套的子类

public class Fragements1 extends Fragment {
    private String urls = "http://www.yulin520.com/a2a/impressApi/news/mergeList?pageSize=10&page=";
    private int us = 1;
    private View view;
    private PullToRefreshListView fl_prlv;
    private List list;
    private MAdapter adapter;
    private ImageLoader instance;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragements, container, false);

        intView();

        adapter = new MAdapter();
        fl_prlv.setAdapter(adapter);
        intData(1);
        //设置可以上下拉动的模式  fl_prlv.setMode(PullToRefreshBase.Mode.BOTH);
        //设置刷新的监听  fl_prlv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            //下拉  public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
                list.clear();
                intData(1);
            }

            @Override
            //上拉  public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
                intData(us++);
            }
        });
        return view;
    }

    private void intData(int page) {
        new MAsyncTask().execute(urls+us);

    }

    private void intView() {
        fl_prlv = (PullToRefreshListView) view.findViewById(R.id.fs_prlv);
        list = new ArrayList<>();
        instance = ImageLoader.getInstance();
    }

    class MAsyncTask extends AsyncTask {

        @Override
        protected String doInBackground(String... strings) {
            return NetWorkUtil.getNetjson(strings[0]);

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.d("Tag", "onPostExecute: "+s);
            Gson gson = new Gson();
            News news = gson.fromJson(s, News.class);
            List dataBeanList = news.getData();
            list.addAll(dataBeanList);
            adapter.notifyDataSetChanged();
        }
    }

    class MAdapter extends BaseAdapter {
        private final int Type0 = 0;
        private final int Type1 = 1;

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        //下标所对应的布局  public int getItemViewType(int position) {
            return position % 2;
        }

        @Override
        //返回几个布局  public int getViewTypeCount() {
            return 2;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            int type = getItemViewType(i);
            switch (type) {
                case Type0:
                    view = View.inflate(getActivity(), R.layout.fs1_items, null);
                    TextView tv1_1 = view.findViewById(R.id.fs1_tv1);
                    TextView tv1_2 = view.findViewById(R.id.fs1_tv2);
                    ImageView iv = view.findViewById(R.id.fs1_iv);
                    tv1_1.setText(list.get(i).getTitle());
                    tv1_2.setText(list.get(i).getContent());
                    instance.displayImage(list.get(i).getImg(), iv);
                    break;
                case Type1:
                    view = View.inflate(getActivity(), R.layout.fs2_items, null);
                    TextView tv2_1 = view.findViewById(R.id.fs2_tv1);
                    TextView tv2_2 = view.findViewById(R.id.fs2_tv2);
                    tv2_1.setText(list.get(i).getTitle());
                    tv2_2.setText(list.get(i).getContent());
                    break;
            }
            return view;
        }
    }
}

你可能感兴趣的:(嵌套的子类)