1. 程式人生 > >Fragment 側拉 多條目載入

Fragment 側拉 多條目載入

MainActivity

package com.example.week02lxz;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private DrawerLayout drawer;
private FrameLayout fram_layout;
private RadioGroup radio_group;
private FragmentManager manager;
private FragmentTransaction transaction;
private ListView list_view;
ArrayList<String> alist = new ArrayList<String>();
private RadioButton btn_1;
private RadioButton btn_2;
private RadioButton btn_3;
private ImageView img1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    drawer = findViewById(R.id.drawer);
    fram_layout = findViewById(R.id.fram_layout);
    radio_group = findViewById(R.id.radio_group);
    list_view = findViewById(R.id.list_view);
    btn_1 = findViewById(R.id.btn_1);
    btn_2 = findViewById(R.id.btn_2);
    btn_3 = findViewById(R.id.btn_3);
    img1 = findViewById(R.id.img1);

    DisplayImageOptions options=ImageloaderUtils.getdisplay();
    ImageLoader instance = ImageLoader.getInstance();
    instance.displayImage("drawable://"+R.drawable.o,img1,options);
    initData();
    manager = getSupportFragmentManager();
    transaction = manager.beginTransaction();
    transaction.replace(R.id.fram_layout, new Fragment01()).commit();
    radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            FragmentTransaction transaction1 = manager.beginTransaction();
            switch (checkedId) {
                case R.id.btn_1:
                    transaction1.replace(R.id.fram_layout, new Fragment01());
                    break;
                case R.id.btn_2:
                    transaction1.replace(R.id.fram_layout, new Fragment02());
                    break;
                case R.id.btn_3:
                    transaction1.replace(R.id.fram_layout, new Fragment03());
                    break;
            }
            transaction1.commit();
        }
    });
    list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            FragmentTransaction transaction1 = manager.beginTransaction();
            switch (position) {
                case 0:
                    transaction1.replace(R.id.fram_layout, new Fragment01());
                    drawer.closeDrawers();
                    btn_1.setChecked(true);
                    break;
                case 1:
                    transaction1.replace(R.id.fram_layout, new Fragment02());
                    drawer.closeDrawers();
                    btn_2.setChecked(true);
                    break;
                case 2:
                    transaction1.replace(R.id.fram_layout, new Fragment03());
                    drawer.closeDrawers();
                    btn_3.setChecked(true);
                    break;
            }
            transaction1.commit();
        }
    });
}
private void initData() {
    for (int i = 1; i < 10; i++) {
        alist.add("fragment0" + i);
    }
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, alist);
    list_view.setAdapter(arrayAdapter);
}

}

Fragment01

package com.example.week02lxz;

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.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bwie.xlistviewlibrary.utils.NetWordUtils;
import com.bwie.xlistviewlibrary.view.XListView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

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

public class Fragment01 extends Fragment {
String urlString = “http://api.expoon.com/AppNews/getNewsList/type/1/p/”;
ArrayList<Goods.DataBean> alist=new ArrayList<Goods.DataBean>();
private XListView xls;
private MyBadapter badapter;
private ImageLoader instance;
int page;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment01, container, false);
xls = view.findViewById(R.id.xls);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
badapter = new MyBadapter();
instance = ImageLoader.getInstance();
xls.setAdapter(badapter);
erinitData(page);
xls.setPullLoadEnable(true);
xls.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
alist.clear();
erinitData(0);
}

        @Override
        public void onLoadMore() {
            page++;
            erinitData(page);
        }
    });
}

private void erinitData(int page) {
    String murl=urlString+page;
    new MAsyncTask().execute(murl);
}


private class MyBadapter extends BaseAdapter {
    ImageView imageView;
    TextView textView;
    View view1;

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

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

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

    @Override
    public Object getItem(int position) {
        return alist.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        int itemViewType = getItemViewType(position);
        switch (itemViewType){
            case 0:
                view1= View.inflate(getActivity(), R.layout.item2, null);
                imageView=view1.findViewById(R.id.img2);
                textView=view1.findViewById(R.id.text2);
                textView.setText(alist.get(position).getNews_title());
                DisplayImageOptions options=ImageloaderUtils.getdisplay();
                instance.displayImage(alist.get(position).getPic_url(),imageView,options);
                break;
            case 1:
                view1= View.inflate(getActivity(), R.layout.item2, null);
                imageView=view1.findViewById(R.id.img2);
                textView=view1.findViewById(R.id.text2);
                textView.setText(alist.get(position).getNews_title());
                imageView.setVisibility(View.GONE);
                break;
        }
        return view1;
    }

}

private class MAsyncTask 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 = new Gson();
        Goods goods = gson.fromJson(s, Goods.class);
        List<Goods.DataBean> data = goods.getData();
        alist.addAll(data);
        badapter.notifyDataSetChanged();
        UIcomplete();
    }
}
public void UIcomplete(){
    xls.setRefreshTime("剛剛");
    xls.stopLoadMore();
    xls.stopRefresh();
}

}

MApp

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

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:orientation=“vertical”
tools:context=".MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fram_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"></FrameLayout>

    <RadioGroup
        android:id="@+id/radio_group"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/btn_1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/anya"
            android:button="@null"
            android:checked="true"
            android:gravity="center"
            android:text="微信" />

        <RadioButton
            android:id="@+id/btn_2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/anya"
            android:button="@null"
            android:gravity="center"
            android:text="通訊錄" />

        <RadioButton
            android:id="@+id/btn_3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/anya"
            android:button="@null"
            android:gravity="center"
            android:text="發現" />
    </RadioGroup>
</LinearLayout>

<LinearLayout
    android:id="@+id/linear"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:layout_gravity="start">

    <ImageView
        android:id="@+id/img1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/o"/>

    <ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

</LinearLayout>

</android.support.v4.widget.DrawerLayout>

fragment01.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
xmlns:android=“http://schemas.android.com/apk/res/android” android:layout_width=“match_parent”
android:layout_height=“match_parent”>

<com.bwie.xlistviewlibrary.view.XListView
    android:id="@+id/xls"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</com.bwie.xlistviewlibrary.view.XListView>

</android.support.constraint.ConstraintLayout>