Image infinite carousel plus pull down to refresh

 

public class Second extends Activity{

    private int pageIndex=1;

    private int operType=1;

    private String myurl="http://365jia.cn/news/api3/365jia/news/headline?page="+pageIndex;

 

    private List<String> imgUrls=new ArrayList<>();

    private List<Urse.DataBeanX.DataBean> datas=new ArrayList<>();

    private PullToRefreshScrollView psv;

    private ListView listView;

    private ViewPager vp;

    private LinearLayout linPoint;

    private ListViewAdapter adapter;

    private List<ImageView> lists=new ArrayList<>();

 

 

    @SuppressLint("HandlerLeak")

    private Handler mhandler =new Handler(){

        @Override

        public void handleMessage(Message msg) {

            super.handleMessage(msg);

 

            int cIndex = vp.getCurrentItem();

            cIndex++;

            vp.setCurrentItem(cIndex);

 

            setSelectedPoint(cIndex%lists.size());

 

            sendEmptyMessageDelayed (0.1000);

        }

    };

 

 

    @Override

    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate (savedInstanceState);

        setContentView(R.layout.main2);

        psv = findViewById(R.id.psv);

        listView = findViewById(R.id.lv);

        vp = findViewById(R.id.vp);

        linPoint = findViewById(R.id.lin_point);

//initialization

        initPsv ();

// request data

        recreateData();

 

    }

 

    private void recreatData() {

        new MyTask().execute(myurl);

    }

 

    public void setSelectedPoint(int pageIndex) {

        for (int i = 0; i <lists.size() ; i++) {

            if (i==pageIndex){

                lists.get(i).setImageResource(R.drawable.point_selected);

            }else {

                lists.get(i).setImageResource(R.drawable.point_unselected);

            }

        }

    }

 

 

    private class MyTask extends AsyncTask<String, Void, String> {

        // Tasks to execute in the background

        @Override

        protected String doInBackground(String... strings) {//String... variable-length string string type array, the length is not fixed

            try {

                URL url = new URL(strings[0]);

                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                InputStream inputStream = urlConnection.getInputStream();

                String s = streamToString(inputStream);

 

                return s;

            } catch (Exception e) {

                e.printStackTrace ();

            }

 

 

            return "123";

        }

 

        //After doInBackground, the method called, ,, the operation of updating the UI, is equivalent to the function in handlerMessage

        @Override

        protected void onPostExecute(String s) {

            super.onPostExecute(s);

            //Analysis + display

            Gson gson = new Gson ();

 

            Urse urse = gson.fromJson(s, Urse.class);

            List<Urse.DataBeanX.DataBean> data = urse.getData().getData();

 

            showPics(data);

 

           

            if (operType == 1) {

                datas.clear();

            }

            datas.addAll(data);

 

            setAdapter();

 

            psv.onRefreshComplete();

        }

    }

    private void showPics(List<Urse.DataBeanX.DataBean> data) {

        imgUrls.clear();

        imgUrls.add("http://365jia.cn/uploads/"+data.get(0).getPics().get(0));

        imgUrls.add("http://365jia.cn/uploads/"+data.get(1).getPics().get(0));

        imgUrls.add("http://365jia.cn/uploads/"+data.get(2).getPics().get(0));

 

        MyAdapter adapter = new MyAdapter(imgUrls,Second.this);

        vp.setAdapter(adapter);

 

        

        initPoint();

        //Handler sends a message

        mhandler.sendEmptyMessageDelayed (0.1000);

    }

 

    private void initPoint() {

        linPoint.removeAllViews();

        lists.clear();

        for (int i = 0; i <imgUrls.size() ; i++) {

            ImageView imageView = new ImageView(this);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

 

            params.setMargins(0,0,10,0);

            if (i==0){

                imageView.setImageResource(R.drawable.point_selected);

            }else {

                imageView.setImageResource(R.drawable.point_unselected);

            }

            linPoint.addView(imageView,params);

            lists.add(imageView);

 

            }

        }

 

 

 

    private void setAdapter() {

        if (adapter==null) {

            adapter = new ListViewAdapter(datas,Second.this);

            listView.setAdapter(adapter);

        }else {

            adapter.notifyDataSetChanged();

        }

    }

 

    private String streamToString(InputStream stream) {

        StringBuilder builder = new StringBuilder();

        String str;

        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));

        try {

            while ((str=reader.readLine())!=null){

                builder.append(str);

                Log.i("jljs", builder.toString());

            }

        } catch (IOException e) {

            e.printStackTrace ();

        }

        return  builder.toString();

    }

 

 

 

    private void initPsv() {

        psv.setMode(PullToRefreshBase.Mode.BOTH);

 

        psv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {

            @Override

            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {

                // refresh callback

                operType=1;

                pageIndex=1;

                myurl="http://365jia.cn/news/api3/365jia/news/headline?page="+pageIndex;

                mhandler.removeCallbacksAndMessages(null);

                recreateData();

 

            }

 

            @Override

            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {

                //load callback

                operType=2;

                pageIndex++;

                myurl="http://365jia.cn/news/api3/365jia/news/headline?page="+pageIndex;

                mhandler.removeCallbacksAndMessages(null);

                recreateData();

            }

        });

    }

}

 

 

 

