新手XlistView+LoadImager+多条目加载 ————

个人资源文件,新手XlistView+LoadImager+多条目加载

链接:https://blog.csdn.net/L_1145418863/article/details/83032279

这个仅仅用于学习,个人笔记

个人的代码片.

布局

<?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"
    tools:context=".ui.MainActivity"
    android:orientation="vertical">

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

    </android.support.v4.view.ViewPager>
    <RadioGroup
        android:id="@+id/rgroup"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rb1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:checked="true"
            android:background="@drawable/bac_change"
            android:text="主页"/>
        <RadioButton
            android:id="@+id/rb2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:background="@drawable/bac_change"
            android:text="次页"/>
        <RadioButton
            android:id="@+id/rb3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center"
            android:background="@drawable/bac_change"
            android:text="末页"/>
    </RadioGroup>

</LinearLayout>

fragment–只写了一个


<?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="#9F12">

    <me.maxwin.view.XListView
        android:id="@+id/xlv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </me.maxwin.view.XListView>

</LinearLayout>

条目没写

主页面

package com.bwei.liuzhenguo.ui;

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.view.View;
import android.widget.RadioGroup;

import com.bwei.liuzhenguo.R;
import com.bwei.liuzhenguo.feagment.Fragment1;
import com.bwei.liuzhenguo.feagment.Fragment2;
import com.bwei.liuzhenguo.feagment.Fragment3;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity {
    ViewPager vp;
    ArrayList<Fragment> list;
    RadioGroup group;

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

        group = findViewById(R.id.rgroup);
        vp = findViewById(R.id.vp);
        list = new ArrayList<>();
        list.add(new Fragment1());
        list.add(new Fragment2());
        list.add(new Fragment3());

        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return list.get(i);
            }

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

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        group.check(R.id.rb1);
                        break;
                    case 1:
                        group.check(R.id.rb2);
                        break;
                    case 2:
                        group.check(R.id.rb3);
                        break;

                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb1:
                        vp.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        vp.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        vp.setCurrentItem(2);
                        break;
                }
            }
        });


    }
}

第一个fragment

package com.bwei.liuzhenguo.feagment;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.bwei.liuzhenguo.R;
import com.bwei.liuzhenguo.adapter.BaseAdapter_My;
import com.bwei.liuzhenguo.bean.NewItems;
import com.bwei.liuzhenguo.bean.NewResu;
import com.google.gson.Gson;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtilsHC4;

import java.io.IOException;
import java.util.ArrayList;

import me.maxwin.view.XListView;

public class Fragment1 extends Fragment {
    XListView xlv;
    BaseAdapter_My md;
    ArrayList<NewItems> list;
    int load = 1;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = View.inflate(getActivity(), R.layout.fragment1, null);
        xlv = view.findViewById(R.id.xlv);

        md = new BaseAdapter_My(getActivity());
        list = new ArrayList<>();

        xlv.setAdapter(md);

        if(!JudgeInternt()){
            Toast.makeText(getActivity(),"没有网络",Toast.LENGTH_SHORT).show();
        }else{
            HttpLoadData();
        }

        xlv.setPullLoadEnable(true);

        xlv.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                load = 1 ;
                HttpLoadData();
            }

            @Override
            public void onLoadMore() {
                load = 0;
                HttpLoadData();
            }
        });

        return view;
    }

    private void HttpLoadData() {
        AsyncTask<String,String,ArrayList<NewItems>> asyncTask = new AsyncTask<String, String, ArrayList<NewItems>>() {
            @Override
            protected ArrayList<NewItems> doInBackground(String... strings) {
                ArrayList<NewItems> list = new ArrayList<>();
                CloseableHttpClient client = HttpClients.createDefault();

                for (String uri : strings){
                    HttpUriRequest build = RequestBuilder.get().setUri(uri).build();

                    try {
                        CloseableHttpResponse execute = client.execute(build);
                        Gson gson = new Gson();
                        int statusCode = execute.getStatusLine().getStatusCode();

                        if(statusCode == 200){
                            String s = EntityUtilsHC4.toString(execute.getEntity());
                            NewResu newResu = gson.fromJson(s, NewResu.class);
                            list.addAll(newResu.result.data);
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return list;
            }

            @Override
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);
            }

            @Override
            protected void onPostExecute(ArrayList<NewItems> newItems) {
                md.SetListData(newItems,load);
                xlv.stopLoadMore();
                xlv.stopRefresh();
            }
        };
        asyncTask.execute(
                "http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri=gn",
                "http://result.eolinker.com/k2BaduF2a6caa275f395919a66ab1dfe4b584cc60685573?uri=tt"
        );

    }

    public boolean JudgeInternt(){
        ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        return info.isAvailable()&&info!=null;
    }

}

bean类并没有添加
↓ adapter

package com.bwei.liuzhenguo.adapter;

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.bwei.liuzhenguo.R;
import com.bwei.liuzhenguo.bean.NewItems;
import com.bwei.liuzhenguo.util.ImageLoader_;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

public class BaseAdapter_My extends BaseAdapter {

    Context context;
    ArrayList<NewItems> list;
    public BaseAdapter_My(Context con){
        this.context=con;
        this.list = new ArrayList<>();
    }

