1. 程式人生 > >XListView多條目載入沒有上拉,下拉

XListView多條目載入沒有上拉,下拉

activity_main

<com.bwie.xlistview.XListView
    android:id="@+id/lv_news"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp">
</com.bwie.xlistview.XListView>

第一條目佈局item_news1

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/img_logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

第二條目佈局item_news2

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/img_logo1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <ImageView
            android:id="@+id/img_logo2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"/>
    </LinearLayout>
</LinearLayout>

第三條目佈局item_news3

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
       <ImageView
           android:id="@+id/img_logo1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />
       <ImageView
           android:id="@+id/img_logo2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dp"/>
       <ImageView
           android:id="@+id/img_logo3"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dp"/>
   </LinearLayout>
</LinearLayout>

MainActivity

package wanghuiqi.bawie.com.xlistview_1011;

import android.annotation.SuppressLint; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle;

import com.bwie.xlistview.XListView; import com.google.gson.Gson;

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

public class MainActivity extends AppCompatActivity {

    private List<News.DataBean> list;

    @SuppressLint("StaticFieldLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找控制元件
        XListView lvnews=findViewById(R.id.lv_news);
        list = new ArrayList<>();
        //介面卡
        final NewsAdapter newsAdapter=new NewsAdapter(this, list);
        lvnews.setAdapter(newsAdapter);

        new AsyncTask<String,Integer,String>(){

            @Override
            protected String doInBackground(String... strings) {

                return HttpUtils.getStringFromHttp(strings[0]);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson = new Gson();
                News news = gson.fromJson(s, News.class);
                list.clear();
                list.addAll(news.getData());
                newsAdapter.notifyDataSetChanged();
            }
        }.execute("http://www.xieast.com/api/news/news.php?page=1");
    }
}

HttpUtils

package wanghuiqi.bawie.com.xlistview_1011;

import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection;

class HttpUtils {
    private static String result="";
    public static String getStringFromHttp(String urlString) {
        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setUseCaches(false);
            connection.setConnectTimeout(5000);
            if (connection.getResponseCode()==200){
                InputStream is = connection.getInputStream();
                result=getStringFromInputStream(is);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    private static String getStringFromInputStream(InputStream is) throws IOException {
        ByteArrayOutputStream boas = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len=-1;
        while ((len=is.read(buffer,0,buffer.length))!=-1){
            boas.write(buffer,0,len);
            boas.flush();
        }
        result=boas.toString();
        boas.close();
        is.close();

        return result;
    }
}

MyAppImg

package wanghuiqi.bawie.com.xlistview_1011;

import android.app.Application;

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

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

NewsAdapter

package wanghuiqi.bawie.com.xlistview_1011;

import android.content.Context; import android.text.TextUtils; 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.List;

public class NewsAdapter extends BaseAdapter { private Context context; private List<News.DataBean>list; private static final int TYPE_ONE_PIC=0; private static final int TYPE_TWO_PIC=1; private static final int TYPE_THREE_PIC=2;

public NewsAdapter(Context context, List<News.DataBean> list) {
    this.context = context;
    this.list = list;
}


@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 int getViewTypeCount() {
    return 3;
}

@Override
public int getItemViewType(int position) {
    News.DataBean bean = list.get(position);
    if (!TextUtils.isEmpty(bean.getThumbnail_pic_s())){
        return 2;
    }else if(!TextUtils.isEmpty(bean.getThumbnail_pic_s02())){
        return 1;
    }else{
        return 0;
    }
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   Viewholder1 holder1;
   Viewholder2 holder2;
   Viewholder3 holder3;
    int type = getItemViewType(position);
    switch (type){
        case 0:
            if (convertView==null){
                convertView=View.inflate(context,R.layout.item_news1,null);
                holder1=new Viewholder1();
                holder1.textLogin=convertView.findViewById(R.id.text_login);
                holder1.imgLogo=convertView.findViewById(R.id.img_logo);
                convertView.setTag(holder1);
            }else{
                holder1= (Viewholder1) convertView.getTag();
            }
            holder1.textLogin.setText(list.get(position).getTitle());
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder1.imgLogo);
            break;
        case 1:

            if (convertView==null){
                holder2=new Viewholder2();
                convertView=View.inflate(context,R.layout.item_news2,null);
                holder2.textLogin=convertView.findViewById(R.id.text_login);
                holder2.imgLogo1=convertView.findViewById(R.id.img_logo1);
                holder2.imgLogo2=convertView.findViewById(R.id.img_logo2);
                convertView.setTag(holder2);
            }else{
                holder2= (Viewholder2) convertView.getTag();
            }
            holder2.textLogin.setText(list.get(position).getTitle());
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder2.imgLogo1);
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),holder2.imgLogo2);
            break;
        case 2:
            if (convertView==null){
                holder3=new Viewholder3();
                convertView=View.inflate(context,R.layout.item_news3,null);
                holder3.textLogin=convertView.findViewById(R.id.text_login);
                holder3.imgLogo1=convertView.findViewById(R.id.img_logo1);
                holder3.imgLogo2=convertView.findViewById(R.id.img_logo2);
                holder3.imgLogo3=convertView.findViewById(R.id.img_logo3);
                convertView.setTag(holder3);
            }else{
                holder3= (Viewholder3) convertView.getTag();
            }
            holder3.textLogin.setText(list.get(position).getTitle());
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),holder3.imgLogo1);
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s02(),holder3.imgLogo2);
            ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s03(),holder3.imgLogo3);
            break;
    }
    return convertView;
}
class Viewholder1{
    TextView textLogin;
    ImageView imgLogo;
}
class Viewholder2{
    TextView textLogin;
    ImageView imgLogo1;
    ImageView imgLogo2;
}
class Viewholder3{
    TextView textLogin;
    ImageView imgLogo1;
    ImageView imgLogo2;
    ImageView imgLogo3;
}

}

沒有News