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

//主布局

<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();
          }
      });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值