class MyAdapter extends PagerAdapter {

 

    //data source

    private List<String> urls;

    //context object

    private Context context;

 

    private DisplayImageOptions options;

 

    public MyAdapter(List<String> urls, Context context) {

        this.urls = urls;

        this.context = context;

        this.options=new DisplayImageOptions.Builder()

                .cacheInMemory(true)//Use memory cache

                .cacheOnDisk(true)//Use disk cache

                .showImageOnLoading(R.mipmap.ic_launcher)//Set the image being downloaded

                .showImageForEmptyUri(R.mipmap.ic_launcher)//When the url is empty or the requested resource does not exist

                .showImageOnFail(R.mipmap.ic_launcher)//The image displayed when the download fails

                .bitmapConfig(Bitmap.Config.RGB_565)//Set the image color mode 1px=2 bytes ARGB_8888 1px=4 bytes ARGB_4444 1px=2 bytes ALPHA_8 1px=1 bytes

                .imageScaleType(ImageScaleType.EXACTLY)//Set the zoom mode of the image

                .build();

    }

 

    @Override

    public int getCount() {

        return Integer.MAX_VALUE;

    }

 

 

    @Override

    public boolean isViewFromObject(View view, Object object) {

        return view==object;

    }

 

    @Override

    public Object instantiateItem(ViewGroup container, int position) {

        ImageView imageView=new ImageView(context);

        // download image

        ImageLoader.getInstance().displayImage(urls.get(position%urls.size()),imageView,options);

 

        container.addView(imageView);

        return imageView;

    }

 

    @Override

    public void destroyItem(ViewGroup container, int position, Object object) {

        //super.destroyItem(container, position, object);

        container.removeView((View)object);

    }

}

 

 

 

class ListViewAdapter extends BaseAdapter {

 

    private List<Urse.DataBeanX.DataBean> data;

    private Context context;

    private final DisplayImageOptions options;

 

    public ListViewAdapter( List<Urse.DataBeanX.DataBean> data, Context context) {

        this.data = data;

        this.context = context;

 

        options = new DisplayImageOptions.Builder()

                .cacheInMemory(true)//Use memory cache

                .cacheOnDisk(true)//Use disk cache

                .showImageOnLoading(R.mipmap.ic_launcher)//Set the image being downloaded

                .showImageForEmptyUri(R.mipmap.ic_launcher)//When the url is empty or the requested resource does not exist

                .showImageOnFail(R.mipmap.ic_launcher)//The image displayed when the download fails

                .bitmapConfig(Bitmap.Config.RGB_565)//Set the image color mode 1px=2 bytes ARGB_8888 1px=4 bytes ARGB_4444 1px=2 bytes ALPHA_8 1px=1 bytes

                .imageScaleType(ImageScaleType.EXACTLY)//Set the zoom mode of the image

                .build();

    }

 

 

 

 

    @Override

    public int getCount() {

        return data.size();

    }

 

    @Override

    public Object getItem(int i) {

        return data.get(i);

    }

 

    @Override

    public long getItemId(int i) {

        return i;

    }

 

 

 

    @Override

    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHolder holder;

        if(view==null){

            view=View.inflate(context,R.layout.item,null);

            holder=new ViewHolder();

            holder.textView=view.findViewById(R.id.tv1);

            holder.img1=view.findViewById(R.id.img1);

 

            view.setTag (holder);

        }else {

            holder= (ViewHolder) view.getTag();

        }

 

        holder.textView.setText(data.get(i).getTitle());

        ImageLoader.getInstance().displayImage("http://365jia.cn/uploads/"+data.get(i).getPics().get(0),holder.img1,options);

 

        return view;

    }

 

    class ViewHolder{

        TextView textView;

          ImageView img1;

    } 

}

 

public class MyListView extends ListView{

    public MyListView(Context context) {

        super(context);

    }

 

    public MyListView(Context context, AttributeSet attrs) {

        super(context, attrs);

    }

 

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

 

        int newHeight = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);

 

        super.onMeasure(widthMeasureSpec, newHeight);

 

    }

}

 

 

 

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">


<com.handmark.pulltorefresh.library.PullToRefreshScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/psv">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:id="@+id/vp"
android:layout_height="200dp">

</android.support.v4.view.ViewPager>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lin_point"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="bottom"></LinearLayout>

</FrameLayout>


<com.example.week3_lianxi.MyListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv"></com.example.week3_lianxi.MyListView>
>

</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>

</LinearLayout>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324977248&siteId=291194637