您的位置:首页 > 其它

ViewPage+Fragment+ListView+ImageLoade的无限轮播

2017-08-24 11:39 357 查看
主Activaty



public class MainActivity extends FragmentActivity {
ViewPager Vp;
RadioGroup Rg;
List<Fragment> listF;
RadioButton Rb1,Rb2;
FragmentApdate Fa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化数据
Vp = (ViewPager) findViewById(R.id.Vp);
Rg = (RadioGroup) findViewById(R.id.Rg);
Rb1 = findViewById(R.id.Rb1);
Rb2 = findViewById(R.id.Rb2);
//添加数据
initData();
Fa = new FragmentApdate(getSupportFragmentManager(),listF);
Vp.setAdapter(Fa);
Vp.setCurrentItem(0);
//ViewPager的监听
Vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {

switch (position){
case 0:
Rg.check(R.id.Rb1);
Rb1.setBackgroundColor(Color.BLUE);
Rb2.setBackgroundColor(Color.GREEN);
break;
case 1:
Rg.check(R.id.Rb2);
Rb1.setBackgroundColor(Color.GREEN);
Rb2.setBackgroundColor(Color.BLUE);
break;
}
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
//RadioGroup的监听
Rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {

switch(i){
case R.id.Rb1:
Vp.setCurrentItem(0);

break;
case R.id.Rb2:
Vp.setCurrentItem(1);
break;
}
}
});
}
private void initData() {
listF = new ArrayList<Fragment>();
listF.add(new FrgmentOne());
listF.add(new FrgmentTwo());
}
}

主Activaty的布局

