1activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/draw"
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="com.example.xin09921.MainActivity">
<HorizontalScrollView
android:id="@+id/hsv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/hll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
></LinearLayout>
</HorizontalScrollView>
<android.support.v4.view.ViewPager
android:id="@+id/hvp"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:id="@+id/cll"
android:layout_width="300dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="left"
android:choiceMode="singleChoice"
android:background="#fe0"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="侧滑栏"
android:textSize="30dp"
/>
<ListView
android:id="@+id/clv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
2fragmen_pull
<?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.handmark.pulltorefresh.library.PullToRefreshListView
android:id="@+id/pullLv"
android:layout_width="match_parent"
android:layout_height="match_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>
3pull_item_layout.xml
<?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:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="正在加载..."
android:textSize="30dp"
/>
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
4
activity_view_pager.xml
<?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="com.example.xin09921.Main2Activity">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp" />
<LinearLayout
android:id="@+id/ll"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
></LinearLayout>
</RelativeLayout>
5主activty
package com.example.xin09921;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private HorizontalScrollView hsv;
private LinearLayout hll;
private LinearLayout cll;
private ListView clv;
private String[] titles = new String[]{"福利","Android","iOS","休息视频","拓展资源","前端","all"};
private DrawerLayout draw;
private List<TextView> list;
private Mvpdapter mvpdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intiView();
FragmentManager supportFragmentManager = getSupportFragmentManager();
mvpdapter = new Mvpdapter(supportFragmentManager);
vp.setAdapter(mvpdapter);
vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
for (int i=0;i<list.size();i++)
{
if (position==i)
{
list.get(i).setTextColor(Color.RED);
}else {
list.get(i).setTextColor(Color.BLACK);
}
}
TextView textView = list.get(position);
int i = textView.getWidth() + 10;
hsv.scrollTo(i*position,0);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
clv.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,titles));
clv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
vp.setCurrentItem(i);
Fragment1.getString("http://gank.io/api/data/"+ URLEncoder.encode(titles[i])+"/");
draw.closeDrawers();
}
});
}
class Mvpdapter extends FragmentPagerAdapter{
public Mvpdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return Fragment1.getString("http://gank.io/api/data/"+ URLEncoder.encode(titles[position])+"/");
}
@Override
public int getCount() {
return list.size();
}
}
private void intiView() {
vp = (ViewPager) findViewById(R.id.hvp);
hsv = (HorizontalScrollView) findViewById(R.id.hsv);
hll = (LinearLayout) findViewById(R.id.hll);
cll = (LinearLayout) findViewById(R.id.cll);
clv = (ListView) findViewById(R.id.clv);
draw = (DrawerLayout) findViewById(R.id.draw);
//创建集合存放textview
list = new ArrayList<>();
//创建textview
for(int i=0;i<titles.length;i++){
//创建Textview
TextView textView = new TextView(MainActivity.this);
textView.setText(titles[i]);
textView.setTextSize(20);
textView.setId(i+1000);
//点击事件
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int id = view.getId();
vp.setCurrentItem(id-1000);
}
});
//刚进入程序第一个为红色
if(i==0){
textView.setTextColor(Color.RED);
}else{
textView.setTextColor(Color.BLACK);
}
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(20,10,20,10);
hll.addView(textView,params);
//添加到集合
list.add(textView);
}
}
}
6第二个无限轮播
package com.example.xin09921;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;
public class Main2Activity extends AppCompatActivity {
private ViewPager vp;
private LinearLayout ll;
private List<ImageView> ivlist = new ArrayList<>();
private String[] urlStr = new String[]{"http://pic8.nipic.com/20100701/5290458_114840036316_2.jpg",
"http://pic2.nipic.com/20090424/1468853_230119053_2.jpg",
"http://img3.3lian.com/2013/s1/20/d/57.jpg",
"http://pic39.nipic.com/20140226/18071023_164300608000_2.jpg",
"http://a0.att.hudong.com/15/08/300218769736132194086202411_950.jpg"};
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int i=vp.getCurrentItem();
i++;
vp.setCurrentItem(i);
sendEmptyMessageDelayed(0,2000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
vp = (ViewPager) findViewById(R.id.vp);
ll = (LinearLayout) findViewById(R.id.ll);
MVPAdapter mvpAdapter = new MVPAdapter();
vp.setAdapter(mvpAdapter);
initDot();
vp.setCurrentItem(1000*urlStr.length);
handler.sendEmptyMessageDelayed(0,2000);
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for(int i=0;i<ivlist.size();i++){
if(position%urlStr.length==i){
ivlist.get(position%urlStr.length).setImageResource(R.drawable.dot_focuable);
}else {
ivlist.get(i).setImageResource(R.drawable.dot_normal);
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initDot() {
if(ivlist!=null){
ivlist.clear();
}
for(int i=0;i<urlStr.length;i++){
ImageView imageView = new ImageView(Main2Activity.this);
if(i==0){
imageView.setImageResource(R.drawable.dot_focuable);
}else{
imageView.setImageResource(R.drawable.dot_normal);
}
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);
layoutParams.setMargins(10,0,10,0);
ll.addView(imageView,layoutParams);
ivlist.add(imageView);
}
}
class MVPAdapter extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(Main2Activity.this);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(urlStr[position%urlStr.length],imageView);
container.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
startActivity(intent);
}
});
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
}
7.fragment纵向滑动
package com.example.xin09921;
import android.os.AsyncTask;
import android.os.Bundle;
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.bawei.wzq.wzlibrary.utils.NetWorkUtil;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* author:Created by WangZhiQiang on 2017/9/21.
*/
public class Fragment1 extends Fragment {
int i = 0;
private View view;
private String url;
private PullToRefreshListView pullLv;
private List<Strbean> list;
private MpullAdapter mpullAdapter;
public static Fragment1 getString(String s) {
Fragment1 fragment1 = new Fragment1();
Bundle bundle = new Bundle();
bundle.putString("Url", s);
fragment1.setArguments(bundle);
return fragment1;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle arguments = getArguments();
url = arguments.getString("Url");
pullLv = (PullToRefreshListView) view.findViewById(R.id.pullLv);
pullLv.setMode(PullToRefreshBase.Mode.BOTH);
list = new ArrayList<>();
mpullAdapter = new MpullAdapter();
pullLv.setAdapter(mpullAdapter);
new MAsyncTask().execute(url + "10/1");
pullLv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
list.clear();
i = 1;
new MAsyncTask().execute(url + "10/1");
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
i++;
new MAsyncTask().execute(url + "10/" + i);
}
});
}
class MAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
String netString = new NetWorkUtil().getNetString(strings[0]);
Log.e("33TAG---------------", netString);
return netString;
/* try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
int responseCode = urlConnection.getResponseCode();
if (responseCode ==200){
InputStream inputStream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer buffer = new StringBuffer();
String string = "";
while ((string=reader.readLine())!=null){
buffer.append(reader);
}
Log.e("bbbb--------",buffer.toString() );
return buffer.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;*/
}
//http://img3.3lian.com/2013/s1/20/d/57.jpg
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.e("sss-------------", 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 Strbean((String) images.get(0), results.optJSONObject(i).optString("desc")));
} else {
String u = "http://img3.3lian.com/2013/s1/20/d/57.jpg";
list.add(new Strbean(u, results.optJSONObject(i).optString("desc")));
}
}
mpullAdapter.notifyDataSetChanged();
pullLv.onRefreshComplete();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
class MpullAdapter 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 iv;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHowder vh = null;
if (view == null) {
view = View.inflate(getActivity(), R.layout.itempull, null);
vh = new ViewHowder();
vh.iv = (ImageView) view.findViewById(R.id.iv);
vh.tv = (TextView) view.findViewById(R.id.tv);
view.setTag(vh);
} else {
vh = (ViewHowder) view.getTag();
}
vh.tv.setText(list.get(i).getTv());
ImageLoader.getInstance().displayImage(list.get(i).getImg(), vh.iv);
return view;
}
}
}
8.存放
package com.example.xin09921;
/**
* author:Created by WangZhiQiang on 2017/9/21.
*/
public class Strbean {
private String img;
private String tv;
public Strbean(String img, String tv) {
this.img = img;
this.tv = tv;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
public String getTv() {
return tv;
}
public void setTv(String tv) {
this.tv = tv;
}
@Override
public String toString() {
return "Strbean{" +
"img='" + img + '\'' +
", tv='" + tv + '\'' +
'}';
}
}
9.图片的获取
package com.example.xin09921;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
/**
* author:Created by WangZhiQiang on 2017/9/21.
*/
public class Mapp extends Application{
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration build = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(build);
}
}
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xin09921">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:name=".Mapp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>