    public void SetListData(ArrayList<NewItems> items , int load){
        if(load == 1){
            this.list.clear();
        }
        this.list.addAll(items);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        NewItems items = list.get(position);

        if(items.IsGuoNei()){
            return super.getItemViewType(position);
        }
        if(items.IsTouTiao()){
            return 1;
        }

        return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount()+1;
    }

    @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) {
        MyHolper mh = null;
        if(convertView == null){
            if(list.get(position).IsGuoNei()){
                convertView = View.inflate(context,R.layout.item1,null);
            }else{
                convertView = View.inflate(context,R.layout.item2,null);
            }
            mh = new MyHolper(convertView,list.get(position));
        }else{
            mh = (MyHolper) convertView.getTag();
        }

        mh.Setdata(list.get(position));

        return convertView;
    }

    class MyHolper{
        ImageView img;
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
        View item;

        public MyHolper(View item,NewItems items){
            this.item = item;
            if(items.IsGuoNei()){
                img = item.findViewById(R.id.i1_image);
                tv1 = item.findViewById(R.id.i1_tv1);
                tv2 = item.findViewById(R.id.i1_tv2);
            }
            if(items.IsTouTiao()){
                tv3 = item.findViewById(R.id.i2_tv1);
                tv4 = item.findViewById(R.id.i2_tv2);
            }
            this.item.setTag(this);
        }

        public void Setdata(NewItems items){
            if(items.IsGuoNei()){
                tv1.setText(items.title);
                tv2.setText(items.url);
                /*------------加载图片----------------*/
                ImageLoader.getInstance().displayImage(items.thumbnail_pic_s,img,
                        ImageLoader_.getDisplayImageOptions(img.getContext()));
            }
            if(items.IsTouTiao()){
                tv3.setText(items.title);
                tv4.setText(items.url);
            }
        }

    }

}

工具类中的LoaderImager

package com.bwei.liuzhenguo.adapter;

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.bwei.liuzhenguo.R;
import com.bwei.liuzhenguo.bean.NewItems;
import com.bwei.liuzhenguo.util.ImageLoader_;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

public class BaseAdapter_My extends BaseAdapter {

    Context context;
    ArrayList<NewItems> list;
    public BaseAdapter_My(Context con){
        this.context=con;
        this.list = new ArrayList<>();
    }

    public void SetListData(ArrayList<NewItems> items , int load){
        if(load == 1){
            this.list.clear();
        }
        this.list.addAll(items);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        NewItems items = list.get(position);

        if(items.IsGuoNei()){
            return super.getItemViewType(position);
        }
        if(items.IsTouTiao()){
            return 1;
        }

        return super.getItemViewType(position);
    }

    @Override
    public int getViewTypeCount() {
        return super.getViewTypeCount()+1;
    }

    @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) {
        MyHolper mh = null;
        if(convertView == null){
            if(list.get(position).IsGuoNei()){
                convertView = View.inflate(context,R.layout.item1,null);
            }else{
                convertView = View.inflate(context,R.layout.item2,null);
            }
            mh = new MyHolper(convertView,list.get(position));
        }else{
            mh = (MyHolper) convertView.getTag();
        }

        mh.Setdata(list.get(position));

        return convertView;
    }

    class MyHolper{
        ImageView img;
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView tv4;
        View item;

        public MyHolper(View item,NewItems items){
            this.item = item;
            if(items.IsGuoNei()){
                img = item.findViewById(R.id.i1_image);
                tv1 = item.findViewById(R.id.i1_tv1);
                tv2 = item.findViewById(R.id.i1_tv2);
            }
            if(items.IsTouTiao()){
                tv3 = item.findViewById(R.id.i2_tv1);
                tv4 = item.findViewById(R.id.i2_tv2);
            }
            this.item.setTag(this);
        }

        public void Setdata(NewItems items){
            if(items.IsGuoNei()){
                tv1.setText(items.title);
                tv2.setText(items.url);
                /*------------加载图片----------------*/
                ImageLoader.getInstance().displayImage(items.thumbnail_pic_s,img,
                        ImageLoader_.getDisplayImageOptions(img.getContext()));
            }
            if(items.IsTouTiao()){
                tv3.setText(items.title);
                tv4.setText(items.url);
            }
        }

    }

}

用于初始化的LoadImager
App

package com.bwei.liuzhenguo;

import android.app.Application;

import com.bwei.liuzhenguo.util.ImageLoader_;
import com.nostra13.universalimageloader.core.ImageLoader;

public class App_ extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        ImageLoader.getInstance().init(ImageLoader_.getImageLoaderConfiguration(this));

    }
}
-----build.gradle--------  因为使用了AsyncTask
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28

    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.example.listview_items"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    configurations {
        all*.exclude group: 'com.google.code.gson'
    }
    implementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
    implementation 'com.google.code.gson:gson:2.8.1'
    implementation project(':xlistview')
}

《此内容只是个人所编写,不接受任何反驳,可以提意见 仅仅用于学习》

发布了15 篇原创文章 · 获赞 8 · 访问量 3306
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览