1. 程式人生 > >XListView上拉下拉載入 DrawerLayout側拉

XListView上拉下拉載入 DrawerLayout側拉

1.佈局

<?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:layout_width=“match_parent”
android:layout_height=“match_parent”
android:id="@+id/drawer"
tools:context=".MainActivity">
<com.bwie.xlistviewlibrary.view.XListView
android:id="@+id/xlv"
android:layout_width=“match_parent”
android:layout_height=“match_parent”
>
</com.bwie.xlistviewlibrary.view.XListView>

<!--<FrameLayout-->
    <!--android:id="@+id/frame"-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"></FrameLayout>-->

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

2.MainActivity

public class MainActivity extends AppCompatActivity {

String baseUrl = "http://api.expoon.com/AppNews/getNewsList/type/1/p/";
int page;
private XListView xListView;
List<Goods.DataBean> list = new ArrayList<>(); //大集合
List<String> slist = new ArrayList<>(); //大集合

private MAdapter mAdapter;
private ImageLoader imageLoaderInstances;
private ListView list_view;
private DrawerLayout drawer;


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

    xListView = (XListView) findViewById(R.id.xlv);
    list_view = findViewById(R.id.list_view);
    drawer = findViewById(R.id.drawer);
    imageLoaderInstances = ImageLoader.getInstance();
    mAdapter = new MAdapter();
    xListView.setAdapter(mAdapter);
    initData(page);
    xListView.setPullLoadEnable(true);
    /*
    上下拉監聽
     */
    xListView.setXListViewListener(new XListView.IXListViewListener() {
        @Override
        public void onRefresh() {
        list.clear();
        initData(0);
        }

        @Override
        public void onLoadMore() {
            page++;
            initData(page);
        }
    });
    shuju();
    drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(@NonNull View view, float v) {

        }

        @Override
        public void onDrawerOpened(@NonNull View view) {
            Toast.makeText(MainActivity.this,"打開了側拉",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onDrawerClosed(@NonNull View view) {

        }

        @Override
        public void onDrawerStateChanged(int i) {

        }
    });
    list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(MainActivity.this,slist.get(position).toString(),Toast.LENGTH_SHORT).show();

// drawer.closeDrawers();
}
});
}

private void shuju() {
    for (int i = 0; i < 20; i++) {
        slist.add("資料"+i);
    }
    ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,slist);
    list_view.setAdapter(arrayAdapter);
}

private void initData(int page) {
    String mUrl = baseUrl + page;
    new MAsycnTask().execute(mUrl);
}

/*
下拉重新整理
 *//*
@Override
public void onRefresh() {

}
*//*
下拉載入更多資料
 *//*
@Override
public void onLoadMore() {

}*/

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 i, View view, ViewGroup viewGroup) {
        View viewItem = View.inflate(MainActivity.this, R.layout.lv_item, null);
        TextView textView = (TextView) viewItem.findViewById(R.id.textView);
        ImageView imageView = (ImageView) viewItem.findViewById(R.id.imageView);
        textView.setText(list.get(i).getNews_title());
        imageLoaderInstances.displayImage(list.get(i).getPic_url(), imageView);


        return viewItem;
    }
}

private 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 = new Gson();
        Goods goods = gson.fromJson(s, Goods.class);
        List<Goods.DataBean> data = goods.getData();
        list.addAll(data);
        mAdapter.notifyDataSetChanged();
        uiComplete();//讓重新整理頭和重新整理底部隱藏;
    }
}

public void uiComplete(){
    xListView.setRefreshTime("剛剛");
    xListView.stopLoadMore();//讓上拉載入的ui消失
    xListView.stopRefresh();//讓重新整理頭消失
}

}