package exam3.myapplication;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import fragments.Sy;
import fragments.Wd;
import fragments.Wtt;
import fragments.Xg;
public class MainActivity extends AppCompatActivity {
private TextView shouye;
private TextView xigua;
private TextView wtt;
private TextView wode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shouye = (TextView) findViewById(R.id.shouye);
xigua = (TextView) findViewById(R.id.xigua);
wtt = (TextView) findViewById(R.id.wtt);
wode = (TextView) findViewById(R.id.wode);
//展示第一个视图
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fl,new Sy());
transaction.commit();
shouye.setTextColor(Color.RED);
xigua.setTextColor(Color.GRAY);
wtt.setTextColor(Color.GRAY);
wode.setTextColor(Color.GRAY);
//点击事件
shouye.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fl,new Sy());
transaction.commit();
shouye.setTextColor(Color.RED);
xigua.setTextColor(Color.GRAY);
wtt.setTextColor(Color.GRAY);
wode.setTextColor(Color.GRAY);
}
});
xigua.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fl,new Xg());
transaction.commit();
shouye.setTextColor(Color.GRAY);
xigua.setTextColor(Color.RED);
wtt.setTextColor(Color.GRAY);
wode.setTextColor(Color.GRAY);
}
});
wtt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fl,new Wtt());
transaction.commit();
shouye.setTextColor(Color.GRAY);
xigua.setTextColor(Color.GRAY);
wtt.setTextColor(Color.RED);
wode.setTextColor(Color.GRAY);
}
});
wode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager manager=getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.fl,new Wd());
transaction.commit();
shouye.setTextColor(Color.GRAY);
xigua.setTextColor(Color.GRAY);
wtt.setTextColor(Color.GRAY);
wode.setTextColor(Color.RED);
}
});
}
}
apackage exam3.myapplication;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;
public class MyFragment extends Fragment{
private ListView lv;
private List<Newslist> list;
private String mString;
private JsonRootBean bean;
private String path;
private int cont=10;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
bean = (JsonRootBean) msg.obj;
list = bean.getNewslist();
myad = new Myad();
lv.setAdapter(myad);
}
};
private Myad myad;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.news_layout,container,false);
final PullToRefreshLayout pull=view.findViewById(R.id.pull);
lv = view.findViewById(R.id.pull_lv);
pull.setRefreshListener(new BaseRefreshListener() {
@Override
public void refresh() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
cont++;
path = "http://api.tianapi.com/meinv/?key=6dd96d7344c784e7d5a6f6dfd118cde3&num=" + cont;
getData();
myad.notifyDataSetChanged();
pull.finishRefresh();
}
},2000);
}
@Override
public void loadMore() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
cont++;
path = "http://api.tianapi.com/meinv/?key=6dd96d7344c784e7d5a6f6dfd118cde3&num=" + cont;
getData();
myad.notifyDataSetChanged();
pull.finishLoadMore();
}
},2000);
}
});
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle bundle =getArguments();
mString = bundle.getString("name","福利");
getData();
}
public void getData() {
new Thread(){
@Override
public void run() {
if (path==null) {
path = "http://api.tianapi.com/meinv/?key=6dd96d7344c784e7d5a6f6dfd118cde3&num=" + cont;
}
try {
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
if (connection.getResponseCode() == 200) {
InputStream stream = connection.getInputStream();
String json = zhuan(stream, "utf-8");
Log.i("TAG",json);
Gson gson = new Gson();
bean = gson.fromJson(json, JsonRootBean.class);
Message message = Message.obtain();
message.what = 0;
message.obj = bean;
handler.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
private String zhuan(InputStream stream, String s) {
try {
InputStreamReader in=new InputStreamReader(stream,s);
BufferedReader re=new BufferedReader(in);
String ss=null;
StringBuilder builder=new StringBuilder();
while((ss=re.readLine())!=null){
builder.append(ss);
}
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
class Myad extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
He he = null;
if (view == null) {
view = View.inflate(getActivity(), R.layout.item, null);
he = new He();
he.item_img1 = view.findViewById(R.id.item_img1);
he.item_img2 = view.findViewById(R.id.item_img2);
he.item_img3 = view.findViewById(R.id.item_img3);
he.tv = view.findViewById(R.id.tv);
view.setTag(he);
} else {
he = (He) view.getTag();
}
he.tv.setText(list.get(i).getTitle());
ImageLoader.getInstance().displayImage(list.get(i).getPicUrl(),he.item_img1);
ImageLoader.getInstance().displayImage(list.get(i).getPicUrl(),he.item_img2);
return view;
}
class He {
ImageView item_img1,item_img2,item_img3;
TextView tv;
}
}
}
package fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import exam3.myapplication.MyFragment;
import exam3.myapplication.R;
public class Sy extends Fragment{
private TabLayout tab;
private ViewPager vp;
private List<String> list;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v=View.inflate(getActivity(), R.layout.sy,null);
tab = v.findViewById(R.id.tab);
vp = v.findViewById(R.id.vp);
//福利、Android、iOS、休息视频、拓展资源、前端、all
//给集合添加数据
list=new ArrayList<String>();
list.add("福利");
list.add("Android");
list.add("iOS");
list.add("休息视频");
list.add("拓展资源");
list.add("前端");
list.add("all");
vp.setAdapter(new FragmentPagerAdapter(getActivity().getSupportFragmentManager()) {
@Override
public CharSequence getPageTitle(int position) {
return list.get(position);
}
@Override
public Fragment getItem(int position) {
MyFragment fragment_1 = new MyFragment();
Bundle bundle=new Bundle();
//判断赋值
if(list.get(position).equals("福利")){
bundle.putString("name","福利");
}else if(list.get(position).equals("Android")){
bundle.putString("name","Android");
}else if(list.get(position).equals("iOS")){
bundle.putString("name","iOS");
}else if(list.get(position).equals("休息视频")){
bundle.putString("name","休息视频");
}else if(list.get(position).equals("拓展资源")){
bundle.putString("name","拓展资源");
}else if(list.get(position).equals("前端")){
bundle.putString("name","前端");
}else if(list.get(position).equals("all")) {
bundle.putString("name", "all");
}
fragment_1.setArguments(bundle);
return fragment_1;
}
@Override
public int getCount() {
return list.size();
}
});
tab.setupWithViewPager(vp);
return v;
}
}
package exam3.myapplication;
import android.app.Application;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
public class Img extends Application{
@Override
public void onCreate() {
DisplayImageOptions options=new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.ic_launcher).displayer(new FadeInBitmapDisplayer(2000)).cacheOnDisc(true).cacheInMemory(true).build();
ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(options).threadPoolSize(5).build();
ImageLoader.getInstance().init(configuration);
super.onCreate();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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" tools:context="exam3.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll"
android:orientation="horizontal"
android:gravity="center"
android:layout_alignParentBottom="true"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="首页"
android:id="@+id/shouye"
android:textColor="#f00"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="西瓜视频"
android:id="@+id/xigua"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="微头条"
android:id="@+id/wtt"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="我的"
android:id="@+id/wode"
android:gravity="center"/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/ll"
android:id="@+id/fl">
</FrameLayout>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:id="@+id/item_img1"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:src="@mipmap/ic_launcher"
android:layout_height="wrap_content"
android:id="@+id/item_img2"/>
<ImageView
android:layout_width="0dp"
android:layout_weight="1"
android:src="@mipmap/ic_launcher"
android:layout_height="wrap_content"
android:id="@+id/item_img3"/>
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android " xmlns:app="http://schemas.android.com/apk/res-auto " android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.TabLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tab" app:tabIndicatorColor="#f5e20d" app:tabIndicatorHeight="5dp" app:tabGravity="center" app:tabSelectedTextColor="#090df7" app:tabTextColor="#000000" app:tabMode="scrollable" android:fillViewport="false"> </android.support.design.widget.TabLayout> <android.support.v4.view.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/vp" ></android.support.v4.view.ViewPager> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.jwenfeng.library.pulltorefresh.PullToRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pull">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pull_lv"></ListView>
</com.jwenfeng.library.pulltorefresh.PullToRefreshLayout>
</LinearLayout>