XLisetView+ViewPager无限轮播+多条目加载

导xlistview

jar包

权限


//主布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    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="bbb.bwie.com.yuokao1.MainActivity">

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

    </android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="漫画"
            android:layout_weight="1"
            android:onClick="info"
            />
        <Button
            android:id="@+id/b2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发现"
            android:layout_weight="1"
            android:onClick="info"
            />
        <Button
            android:id="@+id/b3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="社区"
            android:layout_weight="1"
            android:onClick="info"
            />
        <Button
            android:id="@+id/b4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我的"
            android:layout_weight="1"
            android:onClick="info"
            />
    </LinearLayout>


</LinearLayout>


//xlistview布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <bbb.bwie.com.yuokao1.XListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </bbb.bwie.com.yuokao1.XListView>

</LinearLayout>


//fragment2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第二页"
        />

</LinearLayout>




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第三页"
        />

</LinearLayout>



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第四页"
        />

</LinearLayout>



//viewPager和点布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp2"
        android:layout_width="match_parent"
        android:layout_height="250dp"

        >

    </android.support.v4.view.ViewPager>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:id="@+id/lll"
        android:gravity="center"
        android:orientation="horizontal"
        >

    </LinearLayout>

</LinearLayout>



//布局1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>




//布局2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/img1"
        android:layout_width="250dp"
        android:layout_height="250dp" />

    <ImageView
        android:id="@+id/img2"
        android:layout_width="250dp"
        android:layout_height="250dp" />


</LinearLayout>





import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {

    private ViewPager vp;
    private Button b1;
    private Button b2;
    private Button b3;
    private Button b4;

    List<Fragment> list=new ArrayList<Fragment>();
    List<Button> list2=new ArrayList<Button>();
    private mydata mydata;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vp = (ViewPager) findViewById(R.id.vp);
        b1 = (Button) findViewById(R.id.b1);
        b2 = (Button) findViewById(R.id.b2);
        b3 = (Button) findViewById(R.id.b3);
        b4 = (Button) findViewById(R.id.b4);

        //创建数据源
        //添加4fragment
        indata();
        mydata = new mydata(getSupportFragmentManager());
        vp.setAdapter(mydata);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                for(int i=0;i<list2.size();i++){
                    if(position==i){
                        list2.get(i).setTextColor(Color.RED);
                    }else{
                        list2.get(i).setTextColor(Color.BLACK);
                    }
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void info(View v){
        switch (v.getId()){
            case R.id.b1:
                vp.setCurrentItem(0);
                break;
            case R.id.b2:
                vp.setCurrentItem(1);
                break;
            case R.id.b3:
                vp.setCurrentItem(2);
                break;
            case R.id.b4:
                vp.setCurrentItem(3);
                break;
        }
    }


    private void indata() {
        list.add(new fragment1());
        list.add(new fragment2());
        list.add(new fragment3());
        list.add(new fragment4());

        list2.add(b1);
        list2.add(b2);
        list2.add(b3);
        list2.add(b4);
        b1.setTextColor(Color.RED);//让它一进来就是红色

    }
    class mydata extends FragmentPagerAdapter{

        public mydata(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }
}




import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by HQ on 2017/9/17.
 */

public class fragment1 extends Fragment implements XListView.IXListViewListener{

    private View view;
    private XListView xlv;
    private ViewPager vp;
    private StringBuilder builder;
    private List<Bean.DataBean.ComicsBean> comics;
    private ArrayList<User2> list2;
    private ArrayList<User> list;
    private mydata2 mydata2;

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int a = msg.what;
            if(a==0){
          //XListView刷新适配器
            mydata2.notifyDataSetChanged();
                close();
            }
            if(a==1){
                int i = vp.getCurrentItem() + 1;
                vp.setCurrentItem(i);
                handler.sendEmptyMessageDelayed(1,2000);
            }
        }
    };

    private void close() {

        xlv.stopLoadMore();
        xlv.stopRefresh();
        xlv.setRefreshTime("2017-09-17");
    }

    private ArrayList<ImageView> list3;
    private ImageView iv;
    private LinearLayout lll;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment1, container, false);

        return view;
          }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        xlv = (XListView) view.findViewById(R.id.lv);
        xlv.setPullLoadEnable(true);
        xlv.setPullRefreshEnable(true);
        xlv.setXListViewListener(this);

