上拉加载,下拉刷新列表数据,多条目列表,默认图圆角

MainActivity

  package yxr.com.wekk2_lixie;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.RadioGroup;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class MainActivity extends FragmentActivity {
        private ViewPager viewPager;
        private RadioGroup group;
        private List<Fragment> list=new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            viewPager=findViewById(R.id.viewpager);
            group=findViewById(R.id.group);
            list.add(new Fragment1());
            list.add(new Fragment2());
            list.add(new Fragment3());
    
            viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
                @Override
                public Fragment getItem(int i) {
                    return list.get(i);
                }
    
                @Override
                public int getCount() {
                    return list.size();
                }
            });
            group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId){
                        case R.id.but1:
                            viewPager.setCurrentItem(0);
                            break;
                        case R.id.but2:
                            viewPager.setCurrentItem(1);
                            break;
                        case R.id.but3:
                            viewPager.setCurrentItem(2);
                            break;
                    }
                }
            });
        }
    }

//实现上拉加载,下拉刷新列表

public class Fragment1 extends Fragment {
    String url = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
    int page;
    private XListView xListView;
    List<Goods.DataBean> list = new ArrayList<>();
    private MAdapter mAdapter;
    private ImageLoader imageLoaderInstances;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment1, container, false);
        xListView = view.findViewById(R.id.xlv);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 开启上拉加载更多功能;
        xListView.setPullLoadEnable(true);
        imageLoaderInstances = ImageLoader.getInstance();
        //配置适配器
        mAdapter = new MAdapter();
        xListView.setAdapter(mAdapter);
        //请求网络数据
        initData(page);

        //实现监听设置上下拉的逻辑
        xListView.setXListViewListener(new XListView.IXListViewListener() {
            //下拉刷新
            @Override
            public void onRefresh() {
                page = 0;
                list.clear();
                initData(page);
            }

            //上拉加载更多;
            @Override
            public void onLoadMore() {
                page++;
           
                initData(page);
            }
        });

    }

    //请求网络方法
    private void initData(int page) {
        String mUrl = url + page;
        new MAsycnTask().execute(mUrl);
    }

    //配置适配器
    private class MAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHodler hodler=new ViewHodler();
            if (convertView==null){
                if (convertView==null){
                    convertView=View.inflate(getActivity(),R.layout.lv_item,null);
                    hodler.imageView=convertView.findViewById(R.id.imageView);
                    hodler.textView=convertView.findViewById(R.id.textView);
                    convertView.setTag(hodler);
                }else {
                    hodler= (ViewHodler) convertView.getTag();
                }
               hodler.textView.setText(list.get(position).getNews_title());
                  DisplayImageOptions options = new DisplayImageOptions.Builder()
                    //显示效果:圆形   
                        .displayer(new CircleBitmapDisplayer())
                        //圆角
                        .displayer(new RoundedBitmapDisplayer(50))
                        .build();
     //加载图片           imageLoaderInstances.displayImage(list.get(position).getPic_url(),hodler.imageView,options);
            }


            return  convertView;


        }
    }
    class  ViewHodler{
        ImageView imageView;
        TextView textView;
    }


    class MAsycnTask 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 gson = new Gson();
            Goods goods = gson.fromJson(s, Goods.class);
            List<Goods.DataBean> data = goods.getData();

            list.addAll(data);
            mAdapter.notifyDataSetChanged();
            xListView.setRefreshTime("刚刚刷新");
            xListView.stopRefresh();//隐藏头
            xListView.stopLoadMore();//隐藏脚


        }
    }

}

//实现多条目

