蠟筆小新
阿新 • • 發佈:2018-12-20
activity_main佈局
<?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" android:id="@+id/Drawer_Layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="嘻嘻."/> <FrameLayout android:id="@+id/Framelayout" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> <LinearLayout android:layout_width="300dp" android:layout_height="match_parent" android:orientation="vertical" android:background="#FFFFFF" android:layout_gravity="start"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="嘻嘻." /> <TextView android:id="@+id/xlistview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:padding="20dp" android:text="XlistView" /> <TextView android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:padding="20dp" android:text="TabLayout" /> </LinearLayout> </android.support.v4.widget.DrawerLayout>
frag_a
<?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"> <com.gs.gg.myxlist.me.maxwin.view.XListView android:id="@+id/list_item" android:layout_width="match_parent" android:layout_height="match_parent"> </com.gs.gg.myxlist.me.maxwin.view.XListView> </LinearLayout>
frag_b
<?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:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/tobFrag" android:layout_width="match_parent" android:layout_height="wrap_content"/> <android.support.v4.view.ViewPager android:id="@+id/paper" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout>
item_one
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageOne"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="9"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/textOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
item_two
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textTwo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="TextView" />
<ImageView
android:id="@+id/imageTwo"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="9"
app:srcCompat="@mipmap/ic_launcher" />
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView imageView;
private TextView xlistview;
private TextView tab;
private DrawerLayout Drawer_Layout;
private FragmentManager manager;
private AFragment aFragment;
private BFragment bFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//fragment
initData();
}
private void initData() {
aFragment = new AFragment();
bFragment = new BFragment();
}
private void initView() {
xlistview = (TextView) findViewById(R.id.xlistview);
xlistview.setOnClickListener(this);
tab = (TextView) findViewById(R.id.tab);
tab.setOnClickListener(this);
Drawer_Layout = (DrawerLayout) findViewById(R.id.Drawer_Layout);
manager = getSupportFragmentManager();
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.xlistview:
manager.beginTransaction().replace(R.id.Framelayout,aFragment).commit();
Drawer_Layout.closeDrawers();
break;
case R.id.tab:
manager.beginTransaction().replace(R.id.Framelayout,bFragment).commit();
Drawer_Layout.closeDrawers();
break;
}
}
}
PaperAdapter
public class PaperAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragments;
private ArrayList<String> mTitle;
public PaperAdapter(FragmentManager fm, ArrayList<Fragment> fragments, ArrayList<String> mTitle) {
super(fm);
this.fragments = fragments;
this.mTitle = mTitle;
}
@Override
public Fragment getItem(int i) {
return fragments.get(i);
}
@Override
public int getCount() {
return fragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mTitle.get(position);
}
}
XAdapter
public class XAdapter extends BaseAdapter {
private final int INTE_ONE=0;
private final int INTE_TWO=1;
private Context context;
private List<User.DataBean> list;
public XAdapter(List<User.DataBean> list, Context context) {
this.list=list;
this.context=context;
}
@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) {
HolderOne one=null;
HolderTwo two=null;
int type = getItemViewType(position);
User.DataBean bean = list.get(position);
switch (type){
case INTE_ONE:
if (convertView==null){
one=new HolderOne();
convertView=View.inflate(context,R.layout.item_one,null);
one.textOne=convertView.findViewById(R.id.textOne);
one.imageOne=convertView.findViewById(R.id.imageOne);
convertView.setTag(one);
}else {
one= (HolderOne) convertView.getTag();
}
one.textOne.setText(bean.getTitle()+"");
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),one.imageOne);
break;
case INTE_TWO:
if (convertView==null){
two=new HolderTwo();
convertView=View.inflate(context,R.layout.item_two,null);
two.textTwo=convertView.findViewById(R.id.textTwo);
two.imageTwo=convertView.findViewById(R.id.imageTwo);
convertView.setTag(two);
}else {
two= (HolderTwo) convertView.getTag();
}
two.textTwo.setText(bean.getTitle()+"");
ImageLoader.getInstance().displayImage(list.get(position).getThumbnail_pic_s(),two.imageTwo);
break;
}
return convertView;
}
//返回條目的型別
@Override
public int getItemViewType(int position) {
if (position%2!=0){
return INTE_ONE;
}else {
return INTE_TWO;
}
}
//返回條目的型別數量
@Override
public int getViewTypeCount() {
return 2;
}
class HolderOne{
TextView textOne;
ImageView imageOne;
}
class HolderTwo{
TextView textTwo;
ImageView imageTwo;
}
}
HttpUtils
public class HttpUtils {
public static String get(String string) throws Exception {
URL url=new URL(string);
//由一個有效的網址服務返回這個物件
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();
}
}
MyApp
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration loaderConfiguration=ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(loaderConfiguration);
}
}
AFragment
public class AFragment extends Fragment implements XListView.IXListViewListener {
private String mUrl = "http://www.xieast.com/api/news/news.php?page=";
private int i=1;
//private Context context;
private XListView list_item;
private XAdapter adapter;
private List<User.DataBean> list =new ArrayList<>();
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_a, container, false);
//方法(找控制元件)
initView(view);
//介面卡
adapter = new XAdapter(list,getActivity());
list_item.setAdapter(adapter);
//(非同步)方法
new MyTask().execute(mUrl+i);
return view;
}
//需要實現一個介面用來做下拉監聽和上拉監聽
@Override
public void onRefresh() {//重新整理(下拉)
list.clear();
i=1;
new MyTask().execute(mUrl+i);
}
@Override
public void onLoadMore() {//載入(上拉)
i++;
new MyTask().execute(mUrl+i);
}
//載入完停止Progress在旋轉
private void stop() {
list_item.stopRefresh();
list_item.stopLoadMore();
//設定時間
Date date=new Date();
String dateStr= (String) DateFormat.format("EEEE,MMMM dd,yyyy kk:mm:ss",date);
list_item.setRefreshTime(dateStr);
}
//自己創造方法(非同步)
class MyTask extends AsyncTask<String,Void,List<User.DataBean>>{
@Override
protected List<User.DataBean> doInBackground(String... strings) {
try {
//Gson解析
String json = HttpUtils.get(strings[0]);
Gson gson=new Gson();
User user = gson.fromJson(json, User.class);
return user.getData();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(List<User.DataBean> dataBeans) {
list.addAll(dataBeans);
adapter.notifyDataSetChanged();
stop();
}
}
//找控制元件
private void initView(View view) {
list_item = view.findViewById(R.id.list_item);
list_item.setPullLoadEnable(true);
list_item.setXListViewListener(this);
}
}
BFragment
public class BFragment extends Fragment {
private TabLayout tobFrag;
private ViewPager paper;
private ArrayList<Fragment> mList = new ArrayList<>();
private ArrayList<String> mTitle = new ArrayList<>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_b, container, false);
if (mList.isEmpty()){
initData();
}
initView(view);
return view;
}
private void initData() {
//新增fragment
mList.add(new AFragment());
mList.add(new AFragment());
//標題名
mTitle.add("新聞");
mTitle.add("娛樂");
}
private void initView(View view) {
//找控制元件
tobFrag = (TabLayout) view.findViewById(R.id.tobFrag);
paper = (ViewPager) view.findViewById(R.id.paper);
//介面卡
paper.setAdapter(new PaperAdapter(getActivity().getSupportFragmentManager(),mList,mTitle));
tobFrag.setupWithViewPager(paper);
}
}