側拉列表展示
//MainActivity的xml
<?xml version="1.0" encoding="UTF-8"?>-<android.support.v4.widget.DrawerLayout android:layout_height=“match_parent” android:layout_width=“match_parent” android:id="@+id/Drawer_Layout" xmlns:android=“http://schemas.android.com/apk/res/android”>
-
-
</android.support.v4.widget.DrawerLayout>
//MainActivityd 類
package com.zhuyue.dell.monilianxi;
import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.view.ViewPager; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.FrameLayout; import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FrameLayout Framelayout; private TextView xtv; private TextView ltv; private DrawerLayout Drawer_Layout; private AFragment aFragment; private BFragment bFragment; private FragmentManager manager; private ViewPager vp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); initView(); } private void initData() { aFragment = new AFragment(); bFragment = new BFragment(); } private void initView() { Framelayout = (FrameLayout) findViewById(R.id.Framelayout); xtv = (TextView) findViewById(R.id.xtv); xtv.setOnClickListener(this); ltv = (TextView) findViewById(R.id.ltv); ltv.setOnClickListener(this); Drawer_Layout = (DrawerLayout) findViewById(R.id.Drawer_Layout); manager=getSupportFragmentManager(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.xtv: manager.beginTransaction().replace(R.id.Framelayout,aFragment).commit(); Drawer_Layout.closeDrawers(); break; case R.id.ltv: manager.beginTransaction().replace(R.id.Framelayout,bFragment).commit(); Drawer_Layout.closeDrawers(); break; } }
}
建一個applicationdemo包 package com.zhuyue.dell.monilianxi.applicationdemo;
import android.app.Application; import android.os.Environment;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache; import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator; import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.assist.QueueProcessingType; import com.nostra13.universalimageloader.core.decode.BaseImageDecoder; import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import java.io.File;
/**
-
你只要寫了Application必須要註冊如果不註冊就會報錯 */ public class MyApp extends Application {
@Override public void onCreate() { super.onCreate(); ImageLoaderConfiguration imageLoaderConfiguration = ImageLoaderConfiguration.createDefault(this); ImageLoader.getInstance().init(imageLoaderConfiguration); } }
Utils 工具包
package com.zhuyue.dell.monilianxi.utils;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL;
public class HttpUtils {
public static String get(String urlStr) throws Exception {
URL url = new URL(urlStr);
//由一個有效的網址服務返回這個物件
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//設定請求方式
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
InputStream stream = connection.getInputStream();
String inputStr = getInputStr(stream);
return inputStr;
}
//把流轉成字串
private static String getInputStr(InputStream stream) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str);
}
return sb.toString();
}
}
//AFragment的xml
<?xml version="1.0" encoding="utf-8"?><com.zhuyue.dell.mylibrary.view.XListView
android:id="@+id/X_ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
AFragment的類
package com.zhuyue.dell.monilianxi;
import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
import com.google.gson.Gson; import com.zhuyue.dell.monilianxi.Aadapter.MyAdapter; import com.zhuyue.dell.monilianxi.bean.Json; import com.zhuyue.dell.monilianxi.utils.HttpUtils; import com.zhuyue.dell.mylibrary.view.XListView;
import java.util.ArrayList;
/**
-
A simple {@link Fragment} subclass. */ public class AFragment extends Fragment implements XListView.IXListViewListener {
private String mUrl = “http://www.xieast.com/api/news/news.php?page=”; private int index = 1; private View mView; private ArrayList<Json.DataBean> mList = new ArrayList<>(); private XListView X_ListView; private MyAdapter mAdapter;
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mView = inflater.inflate(R.layout.fragment_a, container, false);
initView(mView); mAdapter=new MyAdapter(mList,getActivity()); X_ListView.setAdapter(mAdapter); new MyTask().execute(mUrl+index); return mView;
}
private void initView(View mView) { X_ListView = mView.findViewById(R.id.X_ListView); X_ListView.setPullLoadEnable(true); X_ListView.setXListViewListener(this); }
@Override public void onRefresh() { mList.clear(); index=1; new MyTask().execute(mUrl+index);
} public void stop(){ X_ListView.stopRefresh(); X_ListView.stopLoadMore(); X_ListView.setRefreshTime(“剛剛”);
}
@Override public void onLoadMore() { index++; new MyTask().execute(mUrl+index);
} class MyTask extends AsyncTask<String,Void,ArrayList<Json.DataBean>>{
@Override protected ArrayList<Json.DataBean> doInBackground(String... strings) { try { String s = HttpUtils.get(strings[0]); Gson gson = new Gson(); Json json = gson.fromJson(s, Json.class); return (ArrayList<Json.DataBean>)json.getData(); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(ArrayList<Json.DataBean> dataBeans) { mList.addAll(dataBeans); mAdapter.notifyDataSetChanged(); stop(); }
} }
//AFragment的介面卡 package com.zhuyue.dell.monilianxi.Aadapter;
import android.content.Context; import android.graphics.Bitmap; 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.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer; import com.zhuyue.dell.monilianxi.R; import com.zhuyue.dell.monilianxi.bean.Json;
import java.util.ArrayList;
public class MyAdapter extends BaseAdapter { private final int ITEM_ONE = 0; private final int ITEM_TWO = 1; private ArrayList<Json.DataBean> mList; private Context mContext; private ImageLoader mImageLoader = ImageLoader.getInstance(); private DisplayImageOptions mDisplayImageOp;
public MyAdapter(ArrayList<Json.DataBean> mList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
mDisplayImageOp = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher)
.cacheInMemory(true)
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.displayer(new CircleBitmapDisplayer())
.build();
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
HolderOne one = null;
HolderTwo two = null;
int type = getItemViewType(position);
Json.DataBean dataBean = mList.get(position);
switch (type) {
case ITEM_ONE:
if (convertView == null) {
one = new HolderOne();
convertView = View.inflate(mContext, R.layout.item_one, null);
one.mNameOne = convertView.findViewById(R.id.Item_One_Text);
one.mImageOne = convertView.findViewById(R.id.Item_One_Image);
convertView.setTag(one);
} else {
one = (HolderOne) convertView.getTag();
}
one.mNameOne.setText(dataBean.getTitle() + "");
mImageLoader.displayImage(dataBean.getThumbnail_pic_s(), one.mImageOne, mDisplayImageOp);
break;
case ITEM_TWO:
if (convertView == null) {
two = new HolderTwo();
convertView = View.inflate(mContext, R.layout.item_two, null);
two.mNameTwo = convertView.findViewById(R.id.Item_Two_Text);
two.mImageTwo = convertView.findViewById(R.id.Item_Two_Image);
convertView.setTag(two);
} else {
two= (HolderTwo) convertView.getTag();
}
two.mNameTwo.setText(dataBean.getTitle()+"");
mImageLoader.displayImage(dataBean.getThumbnail_pic_s(),two.mImageTwo,mDisplayImageOp);
break;
}
return convertView;
}
@Override
public int getItemViewType(int position) {
if (position % 2 == 0) {
return ITEM_ONE;
} else {
return ITEM_TWO;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
class HolderOne {
private TextView mNameOne;
private ImageView mImageOne;
}
class HolderTwo {
private TextView mNameTwo;
private ImageView mImageTwo;
}
}
//介面卡的xml //item_one
<?xml version="1.0" encoding="utf-8"?><ImageView
android:id="@+id/Item_One_Image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/Item_One_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="heheda" />
//item_two
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/Item_Two_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="heheda" />
<ImageView
android:id="@+id/Item_Two_Image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher_round" />
//BFragment的xml
<?xml version="1.0" encoding="utf-8"?><!-- TODO: Update blank fragment layout -->
<android.support.design.widget.TabLayout
android:id="@+id/BFrag_Tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/BFrag_Pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/BFrag_Tab"/>
//BFragment的類 package com.zhuyue.dell.monilianxi;
import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;
import com.zhuyue.dell.monilianxi.Badapter.MyPagerAdapter;
import java.util.ArrayList;
/**
-
A simple {@link Fragment} subclass. */ public class BFragment extends Fragment { private View mView; private TabLayout BFage_Tab; private ViewPager BFage_Pager; private ArrayList mList = new ArrayList<>(); private ArrayList mTitle = new ArrayList<>();
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mView = inflater.inflate(R.layout.fragment_b, container, false); initData(); initView(mView); return mView; }
private void initData() { mList.add(new AFragment()); mList.add(new AFragment()); mTitle.add(“新聞”); mTitle.add(“娛樂”); }
private void initView(View mView) { BFage_Tab = mView.findViewById(R.id.BFrag_Tab); BFage_Pager = mView.findViewById(R.id.BFrag_Pager); BFage_Pager.setAdapter(new MyPagerAdapter(getChildFragmentManager(), mList, mTitle)); BFage_Tab.setupWithViewPager(BFage_Pager);
}
} //menu檔案 的main.xml
<?xml version="1.0" encoding="utf-8"?>