轮播图——上下刷新----

//主布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.bawi.lunfan.MainActivity">
    <com.handmark.pulltorefresh.library.PullToRefreshScrollView
        android:id="@+id/pull_to_refresh_scrollview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="240dp">

            <android.support.v4.view.ViewPager
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="240dp"></android.support.v4.view.ViewPager>

            <LinearLayout
                android:id="@+id/ll_point"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:gravity="center"
                android:layout_alignBottom="@id/view_pager"
                android:layout_alignParentRight="true"
                android:orientation="horizontal" />
        </RelativeLayout>

        <com.bawi.lunfan.MyListview
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"></com.bawi.lunfan.MyListview>
    </LinearLayout>
    </com.handmark.pulltorefresh.library.PullToRefreshScrollView>

</LinearLayout>

//子布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/t_iamge"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        app:srcCompat="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/t_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/t_iamge"
        android:layout_toRightOf="@+id/t_iamge"
        android:text="TextView" />

    <TextView
        android:id="@+id/t_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/t_iamge"
        android:layout_toEndOf="@+id/t_iamge"
        android:layout_toRightOf="@+id/t_iamge"
        android:text="TextView" />
</RelativeLayout>

//主Activity


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity===";
    public static String url = "https://www.zhaoapi.cn/ad/getAd";
    public  static String two_url="https://api.tianapi.com/wxnew/?key=48a7d7193e11bd2dd4a683b6e2f90a4f&num=10";
    private ViewPager viewPager;
    private List<ImageView> ps = new ArrayList<>();
    private List<ImageView> ads = new ArrayList<>();
    private LinearLayout ll_points;
    private AdAdapter adAdapter;
    private ListAdapter listAdapter;
    private MyListview listView;
    private int page=1;
    private PullToRefreshScrollView pullToRefreshScrollView;
    List<NewsBean.NewslistBean> list=new ArrayList<>();
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            viewPager.setCurrentItem(viewPager.getCurrentItem() + 1);
            for (int i = 0; i < ps.size(); i++) {
                if (i == viewPager.getCurrentItem() % ps.size()) {
                   // ps.get(i).setSelected(true);
                    ps.get(i).setImageResource(R.drawable.points);
                } else {
                   // ps.get(i).setSelected(false);
                    ps.get(i).setImageResource(R.drawable.gravity);
                }
            }
            handler.sendEmptyMessageDelayed(0, 1000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
        listAdapter=new ListAdapter(MainActivity.this,list);
        listView.setAdapter(listAdapter);
        initDatas();


    }

    private void initDatas() {
        HttpUtils httpUtils = HttpUtils.getInstance();
        //获取网址
        httpUtils.get(url);
        httpUtils.setHttpUtilsListener(new HttpUtils.HttpUtilListener() {
            @Override
            public void getSuccess(String json) {
                //log打印是否获取到数据
                Log.d(TAG, "成功" + json);
                //GSON解析
                Gson gson = new Gson();
                AdBean adBean = gson.fromJson(json, AdBean.class);
                List<AdBean.DataBean> data = adBean.getData();
                for (int i = 0; i < data.size(); i++) {
                    String icon = data.get(i).getIcon();
                    ImageView imageView = new ImageView(MainActivity.this);
                    ImageLoader.getInstance().displayImage(icon, imageView, MyApp.getOptions());
                    ads.add(imageView);
                    //添加小圆点
                    ImageView p = new ImageView(MainActivity.this);
                    p.setImageResource(R.drawable.gravity);
                    ps.add(p);
                    LinearLayout.LayoutParams lp = new
                            LinearLayout.LayoutParams(20, 20);
                    lp.setMargins(15, 0, 15, 0);
                    // p.setLayoutParams(lp);
                    //将小圆点放入布局里
                     /*TextView textView = new TextView(MainActivity.this);
                     textView.setText("Hello");*/
                    ll_points.addView(p, lp);
                }
                //默认选中第一个
                ps.get(0).setSelected(true);
                //设置适配器
                adAdapter = new AdAdapter(ads);
                viewPager.setAdapter(adAdapter);
                //hander转动
                handler.sendEmptyMessageDelayed(0, 1000);
                //不断更新
                adAdapter.notifyDataSetChanged();
                initNewsDatas();

            }

            @Override
            public void getError(String error) {

            }
        });
    }

    private void initNewsDatas() {
        HttpUtils httpUtils=HttpUtils.getInstance();
        httpUtils.get(two_url);
        httpUtils.setHttpUtilsListener(new HttpUtils.HttpUtilListener() {
            @Override
            public void getSuccess(String json) {
                Log.d(TAG, "成功"+json);
                Gson gson=new Gson();
                NewsBean newsBean=gson.fromJson(json,NewsBean.class);
                 List<NewsBean.NewslistBean> s_data=newsBean.getNewslist();

                 if (page==1){
                     list.clear();
                 }
                list.addAll(s_data);
                listAdapter.notifyDataSetChanged();
                pullToRefreshScrollView.onRefreshComplete();
            }
            @Override
            public void getError(String error) {
                Log.d(TAG, "失败"+error);
            }
        });
    }


    private void initViews() {
        viewPager = findViewById(R.id.view_pager);
        ll_points = findViewById(R.id.ll_point);
        listView=findViewById(R.id.list_view);
      pullToRefreshScrollView=findViewById(R.id.pull_to_refresh_scrollview);
      pullToRefreshScrollView.setMode(PullToRefreshScrollView.Mode.BOTH);
      pullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
          @Override
          public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
              page=1;
              initNewsDatas();
          }

          @Override
          public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
              page++;
              initNewsDatas();
          }
      });
    }
}

猜你喜欢

转载自blog.csdn.net/air_show/article/details/80785647