TabLayout、Xlistview根據圖片數量多條目載入
package liyuanqi.bwie.com.zhoukao3lx;
import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View;
import java.util.ArrayList; import java.util.List;
public class MainActivity extends AppCompatActivity { ViewPager pagerx; TabLayout tabs; MyPagerAdapter myPagerAdapter; List fragmnetList; List titlelist; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pagerx = findViewById(R.id.pagerx); tabs = findViewById(R.id.tabs); initdata(); pagerdata(); }
private void pagerdata() { myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager()); myPagerAdapter.listData(titlelist,fragmnetList); tabs.setTabMode(TabLayout.MODE_SCROLLABLE); tabs.setupWithViewPager(pagerx); pagerx.setAdapter(myPagerAdapter); } private void initdata() { titlelist=new ArrayList<>(); titlelist.add("全部"); titlelist.add("國內"); titlelist.add("國際"); titlelist.add("科技"); titlelist.add("股市"); titlelist.add("美股"); titlelist.add("財經"); titlelist.add("社會"); titlelist.add("體育"); titlelist.add("娛樂"); titlelist.add("軍事"); titlelist.add("科技"); fragmnetList=new ArrayList<>(); fragmnetList.add(new Fragmnet(this,"2509")); fragmnetList.add(new Fragmnet(this,"2510")); fragmnetList.add(new Fragmnet(this,"2511")); fragmnetList.add(new Fragmnet(this,"2515")); fragmnetList.add(new Fragmnet(this,"2517")); fragmnetList.add(new Fragmnet(this,"2518")); fragmnetList.add(new Fragmnet(this,"2516")); fragmnetList.add(new Fragmnet(this,"2669")); fragmnetList.add(new Fragmnet(this,"2512")); fragmnetList.add(new Fragmnet(this,"2513")); fragmnetList.add(new Fragmnet(this,"2514")); fragmnetList.add(new Fragmnet(this,"2515")); }
}
package liyuanqi.bwie.com.zhoukao3lx;
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.PagerAdapter;
import java.util.List;
class MyPagerAdapter extends FragmentPagerAdapter{ private List mytitlelist; List myfragmnetList; public MyPagerAdapter(FragmentManager fm) { super(fm); } public void listData(List titlelist, List fragmnetList) { this.myfragmnetList=fragmnetList; this.mytitlelist=titlelist; } @Override public Fragment getItem(int position) { return myfragmnetList.get(position); }
@Override
public int getCount() {
return myfragmnetList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return mytitlelist.get(position);
}
}
package liyuanqi.bwie.com.zhoukao3lx;
import android.annotation.SuppressLint; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast;
import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Date; import java.util.List;
import me.maxwin.view.XListView;
@SuppressLint(“ValidFragment”) public class Fragmnet extends Fragment { private Context context; private String lid; XListView xlsv; int pager=1; MyxlvAdapter myxlvAdapter; String wz2; public Fragmnet(Context context, String lid) { this.context = context; this.lid = lid; }
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.fram, null);
xlsv = view.findViewById(R.id.xlsv);
Log.i("lyq",""+lid);
xlsv.setPullRefreshEnable(true);
xlsv.setPullLoadEnable(true);
xlsv.setRefreshTime(new Date().toString());
xlsv.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
pager=1;
httpchuli(pager);
}
@Override
public void onLoadMore() {
httpchuli(pager);
}
});
myxlvAdapter = new MyxlvAdapter(getActivity());
xlsv.setAdapter(myxlvAdapter);
httpchuli(pager);
return view;
}
private void httpchuli(int pager) {
Log.i("lyq",""+lid);
// String ida="&lid="+lid;
//&lid=2509
String wz1="https://feed.mix.sina.com.cn/api/roll/get?pageid=153&k=&num=20&page=";
wz2=wz1+pager+"&lid="+lid;
new AsyncTask<String, Void, List<JsonBean.ResultBean.DataBean>>() {
@Override
protected List<JsonBean.ResultBean.DataBean> doInBackground(String... strings) {
return HttpUtile.httpdate(strings[0]);
}
@Override
protected void onPostExecute(List<JsonBean.ResultBean.DataBean> dataBeans) {
super.onPostExecute(dataBeans);
Log.e("lyqqweqw",""+dataBeans);
if (dataBeans==null){
Toast.makeText(getActivity(),"資料請求失敗",Toast.LENGTH_SHORT).show();
return;
}
adapterdate(dataBeans);
pagerdizheng();
}
}.execute(wz2);
}
private void pagerdizheng() {
pager++;
xlsv.stopRefresh();
xlsv.stopLoadMore();
}
private void adapterdate(List<JsonBean.ResultBean.DataBean> dataBeans) {
if (pager==1){
myxlvAdapter.datas(dataBeans);
}else {
myxlvAdapter.adddatas(dataBeans);
}
}
}
package liyuanqi.bwie.com.zhoukao3lx;
import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList; import java.util.List;
class MyxlvAdapter extends BaseAdapter{ private Context context; List<JsonBean.ResultBean.DataBean> list; final static int ONE=0; final static int TOW=1; final static int THREE=2; final static int FOUR=3; final static int FIVE=4;
public MyxlvAdapter(Context context) {
this.context = context;
list=new ArrayList<>();
}
public void datas(List<JsonBean.ResultBean.DataBean> dataBeans) {
this.list.clear();
this.list.addAll(dataBeans);
notifyDataSetChanged();
}
public void adddatas(List<JsonBean.ResultBean.DataBean> dataBeans) {
if (dataBeans!=null){
this.list.addAll(dataBeans);
notifyDataSetChanged();
}
}
//0 dao cout-1
@Override
public int getItemViewType(int position) {
JsonBean.ResultBean.DataBean dataBean = list.get(position);
if (dataBean.getImages().size()>=4){
return FOUR;
}else if (dataBean.getImages().size()==3){
return THREE;
} else if (dataBean.getImages().size()==2){
return TOW;
} else if (dataBean.getImages().size()==1){
return ONE;
}else {
return FIVE;
}
}
@Override
public int getViewTypeCount() {
return 5;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder1 holder1;
ViewHolder2 holder2;
ViewHolder3 holder3;
ViewHolder4 holder4;
ViewHolder5 holder5;
int itemViewType = getItemViewType(position);
switch (itemViewType){
case ONE:
if (convertView==null){
holder1=new ViewHolder1();
convertView=View.inflate(context,R.layout.shuju,null);
holder1.title1=convertView.findViewById(R.id.tit1);
holder1.img1=convertView.findViewById(R.id.img1);
convertView.setTag(holder1);
}else {
holder1= (ViewHolder1) convertView.getTag();
}
holder1.title1.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(list.get(position).getImages().get(0).getU(),holder1.img1);
break;
case TOW:
if (convertView==null){
holder2=new ViewHolder2();
convertView=View.inflate(context,R.layout.shuju2,null);
holder2.title2=convertView.findViewById(R.id.tit2);
holder2.img2=convertView.findViewById(R.id.img2);
holder2.img3=convertView.findViewById(R.id.img3);
convertView.setTag(holder2);
}else {
holder2= (ViewHolder2) convertView.getTag();
}
holder2.title2.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(list.get(position).getImages().get(0).getU(),holder2.img2);
ImageLoader.getInstance().displayImage(list.get(position).getImages().get(1).getU(),holder2.img3);
break;
case THREE:
if (convertView==null){
holder3=new ViewHolder3();
convertView=View.inflate(context,R.layout.shuju3,null);
holder3.title3=convertView.findViewById(R.id.tit3);
holder3.img4=convertView.findViewById(R.id.img4);
holder3.img5=convertView.findViewById(R.id.img5);
holder3.img6=convertView.findViewById(R.id.img6);
convertView.setTag(holder3);
}else {
holder3= (ViewHolder3) convertView.getTag();
}
holder3.title3.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(list.get(position).getImages().get(0).getU(),holder3.img4);
ImageLoader.getInstance().displayImage(list.get(position).getImages().get(1).getU(),holder3.img5);
ImageLoader.getInstance().displayImage(list.get(position).getImages().get(2).getU(),holder3.img6);
break;
case FOUR:
if (convertView==null){
holder4=new ViewHolder4();
convertView=View.inflate(context,R.layout.shuju4,null);
holder4.title4=convertView.findViewById(R.id.tit4);
holder4.img7=convertView.findViewById(R.id.img7);
holder4.img8=convertView.findViewById(R.id.img8);
holder4.img9=convertView.findViewById(R.id.img9);
holder4.img10=convertView.findViewById(R.id.img10);
convertView.setTag(holder4);
}else {
holder4= (ViewHolder4) convertView.getTag();
}
holder4.title4.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(list.get(position).getImages().get(0).getU(),holder4.img7);
ImageLoader.getInstance().displayImage(list.get(position).getImages().get(1).getU(),holder4.img8);
ImageLoader.getInstance().displayImage(list.get(position).getImages().get(2).getU(),holder4.img9);
ImageLoader.getInstance().displayImage(list.get(position).getImages().get(3).getU(),holder4.img10);
break;
case FIVE:
if (convertView==null){
holder5=new ViewHolder5();
convertView=View.inflate(context,R.layout.shuju5,null);
holder5.title5=convertView.findViewById(R.id.tit5);
convertView.setTag(holder5);
}else {
holder5= (ViewHolder5) convertView.getTag();
}
holder5.title5.setText(list.get(position).getTitle());
break;
}
return convertView;
}
class ViewHolder1{
TextView title1;
ImageView img1;
}
class ViewHolder2{
TextView title2;
ImageView img2;
ImageView img3;
}
class ViewHolder3{
TextView title3;
ImageView img4;
ImageView img5;
ImageView img6;
}
class ViewHolder4{
TextView title4;
ImageView img7;
ImageView img8;
ImageView img9;
ImageView img10;
}
class ViewHolder5{
TextView title5;
}
}
package liyuanqi.bwie.com.zhoukao3lx;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
public class App extends Application { @Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).build(); ImageLoader.getInstance().init(configuration); } }