1. 程式人生 > >新手XlistView+LoadImager+多條目載入 ————

新手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
            
           

相關推薦

新手XlistView+LoadImager+條目載入 ————

個人資原始檔,新手XlistView+LoadImager+多條目載入 連結:https://blog.csdn.net/L_1145418863/article/details/83032279 這個僅僅用於學習,個人筆記 個人的程式碼片. 佈局 <?xml ve

XListView實現向上重新整理,向下追加資料,並且條目載入

開始佈局 此處只展示Xlistview的佈局 <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schema

XListView解析資料及PullToRefresh解析資料 及條目載入

package com.example.week3.Fragment; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import and

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

activity_main <com.bwie.xlistview.XListView android:id="@+id/lv_news" android:layout_width="match_parent" android:la

TabLayout、Xlistview根據圖片數量條目載入

package liyuanqi.bwie.com.zhoukao3lx; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; imp

XListView設定上拉重新整理下拉載入條目載入

首先匯入xlistviewlibrary在佈局中寫 com.bwie.xlistviewlibrary.view.XListView 可以用ctrl看看是否匯入成功 xlistView.setPullLoadEnable(true); 可以上拉重新整理更多 init

AndroidStudio xlistview 條目載入

思路: 1 BaseAdapter 介面卡 2 多個條目佈局檔案 3 介面卡方法 在這裡插入程式碼片 ```public class MyAdapter extends BaseAdapter { private Context context; private List

實現條目載入

public class MyAdapter extends BaseAdapter { private Context context; private List<User.ResultBean.DataBean> list; public MyAdapter(Conte

TabLayout+ViewPager+Fragment實現條目載入個介面)

要求: 實現如圖所示效果,上方TabLayout,下面ViewPager+Fragment,下拉重新整理,上拉載入更多,多條目載入,顯示title標題,根據images圖片數量進行多條目載入。 介面如下,其中page為頁數,lid為上方標題的id https://feed.mix.s

Fragment 側拉 條目載入

MainActivity package com.example.week02lxz; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction;

ViewPager 側拉 條目載入

MainActivity裡 package com.example.week02; import android.os.Environment; import android.support.annotation.NonNull; import android.support.v4.

ImageLoader,條目載入

工具類 package utils; import android.os.AsyncTask; import android.util.Log; import java.io.IOException; import java.io.InputStream; import java.i

條目載入

需要重寫getViewTypeCount,getItemViewType兩個方法 private void initData() { //這個過程是在子線中,請求網路比較慢,有可能8秒; new MAsyncTask().execute(url);

基類抽取http服務(網路判斷)資料庫條目載入上拉載入下拉重新整理

//BaseActivity public abstract class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) {

TabLayout+ViewPager+Fragment實現條目載入

要求: 實現如圖所示效果,上方TabLayout,下面ViewPager+Fragment,下拉重新整理,上拉載入更多,多條目載入,顯示title標題,根據images圖片數量進行多條目載入。 介面如下,其中page為頁數,lid為上方標題的id https://fee

Fragment內獲取網路資料XListView重新整理 + 條目,判斷網路+Imageloader+佈局

Fragment內獲取網路資料XListView重新整理,判斷網路+獲取圖片 public class ElankFragment extends Fragment { String path = "http://www.xieast.com/api/

條目載入 +上拉重新整理,下拉載入

//*Afragment->XListView 重新整理 載入 public class AFragment extends Fragment implements XListView.IXListViewListener { private String

ViewPager 側拉 條目載入

MainActivity裡 package com.example.week02; import android.os.Environment; import android.support.annotation.NonNull; import android.

RecyclerView條目載入(網路請求資料)

RecyclerView多條目載入和ListView的多條目載入用法是一樣的,程式碼如下: 1.匯入RecyclerView或XRecyclerView的依賴包(自己按照需求匯入依賴包,下面RecyclerView可以替換成XRecyclerView)XRecyclerVi

Recyclerview 實現條目載入

這種佈局樣式,相信很多開發者都會用到 今天我是用RecyclerView來實現這個多種Item的載入. 其實最關鍵的是要複寫RecyclerView的Adapter中的getItemViewType()方法 這個方法就根據條件返回條目的型別 一般有