<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"
tools:context="maxinawei.bw.com.lunbotu.MainActivity"
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="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/page">
</android.support.v4.view.ViewPager>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:orientation="horizontal">
</LinearLayout>
</FrameLayout>
<maxinawei.bw.com.lunbotu.MyListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listv"></maxinawei.bw.com.lunbotu.MyListView>
</LinearLayout>
</com.handmark.pulltorefresh.library.PullToRefreshScrollView>
</LinearLayout>
Activity-----------------------------------------------------
public class ListActivity extends AppCompatActivity {
private PullToRefreshScrollView psv;
private int indexs=1;
private int opType=1;
private String url="http://api.tianapi.com/huabian/?key=1c18879fe9abf299b86ddbfec3860f03&num=10"+indexs;
private ListView listview;
private List<DateBase.NewslistBean> data=new ArrayList<>();
private MyAdapter adapter;
private ViewPager page;
private List<String> myUrl=new ArrayList<>();
private List<ImageView> liss;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
int pgItme = page.getCurrentItem();
pgItme++;
page.setCurrentItem(pgItme);
Selectlin(pgItme%liss.size());
sendEmptyMessageDelayed(1,1000);
}
};
private LinearLayout lin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
psv = findViewById(R.id.psv);
listview = findViewById(R.id.listv);
page = findViewById(R.id.page);
lin = findViewById(R.id.linpont);
//初始化pulltoScollView
initpsv();
//请求数据
requesDate();
}
private void requesDate() {
MyTask task=new MyTask();
task.execute(url);
}
class MyTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
try {
URL url=new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(5000);
urlConnection.setConnectTimeout(5000);
if(urlConnection.getResponseCode()==200){
InputStream inputStream = urlConnection.getInputStream();
String s = streamToString(inputStream);
return s;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson=new Gson();
DateBase dateBase = gson.fromJson(s, DateBase.class);
List<DateBase.NewslistBean> list = dateBase.getNewslist();
initpage(list);
if (opType==1){
data.clear();
}
data.addAll(list);
RequestAdapter();
psv.onRefreshComplete();
}
}
private void initpage(List<DateBase.NewslistBean> list) {
myUrl.clear();
myUrl.add(list.get(0).getPicUrl());
myUrl.add(list.get(1).getPicUrl());
myUrl.add(list.get(2).getPicUrl());
myUrl.add(list.get(3).getPicUrl());
myUrl.add(list.get(4).getPicUrl());
initpoin();
PaPageAdapter adapter=new PaPageAdapter(myUrl,ListActivity.this);
page.setAdapter(adapter);
handler.sendEmptyMessageDelayed(1,1000);
}
private void initpoin() {
liss=new ArrayList<>();
lin.removeAllViews();
for (int i = 0; i < myUrl.size(); i++) {
ImageView imageView=new ImageView(this);
if (i==0){
imageView.setImageResource(R.mipmap.point_selected);
}else{
imageView.setImageResource(R.mipmap.point_mormal);
}
lin.addView(imageView);
liss.add(imageView);
}
}
public void Selectlin(int pgItem){
for (int i = 0; i <liss.size() ; i++) {
if (i==pgItem){
liss.get(i).setImageResource(R.mipmap.point_selected);
}else{
liss.get(i).setImageResource(R.mipmap.point_mormal);
}
}
}
private void RequestAdapter() {
if (adapter==null){
adapter= new MyAdapter(data,ListActivity.this);
listview.setAdapter(adapter);
}else {
adapter.notifyDataSetChanged();
}
}
//-----------------------------------读取json数据------------------------------------------------------------
private String streamToString(InputStream inputStream) {
StringBuilder sbuilder=new StringBuilder();
String str;
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
try {
while ((str=reader.readLine())!=null){
sbuilder.append(str);
}
} catch (Exception e) {
e.printStackTrace();
}
return sbuilder.toString();
}
//初始化psv===============================================================================
private void initpsv() {
psv.setMode(PullToRefreshBase.Mode.BOTH);
psv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {
opType=1;
indexs=1;
url="http://api.tianapi.com/huabian/?key=1c18879fe9abf299b86ddbfec3860f03&num=10"+indexs;
handler.removeCallbacksAndMessages(null);
requesDate();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ScrollView> pullToRefreshBase) {
opType=2;
indexs++;
url="http://api.tianapi.com/huabian/?key=1c18879fe9abf299b86ddbfec3860f03&num=10"+indexs;
handler.removeCallbacksAndMessages(null);
requesDate();
}
});
}
}