ViewPager 側拉 多條目載入
MainActivity裡
package com.example.week02;
import android.os.Environment; import android.support.annotation.NonNull; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RadioGroup; import android.widget.Toast;
import com.bwie.xlistviewlibrary.view.XListView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity { String urlBitmap = “http://img.my.csdn.net/uploads/201407/26/1406383265_8550.jpg
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ViewPager img1 = findViewById(R.id.img1);
ImageLoader instance = ImageLoader.getInstance();
DisplayImageOptions options=ImageloaderUtils.getdisplay();
//設定圓角圖片
//sd卡里 instance.displayImage("file://"+R.drawable.f,img1,options);
//網路裡 instance.displayImage(urlBitmap,img1,options);
//drawable裡 instance.displayImage("drawable://"+R.drawable.f,img1,options);
instance.displayImage("drawable://"+R.drawable.f,img1,options);
linear = findViewById(R.id.linear);
list_view = findViewById(R.id.list_view);
drawer = findViewById(R.id.drawer);
view_pager = findViewById(R.id.view_pager);
radio_group = findViewById(R.id.radio_group);
flist.add(new Fragment01());
flist.add(new Fragment02());
flist.add(new Fragment03());
MyFadapter myFadapter = new MyFadapter(getSupportFragmentManager());
view_pager.setAdapter(myFadapter);
view_pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int i, float v, int i1) {
radio_group.check(radio_group.getChildAt(i).getId());
}
@Override
public void onPageSelected(int i) {
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
Toast.makeText(MainActivity.this, "點", Toast.LENGTH_SHORT).show();
view_pager.setCurrentItem(0);
drawer.closeDrawers();
break;
case 1:
view_pager.setCurrentItem(1);
drawer.closeDrawers();
break;
case 2:
view_pager.setCurrentItem(2);
drawer.closeDrawers();
break;
}
}
});
radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.btn1:
view_pager.setCurrentItem(0,false);
break;
case R.id.btn2:
view_pager.setCurrentItem(1,false);
break;
case R.id.btn3:
view_pager.setCurrentItem(2,false);
break;
}
}
});
initData();
drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
@Override
public void onDrawerSlide(@NonNull View view, float v) {
}
@Override
public void onDrawerOpened(@NonNull View view) {
}
@Override
public void onDrawerClosed(@NonNull View view) {
//Toast.makeText(MainActivity.this, "關閉了", Toast.LENGTH_SHORT).show();
}
@Override
public void onDrawerStateChanged(int i) {
}
});
/* list_view.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
return true;
}
});*/
}
private void initData() {
for (int i = 1; i < 10; i++) {
alist.add("fragment0" + i);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, alist);
list_view.setAdapter(arrayAdapter);
}
private class MyFadapter extends FragmentPagerAdapter {
public MyFadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
return flist.get(i);
}
@Override
public int getCount() {
return flist.size();
}
}
}
Fragment01裡的
package com.example.week02;
import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.widget.DrawerLayout; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast;
import com.bwie.xlistviewlibrary.utils.NetWordUtils; import com.bwie.xlistviewlibrary.view.XListView; import com.google.gson.Gson; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList; import java.util.List;
public class Fragment01 extends Fragment { String urlString = “http://api.expoon.com/AppNews/getNewsList/type/1/p/”; ArrayList<Goods.DataBean> alist=new ArrayList<Goods.DataBean>(); private XListView xls; private MyBadapter badapter; private ImageLoader instance; int page;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment01, container, false);
xls = view.findViewById(R.id.xls);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
badapter = new MyBadapter();
instance = ImageLoader.getInstance();
xls.setAdapter(badapter);
erinitData(page);
xls.setPullLoadEnable(true);
xls.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
alist.clear();
erinitData(0);
}
@Override
public void onLoadMore() {
page++;
erinitData(page);
}
});
}
private void erinitData(int page) {
String murl=urlString+page;
new MAsyncTask().execute(murl);
}
private class MyBadapter extends BaseAdapter {
ImageView imageView;
TextView textView;
View view1;
@Override
public int getItemViewType(int position) {
return position%2;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return alist.size();
}
@Override
public Object getItem(int position) {
return alist.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int itemViewType = getItemViewType(position);
switch (itemViewType){
case 0:
view1= View.inflate(getActivity(), R.layout.item2, null);
imageView=view1.findViewById(R.id.img2);
textView=view1.findViewById(R.id.text2);
textView.setText(alist.get(position).getNews_title());
DisplayImageOptions options=ImageloaderUtils.getdisplay();
instance.displayImage(alist.get(position).getPic_url(),imageView,options);
break;
case 1:
view1= View.inflate(getActivity(), R.layout.item2, null);
imageView=view1.findViewById(R.id.img2);
textView=view1.findViewById(R.id.text2);
textView.setText(alist.get(position).getNews_title());
imageView.setVisibility(View.GONE);
break;
}
return view1;
}
}
private class MAsyncTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... strings) {
return NetWordUtils.getNetjson(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Goods goods = gson.fromJson(s, Goods.class);
List<Goods.DataBean> data = goods.getData();
alist.addAll(data);
badapter.notifyDataSetChanged();
UIcomplete();
}
}
public void UIcomplete(){
xls.setRefreshTime("剛剛");
xls.stopLoadMore();
xls.stopRefresh();
}
}
activity_main.xml里布局
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android” android:id="@+id/drawer" android:layout_width=“match_parent” android:layout_height=“match_parent”>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_weight="9"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<RadioButton
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/dian"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="首頁" />
<RadioButton
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/dian"
android:button="@null"
android:gravity="center"
android:text="視訊" />
<RadioButton
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/dian"
android:button="@null"
android:gravity="center"
android:text="我的" />
</RadioGroup>
<ImageView
android:id="@+id/img1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/f" />
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
fragment01佈局
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android” android:layout_width=“match_parent” android:layout_height=“match_parent”> <com.bwie.xlistviewlibrary.view.XListView android:id="@+id/xls" android:layout_width=“match_parent” android:layout_height=“match_parent”>
</com.bwie.xlistviewlibrary.view.XListView>
</android.support.constraint.ConstraintLayout>