//XListView添加一个头布局viewpager(先不写)
 View vvp = View.inflate(getActivity(), R.layout.vp, null);
        vp = (ViewPager) vvp.findViewById(R.id.vp2);
        lll = (LinearLayout) vvp.findViewById(R.id.lll);
        xlv.addHeaderView(vvp);

        //读取数据和解析
        new Thread(){

            @Override
            public void run() {
                super.run();
                try {
                    URL url = new URL("http://api.kkmh.com/v1/daily/comic_lists/0?since=0&gender=0&sa_event=eyJwcm9qZWN0Ijoia3VhaWthbl9hcHAiLCJ0aW1lIjoxNDg3NzQyMjQwNjE1LCJwcm9wZXJ0aWVzIjp7IkhvbWVwYWdlVGFiTmFtZSI6IueDremXqCIsIlZDb21tdW5pdHlUYWJOYW1lIjoi54Ot6ZeoIiwiJG9zX3ZlcnNpb24iOiI0LjQuMiIsIkdlbmRlclR5cGUiOiLlpbPniYgiLCJGcm9tSG9tZXBhZ2VUYWJOYW1lIjoi54Ot6ZeoIiwiJGxpYl92ZXJzaW9uIjoiMS42LjEzIiwiJG5ldHdvcmtfdHlwZSI6IldJRkkiLCIkd2lmaSI6dHJ1ZSwiJG1hbnVmYWN0dXJlciI6ImJpZ25veCIsIkZyb21Ib21lcGFnZVVwZGF0ZURhdGUiOjAsIiRzY3JlZW5faGVpZ2h0IjoxMjgwLCJIb21lcGFnZVVwZGF0ZURhdGUiOjAsIlByb3BlcnR5RXZlbnQiOiJSZWFkSG9tZVBhZ2UiLCJGaW5kVGFiTmFtZSI6IuaOqOiNkCIsImFidGVzdF9ncm91cCI6MTEsIiRzY3JlZW5fd2lkdGgiOjcyMCwiJG9zIjoiQW5kcm9pZCIsIlRyaWdnZXJQYWdlIjoiSG9tZVBhZ2UiLCIkY2FycmllciI6IkNoaW5hIE1vYmlsZSIsIiRtb2RlbCI6IlZQaG9uZSIsIiRhcHBfdmVyc2lvbiI6IjMuNi4yIn0sInR5cGUiOiJ0cmFjayIsImRpc3RpbmN0X2lkIjoiQTo2YWRkYzdhZTQ1MjUwMzY1Iiwib3JpZ2luYWxfaWQiOiJBOjZhZGRjN2FlNDUyNTAzNjUiLCJldmVudCI6IlJlYWRIb21lUGFnZSJ9");
                    try {
                        URLConnection connection = url.openConnection();
                        InputStream inputStream = connection.getInputStream();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
                        builder = new StringBuilder();
                        String tem="";
                        while((tem=reader.readLine())!=null){
                            builder.append(tem);
                        }
                        Log.e("========读取到的数据=======",builder.toString());

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        list2 = new ArrayList<User2>();
                        list = new ArrayList<>();
                        Gson gson = new Gson();
                        Bean json = gson.fromJson(builder.toString(), Bean.class);
                        comics = json.getData().getComics();
                        //展示在xlistview上面的数据源
                        for(int j=0;j<comics.size();j++){
                            String title = comics.get(j).getTitle();
                            Bean.DataBean.ComicsBean.TopicBean topic = comics.get(j).getTopic();
                            String cover_image_url = topic.getCover_image_url();
                            list2.add(new User2(title,cover_image_url));

                            }

                        for(int i=0;i<3;i++){
                            Bean.DataBean.ComicsBean.TopicBean topic = comics.get(i).getTopic();
                            String cover_image_url = topic.getCover_image_url();
                            list.add(new User(cover_image_url));//list========无限轮播要用的集合


                        }
                        Log.e("========解析到的数据=========",list2.toString());
                        //xlistview的适配器,展示数据,记得把上拉下拉也做上,Handler
                        mydata2 = new mydata2();
                        xlv.setAdapter(mydata2);
                       (先不写)
                        nodata();//动态添加无限轮播的小点
                        vp.setAdapter( new mydata());
                        vp.setCurrentItem(10000);
                        handler.sendEmptyMessageDelayed(1,2000);//让它2秒展示一次
                        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                            }

                            @Override
                            public void onPageSelected(int position) {

                                for (int x = 0; x < list3.size(); x++) {

                                    if (position % list.size() == x) {
                                        //证明点和图片是对应的,应该点是变红的
                                        list3.get(position % list.size()).setImageResource(R.drawable.dot_focus);
                                    } else {
                                        list3.get(x).setImageResource(R.drawable.dot_normal);
                                    }
                                }
                            }

                            @Override
                            public void onPageScrollStateChanged(int state) {

                            }
                        });

                    }

                });

            }
        }.start();



    }

    //加点
    private void nodata() {
        //list3就只放一个imagview的控件
        list3 = new ArrayList<ImageView>();
        if (list3 != null) {
            list3.clear();
        }

        for (int i = 0; i < list.size(); i++) {
            iv = new ImageView(getActivity());
            if (i == vp.getCurrentItem()) {
                iv.setImageResource(R.drawable.dot_focus);
            } else {
                iv.setImageResource(R.drawable.dot_normal);
            }
            LinearLayout.LayoutParams parmes = new LinearLayout.LayoutParams(20, 20);
            parmes.setMargins(10, 0, 10, 0);
            list3.add(iv);
            lll.addView(iv, parmes);

        }
    }


        //vp的适配器
        class mydata extends PagerAdapter {
            @Override
            public int getCount() {
                return Integer.MAX_VALUE;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view==object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
               container.removeView((View)object);
                // super.destroyItem(container, position, object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = View.inflate(getActivity(), R.layout.textview, null);
                ImageView img = (ImageView) view.findViewById(R.id.img);
                ImageLoader.getInstance().displayImage(list.get(position % list.size()).url, img);
                container.addView(view);
                return view;
            }
        } 
         
    //xlistview的适配器
  class mydata2 extends BaseAdapter {
final int type1 = 0;//多条目加载
final int type2 = 1;
@Override
public int getCount() {
return list2.size();
}
@Override
public Object getItem(int i) {
return list2.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override

public View getView(int i, View view, ViewGroup viewGroup) {
int type = getItemViewType(i);
ViewHolder vh;
if (view == null) {
vh = new ViewHolder();
switch (type) {
case 0:
view = View.inflate(getActivity(), R.layout.textview, null);//第一个布局
vh.name = (TextView) view.findViewById(R.id.name);
vh.img = (ImageView) view.findViewById(R.id.img);
break;
case 1:
view = View.inflate(getActivity(), R.layout.textview2, null);//第二个布局
vh.img = (ImageView) view.findViewById(R.id.img1);
vh.img2 = (ImageView) view.findViewById(R.id.img2);
break;
}
view.setTag(vh);
} else {
vh = (ViewHolder) view.getTag();
}
switch (type) {
case 0:
vh.name.setText(list2.get(i).name);
ImageLoader.getInstance().displayImage(list2.get(i).imgg, vh.img);//记得把imageloder写上
break;
case 1:
ImageLoader.getInstance().displayImage(list2.get(i).imgg, vh.img);
ImageLoader.getInstance().displayImage(list2.get(i).imgg, vh.img2);
break;
}
return view;
}
class ViewHolder {
TextView name;
ImageView img, img2;
}
@Override
public int
getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
//分开多条目显示
if
(position % 2 == 0) {
return 0;
} else {
return 1;
}
}
}
@Override
public void onRefresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
list2.add(0,new User2("怪物恋人","http://f2.kkmh.com/image/170915/a56o0vmx8.webp-w640"));
}
},3000);
handler.sendEmptyMessageDelayed(0,3000); }
@Override
public void onLoadMore() {
handler
.postDelayed(new Runnable() {
@Override
public void run() {
list2.add(new User2("怪物恋人","http://f2.kkmh.com/image/170915/a56o0vmx8.webp-w640"));
handler.sendEmptyMessage(0);
}
},3000);
}
}


Bean类




       public class User {

    public String url;

    public User(String url) {
        this.url = url;
    }

    @Override
    public String toString() {
        return "User{" +
                "url='" + url + '\'' +
                '}';
    }

}




public class User2 {

    public String name;
    public String imgg;

    public User2(String name, String imgg) {
        this.name = name;
        this.imgg = imgg;
    }

    @Override
    public String toString() {
        return "User2{" +
                "name='" + name + '\'' +
                ", imgg='" + imgg + '\'' +
                '}';
    }

}



import android.app.Application;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;


/**
 * Created by HQ on 2017/9/17.
 */

public class Appi extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(build);
    }
}





import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by HQ on 2017/9/17.
 */

public class fragment2 extends Fragment {

    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment2, container, false);

        return view;
          }
        }
 
 
 

 
 








 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值