public class MainActivity extends AppCompatActivity {
private HorizontalScrollView hs;
private LinearLayout linearlayout;
private ViewPager viewpager;
private ArrayList<TextView> textlist;
String[] s = {"福利","Android", "iOS", "休息视频", "拓展资源", "前端", "all"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hs = (HorizontalScrollView) findViewById(R.id.hs);
linearlayout = (LinearLayout) findViewById(R.id.linearlayout);
viewpager = (ViewPager) findViewById(R.id.viewpager);
// new Myasynctask().execute("http://gank.io/api/data/Android/10/1");
textlist = new ArrayList<TextView>();
for (int i = 0; i < s.length; i++) {
final TextView textView = new TextView(MainActivity.this);
textView.setText(s[i]);
textView.setTextSize(20);
textView.setId(i + 1000);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = view.getId();
viewpager.setCurrentItem(id - 1000);
}
});
//第一个默认选中状态
if (i == 0) {
textView.setTextColor(Color.RED);
} else {
textView.setTextColor(Color.BLACK);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 20, 10);
//添加到布局中
linearlayout.addView(textView, layoutParams);
//添加到集合
textlist.add(textView);
viewpager.setAdapter(new Myadapter(getSupportFragmentManager()));
viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
for (int i = 0; i < textlist.size(); i++) {
if (position == i) {
textlist.get(i).setTextColor(Color.RED);
} else {
textlist.get(i).setTextColor(Color.BLACK);
}
//获取当前的textview
TextView textView = textlist.get(position);
//width是每次滑动的距离
int width = textView.getWidth()+2;
//让scrollView滑动 滑动距离是textview之间的间距
hs.scrollTo(width * position, 0);
}
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
}
class Myadapter extends FragmentPagerAdapter {
public Myadapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return Fragment1.getins(textlist.get(position).getText().toString());
}
@Override
public int getCount() {
return textlist.size();
}
}
}
//fragment
public class Fragment1 extends Fragment{
private View vieww;
private PullToRefreshListView pulltorefresh;
private Myadapter myadapter;
List<Bean02> list;
int i=1;
private String title;
public static Fragment1 getins(String title){
Fragment1 fragment1 = new Fragment1();
Bundle bundle = new Bundle();
bundle.putString("title",title);
fragment1.setArguments(bundle);
return fragment1;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
vieww = inflater.inflate(R.layout.ff, container, false);
return vieww;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle arguments = getArguments();
title = arguments.getString("title");
pulltorefresh = (PullToRefreshListView)vieww.findViewById(R.id.pulltorefresh);
pulltorefresh.setMode(PullToRefreshBase.Mode.BOTH);
list = new ArrayList<>();
myadapter = new Myadapter();
pulltorefresh.setAdapter(myadapter);
new Myasynctask().execute("http://gank.io/api/data/"+ URLEncoder.encode(title) +"/10/1");
pulltorefresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
list.clear();
i=1;
new Myasynctask().execute("http://gank.io/api/data/"+ URLEncoder.encode(title) +"/10/1");
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
i++;
new Myasynctask().execute("http://gank.io/api/data/"+ URLEncoder.encode(title) +"/10/"+i);
}
});
}
class Myasynctask extends AsyncTask<String,Void,String> {
private StringBuilder builder;
@Override
protected String doInBackground(String... strings) {
try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int code = urlConnection.getResponseCode();
if(code==200){
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
builder = new StringBuilder();
String s = "";
//拼接
while((s=bf.readLine())!=null){
builder.append(s);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray results = jsonObject.optJSONArray("results");
for (int i=0;i<results.length();i++){
JSONArray images = results.optJSONObject(i).optJSONArray("images");
if(images!=null){
list.add(new Bean02(results.optJSONObject(i).optString("desc"),(String) images.get(0)));
}else{
list.add(new Bean02(results.optJSONObject(i).optString("desc")));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
myadapter.notifyDataSetChanged();
pulltorefresh.onRefreshComplete();
}
}
class Myadapter 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;
}
class ViewHowder{
TextView tv;
ImageView img;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Bean02 bean02 = list.get(i);
ViewHowder howder = null;
int type = getItemViewType(i);
if(type==0) {
if (view == null) {
howder = new ViewHowder();
view = View.inflate(getActivity(), R.layout.layout, null);
howder.tv = view.findViewById(R.id.textview);
howder.img = view.findViewById(R.id.imageview);
view.setTag(howder);
} else {
howder = (ViewHowder) view.getTag();
}
howder.tv.setText(bean02.getTitle());
ImageLoader.getInstance().displayImage(bean02.getImg(), howder.img);
}else{
if (view == null) {
howder = new ViewHowder();
view = View.inflate(getActivity(), R.layout.ff2, null);
howder.tv = view.findViewById(R.id.textview);
view.setTag(howder);
} else {
howder = (ViewHowder) view.getTag();
}
howder.tv.setText(bean02.getTitle());
}
return view;
}
@Override
public int getItemViewType(int position) {
Bean02 bean02 = list.get(position);
if(bean02.getImg()!=null){
return 0;
}else{
return 1;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
}
}
//BaseApplication
public class BaseApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
}
}
//APP
public class App extends BaseApplication{
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(this)
// max width, max height,即保存的每个缓存文件的最大长宽
.memoryCacheExtraOptions(480, 800)
//线程池内加载的数量
.threadPoolSize(5)
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
// You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(2 * 1024 * 1024)
.tasksProcessingOrder(QueueProcessingType.LIFO)
.defaultDisplayImageOptions(DisplayImageOptions.createSimple())
// connectTimeout (5 s), readTimeout (30 s)超时时间
.imageDownloader(new BaseImageDownloader(this, 5 * 1000, 30 * 1000))
// Remove for release app
.writeDebugLogs()
//开始构建
.build();
//初始化imageloader;
ImageLoader.getInstance().init(config);
}
}
//Bean02
public class Bean02 {
private String title;
private String img;
public Bean02(String title, String img) {
this.title = title;
this.img = img;
}
public Bean02(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
//main_Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="activity.example.com.baikaijun20170918.MainActivity">
<HorizontalScrollView
android:id="@+id/hs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</HorizontalScrollView>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></android.support.v4.view.ViewPager>
</RelativeLayout>
//ff
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pulltorefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView>
</RelativeLayout>
//ff2
<?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">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
// layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageview"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/textview"
android:src="@mipmap/ic_launcher"/>
</RelativeLayout>