<android.support.v4.view.ViewPager
android:id="@+id/Vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</android.support.v4.view.ViewPager>
<RadioGroup
android:id="@+id/Rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom=[b]"true"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/Rb1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="@null"
android:text="新闻界面"
android:gravity="center"
android:textSize="20dp"
android:textColor="@android:color/background_dark"
/>
<RadioButton
android:id="@+id/Rb2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:button="@null"
android:text="我的界面"
android:gravity="center"
android:textSize="20dp"
android:textColor="@android:color/holo_blue_dark"
/>
</RadioGroup>
Fragment适配器
public class FragmentApdate extends FragmentPagerAdapter {
List<Fragment> listF = new ArrayList<Fragment>();
public FragmentApdate(FragmentManager fm, List<Fragment> listF) {
super(fm);
this.listF = listF;
}
public FragmentApdate(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return listF.get(position);
}

@Override
public int getCount() {
return listF.size();
}
}
GriwView的适配器
public class GridViewApdate extends BaseAdapter {
List<String> list;
Context context;

public GridViewApdate(List<String> list, Context context) {
super();
this.list = list;
this.context = context;
}

@Override
public int getCount() {
if (list != null) {
return list.size();
}
return 0;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
convertView = View.inflate(context, R.layout.grid_time, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.tv_grid);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(list.get(position));
return convertView;
}

public void setData(List<String> list){
this.list = list;
notifyDataSetChanged();
}

static class ViewHolder {
TextView tv;
}

}
网络请求数据的dao包
public class NetuS {
//轮播
public static Reader getNetData() {
Reader reader = null;
try {
URL url = new URL(
"http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=banner");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();
reader = new InputStreamReader(is);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return reader;
}
//ListView
public static String getNetDataStr() {
String json = null;
try {
URL url = new URL(
"http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=one");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();

ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = -1;
byte[] buff = new byte[1024];
while((len = is.read(buff)) != -1){
bos.write(buff,0,len);
}

json = new String(bos.toByteArray());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}
// GridView
public static String getNetDataGrid(String s) {
String json = null;
try {
URL url = new URL(
"http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=channel");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
InputStream is = conn.getInputStream();

ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len = -1;
byte[] buff = new byte[1024];
while((len = is.read(buff)) != -1){
bos.write(buff,0,len);
}

json = new String(bos.toByteArray());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return json;
}

}
第一个Fragmentone的功能
public class FrgmentOne extends Fragment{
Imagebean Ib ;
List<ImageView> list;
ListUtil Lu;
ViewPager VpF;
ListView lv_fone;
//用于实现无限轮播
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
//获取当前条目
int index = VpF.getCurrentItem();
//向右轮播
VpF.setCurrentItem(index+=1);
//间隔2秒
handler.sendEmptyMessageDelayed(2,2000);
};
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view =View.inflate(getActivity(), R.layout.fragmentone,null);
VpF = view.findViewById(R.id.vpf);
lv_fone = view.findViewById(R.id.lv_fone);
Lu = new ListUtil();
Ib = new Imagebean();
getBeanByGson();
ListGson();
return view;
}
//轮播图的解析
public void getBeanByGson() {
// 使用该方法得到了一个 Json的字符输入流
new AsyncTask<String,String,String>(){
@Override
protected String doInBackground(String... strings) {
Reader reader = NetuS.getNetData();
// 使用Gson把该字符输入流转换成JavaBean
Gson gson = new Gson();
Ib = gson.fromJson(reader, Imagebean.class);
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
list=new ArrayList<ImageView>();
for (int i = 0; i <Ib.getBanner().size() ; i++) {
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
ImageLoader.getInstance().displayImage(Ib.getBanner().get(i).getImage_url(),imageView);
list.add(imageView);
}
MyPagerAdapter myPagerAdapter   = new MyPagerAdapter();
VpF.setAdapter(myPagerAdapter);
handler.sendEmptyMessageDelayed(1,1000);
}
}.execute();
}
//ListView的解析
public void ListGson(){
new AsyncTask<String,String,String>(){
@Override
protected String doInBackground(String... strings) {
String read = NetuS.getNetDataStr();
//Log.i("lureader", "lureader"+ lureader);
// 使用Gson把该字符输入流转换成JavaBean
Gson Lvgson = new Gson();
Lu = Lvgson.fromJson(read, ListUtil.class);
return null;
}
@Override
protected void onPostExecute(String result) {
MyApdateF myApdateF =new MyApdateF();
lv_fone.setAdapter(myApdateF);
myApdateF.notifyDataSetChanged();
super.onPostExecute(result);

}
}.execute();
}

//ViewGroup适配器
class MyPagerAdapter extends PagerAdapter{

@Override
public int getCount() {
return Integer.MAX_VALUE;//Integer.MAX_VALUE可实现无限轮播
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view  == object;//固定的
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
position %= list.size();
container.addView(list.get(position));
return list.get(position);
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}

//ListView适配器
class MyApdateF extends BaseAdapter{
@Override
public int getCount() {
return Lu == null?0:Lu.getData().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) {
ViewHolder holder;
if (view == null){
view = View.inflate(getActivity(),R.layout.list_padate,null);
//初始化holder
holder = new ViewHolder();
holder.iv_list = view.findViewById(R.id.iv_list);
holder.tv_list = view.findViewById(R.id.tv_list);
holder.tv_list2=view.findViewById(R.id.tv_list2);
//赋值
view.setTag(holder);
}else{
//取值
holder = (ViewHolder) view.getTag();
}
holder.tv_list.setText(Lu.getData().get(i).getTitle());
holder.tv_list2.setText(Lu.getData().get(i).getContent());
ImageLoader.getInstance().displayImage(Lu.getData().get(i).getImage_url(),holder.iv_list);
return view;
}
}
class ViewHolder{
TextView tv_list, tv_list2;
ImageView iv_list;
}
}
第二个Fragmenttwo
public class FrgmentTwo extends Fragment{
View view;
GridView gv_f2s;
GridView gv_f2x;
List<String> lsitm;
List<String> listn;
GridViewApdate gridViewApdate1;
GridViewApdate getGridViewApdate2;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
view =View.inflate(getActivity(), R.layout.fragmenttwo,null);
initView();
initData();
return view;
}
private void initView() {
//初始化数据源
gv_f2s = (GridView)view.findViewById(R.id.gv_f2s);
gv_f2x = (GridView)view.findViewById(R.id.gv_f2x);
gridViewApdate1 = new GridViewApdate(lsitm,getActivity());
gv_f2s.setAdapter(gridViewApdate1);
getGridViewApdate2 = new GridViewApdate(listn,getActivity());
gv_f2x.setAdapter(getGridViewApdate2);
//gv_f2s的点击事件
gv_f2s.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView,
View view, int i, long l)
{
//先把点击的添加到底二个Grid里面
listn.add(lsitm.get(i));
//删除第一个在自己的Grid
lsitm.remove(i);
//刷新两个适配器
gridViewApdate1.setData(lsitm);
getGridViewApdate2.setData(listn);
}
});
gv_f2x.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView,
View view, int i, long l)
{
//先把点击的添加在第一个Grid里面
lsitm.add(listn.get(i));
//删除自己里面的数据
listn.remove(i);
//刷新适配器
gridViewApdate1.setData(lsitm);
getGridViewApdate2.setData(listn);
}
});

}
//从络请求数据
private void initData() {
new AsyncTask<String,String,String>(){
@Override
protected String doInBackground(String... strings)
{
String Ggson =  NetuS.getNetDataGrid("http://result.eolinker.com/iYXEPGn4e9c6dafce6e5cdd23287d2bb136ee7e9194d3e9?uri=channel");
//Gson
Gson Gridg =new Gson();
Gridbean json = Gridg.fromJson(Ggson, Gridbean.class);
lsitm = new ArrayList<String>();
listn = new ArrayList<String>();
for (int i = 0 ; i <json.getChannel().size();i++)
{
lsitm.add(json.getChannel().get(i).getChannel_me());
listn.add(json.getChannel().get(i).getChannel_more());
}
return null;
}

@Override
protected void onPostExecute(String s) {
gridViewApdate1.setData(lsitm);
getGridViewApdate2.setData(listn);
super.onPostExecute(s);
}
}.execute();
}

}
这是Bean包
public class Gridbean  {

private int code;
private List<ChannelBean> channel;

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public List<ChannelBean> getChannel() {
return channel;
}

public void setChannel(List<ChannelBean> channel) {
this.channel = channel;
}

public static class ChannelBean {
/**
* channel_me : 推荐
* channel_more : 财经
*/

private String channel_me;
private String channel_more;

public String getChannel_me() {
return channel_me;
}

public void setChannel_me(String channel_me) {
this.channel_me = channel_me;
}

public String getChannel_more() {
return channel_more;
}

public void setChannel_more(String channel_more) {
this.channel_more = channel_more;
}
}
}
第二个
public class Imagebean {

private List<BannerBean> banner;

public List<BannerBean> getBanner() {
return banner;
}

public void setBanner(List<BannerBean> banner) {
this.banner = banner;
}

public static class BannerBean {

private String desc;
private String image_url;

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public String getImage_url() {
return image_url;
}

public void setImage_url(String image_url) {
this.image_url = image_url;
}
}
}
第三个
public class ListUtil {

/**
* code : 200
* data : [{"content":"习近平举行仪式欢迎加蓬总统访华","id":10000,"image_url":"http://pic32.nipic.com/20130817/9745430_101836881000_2.jpg","title":"今日头条","type":1},{"content":"习近平举行仪式欢迎加蓬总统访华","id":10001,"image_url":"http://pic15.nipic.com/20110630/6322714_105943746342_2.jpg","title":"往期故事","type":2},{"content":"这个市的书记市长双双被约谈 引发震动","id":10002,"image_url":"http://pic48.nipic.com/file/20140916/2531170_195153248000_2.jpg","title":"讨论天下","type":3},{"content":"\u201c乔丹\u201d商标是否侵权 4年官司迎最高法判决 调查","id":10003,"image_url":"http://img.taopic.com/uploads/allimg/140626/240469-1406261S24553.jpg","title":"我的珍爱","type":4},{"content":"黑龙江稻农收入暴涨组团进城买房 有人买十多套","id":10004,"image_url":"http://pic77.nipic.com/file/20150911/21721561_155058651000_2.jpg","title":"我的未来","type":5},{"content":"印尼6.4级地震致97死 专家称当地防震意识差","id":10005,"image_url":"http://img4.duitang.com/uploads/item/201603/18/20160318103156_cziuY.jpeg","title":"东芝","type":6}]
* head_url : http://h.hiphotos.baidu.com/zhidao/pic/item/9d82d158ccbf6c81e718270eb93eb13533fa402c.jpg * name : 思凡
*/

private int code;
private String head_url;
private String name;
private List<DataBean> data;

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getHead_url() {
return head_url;
}

public void setHead_url(String head_url) {
this.head_url = head_url;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<DataBean> getData() {
return data;
}

public void setData(List<DataBean> data) {
this.data = data;
}

public static class DataBean {
/**
* content : 习近平举行仪式欢迎加蓬总统访华
* id : 10000
* image_url : http://pic32.nipic.com/20130817/9745430_101836881000_2.jpg * title : 今日头条
* type : 1
*/

private String content;
private int id;
private String image_url;
private String title;
private int type;

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getImage_url() {
return image_url;
}

public void setImage_url(String image_url) {
this.image_url = image_url;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getType() {
return type;
}

public void setType(int type) {
this.type = type;
}
}
}
下面是
ImageLoader所需要继承的类
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration=new ImageLoaderConfiguration.Builder(this)
.threadPoolSize(5)

.build();
ImageLoader.getInstance().init(configuration);
}
}
下面是第一个Fragentone的布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/vpf"
android:layout_width="match_parent"
android:layout_height="170dp"
>