public class Fragment2 extends Fragment {
    private ListView listView;
    String url = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
    List<Goods.DataBean> list = new ArrayList<>();
    private MAdapter1 mAdapter;
    private ImageLoader imageLoaderInstances;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment2, container, false);
        listView = view.findViewById(R.id.listview);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        imageLoaderInstances = ImageLoader.getInstance();
        //配置适配器
        mAdapter = new MAdapter1();
        listView.setAdapter(mAdapter);
        new MAsycnTask1().execute(url);

    }
    class MAsycnTask1 extends AsyncTask<String, Void, String> {
        
        @Override
        protected String doInBackground(String... strings) {
            return NetWordUtils.getNetjson(strings[0]);
        }

        @Override
        protected void onPostExecute(String s) {

            //解析gson
            Gson gson = new Gson();
            Goods goods = gson.fromJson(s, Goods.class);
            List<Goods.DataBean> data = goods.getData();
            list.addAll(data);
            mAdapter.notifyDataSetChanged();

        }
    }

    //配置适配器
    class MAdapter1 extends BaseAdapter {


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

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }


        @Override
        public int getViewTypeCount() {
            return 2;
        }
        @Override
        public int getItemViewType(int position) {
            return position % 2;

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            int itemViewType = getItemViewType(position);//得到条目类型标识
            switch (itemViewType ){
                case 0:
                    ViewHolder0 holder0=new ViewHolder0();
                    if (convertView==null){
                        convertView=View.inflate(getActivity(),R.layout.list_main1,null);
                        holder0.image1=convertView.findViewById(R.id.image1);
                        holder0.text1=convertView.findViewById(R.id.text1);
                        convertView.setTag(holder0);
                    }else {
                        holder0= (ViewHolder0) convertView.getTag();
                    }
                    holder0.text1.setText(list.get(position).getNews_title());
                     DisplayImageOptions options = new DisplayImageOptions.Builder()
                     //显示效果:圆形
                            .displayer(new CircleBitmapDisplayer())
                            //圆角
                            .displayer(new RoundedBitmapDisplayer(50))
                            .build();
				//加载图片                imageLoaderInstances.displayImage(list.get(position).getPic_url(),holder0.imptions);
                    break;
                case 1:
                    ViewHolder1 holder1=new ViewHolder1();
                    if (convertView==null){
                        convertView=View.inflate(getActivity(),R.layout.list_main2,null);

                        holder1.text2=convertView.findViewById(R.id.text2);
                        convertView.setTag(holder1);
                    }else {
                        holder1= (ViewHolder1) convertView.getTag();
                    }
                    holder1.text2.setText(list.get(position).getNews_title());

                    break;
            }
            return convertView;
        }
    }



    class ViewHolder0{
        ImageView image1;
        TextView text1;
    }
    class ViewHolder1{

        TextView text2;
    }



}

//javabean

public class Goods {

    private int status;
    private String info;
    private List<DataBean> data;

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        /**
         * news_id : 13783
         * news_title : 广州国际瓦业展新闻发布会于羊城吹响号角
         * news_summary : 近日,2016第六届中国(广州)国际瓦业交易会暨制瓦工业展•亚太建筑科技论坛新闻发布会新闻发布会在广州军区珠江宾馆隆重召
         * pic_url : http://f.expoon.com/sub/news/2016/01/18/341493_230x162_0.jpg
         */

        private String news_id;
        private String news_title;
        private String news_summary;
        private String pic_url;

        public String getNews_id() {
            return news_id;
        }

        public void setNews_id(String news_id) {
            this.news_id = news_id;
        }

        public String getNews_title() {
            return news_title;
        }

        public void setNews_title(String news_title) {
            this.news_title = news_title;
        }

        public String getNews_summary() {
            return news_summary;
        }

        public void setNews_summary(String news_summary) {
            this.news_summary = news_summary;
        }

        public String getPic_url() {
            return pic_url;
        }

        public void setPic_url(String pic_url) {
            this.pic_url = pic_url;
        }
    }
}

App

public class MApp extends Application{

    @Override
    public void onCreate() {
        super.onCreate();
	//初始化
        ImageLoaderConfiguration configuration = null;
        DisplayImageOptions options = new DisplayImageOptions.Builder().build();
        configuration = new ImageLoaderConfiguration.Builder(this)
                //配置:内存 磁盘 缓存
                //.memoryCache(new LruMemoryCache())
                //.memoryCacheSize()
                //内存缓存大小
                .memoryCacheSizePercentage(10)
                //配置磁盘缓存:目录 文件名生成  大小
                //.diskCache(new LruDiskCache(getCacheDir(), new HashCodeFileNameGenerator(), 10*1024*1024))
                .diskCacheSize(50*1024*1024)
                //线程配置
                //任务优先级配置
                //FIFO
                //加载图片1   2  3  4  5
                //LIFO
                //.tasksProcessingOrder(QueueProcessingType.)
                //默认显示配置
                //.defaultDisplayImageOptions(options)
                .build();
        ImageLoader.getInstance().init(configuration);

    }
}

//布局

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6"
        />
    <RadioGroup
        android:id="@+id/group"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/but1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:checked="true"
            android:background="@drawable/selector"
            android:text="首页"/>
        <RadioButton
            android:id="@+id/but2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:background="@drawable/selector"
            android:text="朋友"/>
        <RadioButton
            android:id="@+id/but3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:background="@drawable/selector"
            android:text="我的"/>
    </RadioGroup>

</LinearLayout>

//布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0"
    android:orientation="horizontal"
    >
    <com.bwie.xlistviewlibrary.view.XListView
        android:id="@+id/xlv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >


    </com.bwie.xlistviewlibrary.view.XListView>
</RelativeLayout>

//布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0f0">
<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

//布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/image1"
        android:layout_width="80dp"
        android:layout_height="80dp" />
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_height="wrap_content" />

</LinearLayout>

//布局

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

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

</LinearLayout>

//布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:padding="10dp"
       />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
      />
</LinearLayout>

//清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="yxr.com.wekk2_lixie">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

    <application
        android:name=".MApp"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值