抽屜 加 主頁面顯示資料
activity_main.xml佈局 (整體佈局)
<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android” android:id="@+id/dl_root" android:layout_width=“match_parent” android:layout_height=“match_parent”>
<LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <com.bwie.xlistview.XListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content"></com.bwie.xlistview.XListView> </LinearLayout> <LinearLayout android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#595353" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@mipmap/ic_launcher_round" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="網易雲" /> <ListView android:id="@+id/list_drawer" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity頁面(抽屜的 顯示資料 的)
package com.example.lenovo.a06mo1;
import android.content.Intent; import android.os.AsyncTask; import android.os.Handler; import android.support.annotation.NonNull; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.Gravity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView;
import com.bwie.xlistview.XListView; import com.google.gson.Gson;
import java.util.ArrayList; import java.util.List;
public class MainActivity extends AppCompatActivity { public static final String url=“http://www.xieast.com/api/news/news.php?page=”; private static final String TAG=“MainActivity”; private DrawerLayout dlRoot; private ListView lvDrawer; private List list; private XListView xListView; private NewsAdapter adapter; private List<News.DataBean> xlist; private int page=1; private Handler handler=new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dlRoot=findViewById(R.id.dl_root); lvDrawer=findViewById(R.id.list_drawer);
xlist=new ArrayList<>();
xListView=findViewById(R.id.list_view);
adapter=new NewsAdapter(this,xlist);
xListView.setAdapter(adapter);
xListView.setPullRefreshEnable(true);
xListView.setPullLoadEnable(true);
xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent=new Intent(MainActivity.this,ShowActivity.class);
startActivity(intent);
}
});
xListView.setXListViewListener(new XListView.IXListViewListener() {
@Override
public void onRefresh() {
page=1;
getdata(page,true);
}
@Override
public void onLoadMore() {
page++;
getdata(page,false);
}
});
getdata(page,false);
list=new ArrayList<>();
list.add("我的音樂");
list.add("我的會員");
list.add("我的網盤");
list.add("我的關注");
lvDrawer.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,list));
lvDrawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NewsFragment fragment=NewsFragment.newInstance(list.get(position));
// getSupportFragmentManager().beginTransaction().replace(R.i,fragment).commit(); dlRoot.closeDrawer(Gravity.START); } }); dlRoot.addDrawerListener(new DrawerLayout.DrawerListener() { @Override public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
}
@Override
public void onDrawerStateChanged(int newState) {
switch (newState){
case DrawerLayout.STATE_SETTLING:
Log.i(TAG,"STATE_SETTLING");
break;
case DrawerLayout.STATE_DRAGGING:
Log.i(TAG,"STATE_DRAGGING");
break;
case DrawerLayout.STATE_IDLE:
Log.i(TAG,"STATE_IDLE");
break;
}
}
});
}
private void getdata(int page,final boolean b) {
new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String... strings) {
return HttpUtils.getInputHttp(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson=new Gson();
News news=gson.fromJson(s,News.class);
if (news!=null){
List<News.DataBean> data=news.getData();
if (data!=null){
if (!b){
xlist.clear();
}
xlist.addAll(data);
adapter.notifyDataSetChanged();
}
}
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (b){
xListView.stopLoadMore();
}else{
xListView.stopRefresh();
xListView.setRefreshTime("剛剛");
}
}
},2000);
}
}.execute(url+page);
}
}
NewsFragment頁面
package com.example.lenovo.a06mo1;
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.TextView;
/**
- Created by lenovo on 2018-10-20. */
public class NewsFragment extends Fragment{ private static final String FLAG=“按入”; private TextView txtShow; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_news, null, false); txtShow=v.findViewById(R.id.txt_show); return v; }
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle bundle=getArguments();
String result=bundle.getString(FLAG);
txtShow.setText(result);
}
/**
* 靜態傳值
* @param
* @return
*/
public static NewsFragment newInstance(String str) {
NewsFragment fragment=new NewsFragment();
Bundle bundle=new Bundle();
bundle.putString(FLAG,str);
fragment.setArguments(bundle);
return fragment;
}
}
HttpUtils
package com.example.lenovo.a06mo1;
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;
/**
- Created by lenovo on 2018-10-20. */
public class HttpUtils { public static String getInputHttp(String urlString){ String result=""; try { URL url=new URL(urlString); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod(“GET”); connection.setUseCaches(true); connection.setDoInput(true); connection.setDoOutput(false); connection.setConnectTimeout(3000); connection.connect();
if (connection.getResponseCode()==200){
InputStream is=connection.getInputStream();
result=getInputStream(is);
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String getInputStream(InputStream is) {
String result="";
try {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int length=-1;
byte[] buffer=new byte[1024];
while ((length=is.read(buffer,0,buffer.length))!=-1){
baos.write(buffer,0,length);
baos.flush();
}
result=baos.toString();
baos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}