</android.support.v4.view.ViewPager>
<ListView
android:id="@+id/lv_fone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/vpf"
android:layout_alignParentBottom="true"
></ListView>

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton"
android:checked="false"
android:layout_alignBottom="@+id/vpf"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="65dp"
android:layout_marginEnd="65dp" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton3"
android:checked="false"
android:layout_alignBottom="@+id/vpf"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="63dp"
android:layout_marginStart="63dp" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioButton4"
android:checked="false"
android:layout_above="@+id/lv_fone"
android:layout_centerHorizontal="true" />

</RelativeLayout>
下面是Fragmenttwo的布局
<RelativeLayout 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_f2s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的频道"
android:textSize="24dp"
android:gravity="center_vertical"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
/>
<GridView
android:id="@+id/gv_f2s"
android:layout_below="@+id/tv_f2s"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="4"
>
</GridView>
<TextView
android:layout_below="@+id/gv_f2s"
android:id="@+id/tv_f2x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更多频道"
android:textSize="24dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
/>
<GridView
android:id="@+id/gv_f2x"
android:layout_below="@+id/tv_f2x"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="4"
>

</GridView>
</RelativeLayout>
下面是ListView所要用的外面布局
<RelativeLayout 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_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文字"
android:layout_toRightOf="@+id/iv_list"
android:layout_marginTop="50dp"
/>
<TextView
android:id="@+id/tv_list2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="描述信息"
android:layout_toRightOf="@+id/iv_list"
android:layout_below="@+id/tv_list"
android:layout_marginTop="50dp"
/>
<ImageView
android:id="@+id/iv_list"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@mipmap/ic_launcher"
/>
</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:id="@+id/tv_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="栏目"/>
</LinearLayout>
记得加网络权限和
ImageLoader的地址

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