listview,fragment结合起来
这是csdn上的以个demo,很适合初学者。来源:http://download.csdn.net/detail/endlife99/7274419,侵删。
MainActiviy:
package com.example.viewpagerdemo; import java.util.ArrayList; import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView; public class MainActivity extends FragmentActivity {
private ArrayList<Fragment> list = null;
private ViewPager mViewPager;
private ImageView iv_bottom_line;
private Resources resources;
private TextView zh;
private TextView xw;
private TextView yl;
private TextView ty;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initWidth();
initViewPager();
}
private void initViewPager() {
Fragment zh = MyFragment.newInstance(Contents.ZH);
Fragment xw = MyFragment.newInstance(Contents.XW);
Fragment yl = MyFragment.newInstance(Contents.YL);
Fragment ty = MyFragment.newInstance(Contents.TY);
list = new ArrayList<Fragment>();
list.add(zh);
list.add(xw);
list.add(yl);
list.add(ty);
mViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(),
list));
mViewPager.setCurrentItem(0);
mViewPager.setOnPageChangeListener(new MyViewPagerChangedListener());
}
private void initView() {
mViewPager = (ViewPager) findViewById(R.id.myviewpager);
iv_bottom_line = (ImageView) findViewById(R.id.iv_bottom_line);
zh = (TextView) findViewById(R.id.zh);
xw = (TextView) findViewById(R.id.xw);
yl = (TextView) findViewById(R.id.yl);
ty = (TextView) findViewById(R.id.ty);
zh.setOnClickListener(new MyClickListener(0));
xw.setOnClickListener(new MyClickListener(1));
yl.setOnClickListener(new MyClickListener(2));
ty.setOnClickListener(new MyClickListener(3));
}
private int first = 0;
private int second = 0;
private int third = 0;
private void initWidth() {
int lineWidth = iv_bottom_line.getLayoutParams().width;
Log.d("lineWidth ", lineWidth + "");
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
resources = getResources();
first = width / 4;
second = first * 2;
third = first * 3;
}
private int currPosition = 0;
class MyViewPagerChangedListener implements OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
Log.d("onchanged", "onchanged " + arg0);
TranslateAnimation ta = null;
switch (arg0) {
case 0:
if (currPosition == 1) {
ta = new TranslateAnimation(first, 0, 0, 0);
}
if (currPosition == 2) {
ta = new TranslateAnimation(second, 0, 0, 0);
}
if (currPosition == 3) {
ta = new TranslateAnimation(third, 0, 0, 0);
}
break;
case 1:
if (currPosition == 0) {
ta = new TranslateAnimation(0, first, 0, 0);
}
if (currPosition == 2) {
ta = new TranslateAnimation(second, first, 0, 0);
}
if (currPosition == 3) {
ta = new TranslateAnimation(third, first, 0, 0);
}
break;
case 2:
if (currPosition == 0) {
ta = new TranslateAnimation(0, second, 0, 0);
}
if (currPosition == 1) {
ta = new TranslateAnimation(first, second, 0, 0);
}
if (currPosition == 3) {
ta = new TranslateAnimation(third, second, 0, 0);
}
break;
case 3:
if (currPosition == 0) {
ta = new TranslateAnimation(0, third, 0, 0);
}
if (currPosition == 1) {
ta = new TranslateAnimation(first, third, 0, 0);
}
if (currPosition == 2) {
ta = new TranslateAnimation(second, third, 0, 0);
}
break;
}
currPosition = arg0;
ta.setDuration(300);
ta.setFillAfter(true);
iv_bottom_line.startAnimation(ta);
}
}
class MyClickListener implements OnClickListener {
private int index = 0;
public MyClickListener(int i) {
index = i;
}
@Override
public void onClick(View v) {
mViewPager.setCurrentItem(index);
}
}
}
MyFragment:
package com.example.viewpagerdemo; import java.util.ArrayList;
import java.util.List; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ListView; public class MyFragment extends Fragment{ private String key = null; static MyFragment newInstance(String s){
MyFragment myFragment = new MyFragment();
Bundle bundle = new Bundle();
bundle.putString("key", s);
myFragment.setArguments(bundle);
return myFragment; } @Override
public void onCreate(Bundle savedInstanceState) { Bundle bundle = getArguments();
key = bundle != null? bundle.getString("key") : null;
super.onCreate(savedInstanceState);
} private ListView listView; private MyListViewAdapter adapter; private List<String> list ; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = null;
Button button = null;
if (key == null || key.equals(Contents.ZH)) {
view = inflater.inflate(R.layout.lay1, container, false);
button = (Button) view.findViewById(R.id.confirm);
button.setOnClickListener(new MyClickListener(1));
} if (key != null && key.equals(Contents.XW)) {
view = inflater.inflate(R.layout.lay2,container,false);
button = (Button) view.findViewById(R.id.confirm02);
button.setOnClickListener(new MyClickListener(2));
} if (key != null && key.equals(Contents.YL)) {
view = inflater.inflate(R.layout.lay3,container,false);
} if (key != null && key.equals(Contents.TY)) {
view = inflater.inflate(R.layout.lay4,container,false);
listView = (ListView) view.findViewById(R.id.mylistview);
list = getdata();
adapter = new MyListViewAdapter(getActivity(),list);
listView.setAdapter(adapter); //listView滑动状态判断
listView.setOnScrollListener(new OnScrollListener() { @Override
public void onScrollStateChanged(AbsListView arg0, int arg1) {
// TODO Auto-generated method stub } @Override
public void onScroll(AbsListView arg0, int firstItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub //到达底部
if (firstItem + visibleItemCount == totalItemCount) {
list = getdata();
adapter.notifyDataSetChanged();
}
}
}); } return view;
} private List<String> getdata(){
int size = 0;
if (list != null) {
size = list.size(); }
if (list == null) {
list = new ArrayList<String>();
} for (int i = 0; i < 20; i++) {
list.add("item" + i + size);
}
return list;
} class MyClickListener implements OnClickListener{ int index; public MyClickListener(int i) {
index = i;
} @Override
public void onClick(View v) {
Log.d("onclick", "onclick " + index);
} } }
MyListViewAdapter:
package com.example.viewpagerdemo; import java.util.List; import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; public class MyListViewAdapter extends BaseAdapter { List<String>list; LayoutInflater inflater; FragmentActivity activity; public MyListViewAdapter(FragmentActivity fragmentActivity, List<String> list) {
// TODO Auto-generated constructor stub
this.list = list;
this.activity = fragmentActivity;
inflater = (LayoutInflater) fragmentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
} @Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
} @Override
public View getView(int arg0, View view, ViewGroup arg2) {
// TODO Auto-generated method stub
if (view == null) {
view = inflater.inflate(R.layout.item, null);
} return view;
} class ViewHolder {
TextView tv;
ImageView im;
} }
FragmentAdapter:
package com.example.viewpagerdemo; import java.util.ArrayList; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter; public class FragmentAdapter extends FragmentPagerAdapter{ private ArrayList<Fragment> list; public FragmentAdapter(FragmentManager fm) {
super(fm);
} public FragmentAdapter(FragmentManager fm,
ArrayList<Fragment> list) {
super(fm);
this.list = list;
} @Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return list.get(arg0);
} @Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
} }
Contents:
package com.example.viewpagerdemo; public class Contents { public static final String ZH = "zonghe"; public static final String XW = "xinwen"; public static final String YL = "yule"; public static final String TY = "tiyu"; }
layout文件夹:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#20d5f0"
android:paddingBottom="2dp"
android:paddingTop="2dp" > <TextView android:id="@+id/zh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="综合"
android:textColor="#fff"
android:textSize="16sp" /> <TextView
android:id="@+id/xw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="新闻"
android:textColor="#fff"
android:textSize="16sp" /> <TextView
android:id="@+id/yl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="娱乐"
android:textColor="#fff"
android:textSize="16sp" /> <TextView
android:id="@+id/ty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="体育"
android:textColor="#fff"
android:textSize="16sp" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical"
android:paddingBottom="3dip"
android:background="#20d5f0"
> <ImageView
android:id="@+id/iv_bottom_line"
android:layout_width="40dip"
android:layout_height="2dip"
android:layout_marginLeft="20dip"
android:scaleType="matrix"
android:src="#fff" />
</LinearLayout> <android.support.v4.view.ViewPager
android:id="@+id/myviewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" /> </LinearLayout>
效果图如下:
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/itemImage"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_alignParentLeft="true" />
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="前段时间做了一个与LBS有关的项目"
android:layout_toRightOf="@+id/itemImage"
android:layout_centerVertical="true"
/> </RelativeLayout>
lay1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一页"
android:gravity="center"
/>
<Button
android:id="@+id/confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="确定"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="分享"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="浏览"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="取消"
/> </LinearLayout>
lay02.xml,lay03.xml除了最顶端设置的页码不一样,其他都一直,所以之类就不放上其对应的xml了。
lay04:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:id="@+id/mylistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/> </LinearLayout>
最终做出来的效果图:
listview,fragment结合起来的更多相关文章
- Android之ActionBar、Tabs、Fragment、ViewPager实现标签页切换并缓存页面
感觉 Android 到处都是坑,每个地方都要把人折腾半天. 今天来简单说说 Android之ActionBar.Tabs.Fragment.ViewPager 实现标签页切换并缓存页面 关于他们的介 ...
- Android 类似时间轴的实现
想要实现图片中的的时间轴的效果,设定了三种颜色,但是出来的只有一个黑色,还不是设定好的,而且长度很长的话不能滚动,下面上代码: 布局文件: <LinearLayout xmlns:android ...
- 【原版的:参赛作品】窥秘懒---android打开下拉手势刷新时代
小飒的成长史原创作品:窥视懒人的秘密---android下拉刷新开启手势的新纪元转载请注明出处 **************************************************** ...
- 【原创】窥视懒人的秘密---android下拉刷新开启手势的新纪元
小飒的成长史原创作品:窥视懒人的秘密---android下拉刷新开启手势的新纪元转载请注明出处 **************************************************** ...
- 国外源码精品-Android-PullToRefresh 简介与DEMO导入
转载地址:http://my.oschina.net/cuitongliang/blog/170708 (一)&&http://my.oschina.net/cuitongliang/ ...
- Android tab页制作
全屏启动页 activity_launcher <?xml version="1.0" encoding="utf-8"?> <Relativ ...
- ViewPager和Fragment的结合使用fragment里包含着listview的常见问题
在我们开发的过程中可能会遇到类似需求,我们需要做一个左右滑动的tab导航,这个时候有些朋友可能会想到viewpager和fragment的结合来实现这个功能:当然实现的方法不单单着一种.我们这个随笔讨 ...
- Fragment中添加ListView而不使用ListFragment
最初的构想是,将Fragment和ViewPager结合起来, 然后突发奇想,在第一个Fragment里添加了ListView, 依照网上的建议,extends了ListFragment,接着各种报错 ...
- 向 ViewPager 中添加 包含 ListView 的 Fragment
对与fragment就不说什么了,直接看API手册吧,亲. 向 ViewPager 中添加 包含 ListView 的 Fragment 的过程比较麻烦.他所表现的效果就是新闻客户端的滑动翻页效果. ...
随机推荐
- Snap.svg中transform旋转值的“r+数组”表现形式
Snap.svg中transform的值还可以写为类似以下这种形式: transform:'r'+[100,[50,50]]; 这种写法的意思是,让元素以(50,50)为旋转中心点,然后旋转100度. ...
- XHTML CSS 常见问题和解决方案
原文地址:XHTML CSS 常见问题和解决方案 作为前端开发人员,在日常的页面制作时,不可避免的会碰上这样那样的问题,我挑选了其中的一些进行总结归档,希望对大家会有所帮助: 1.如何定义高度很小的容 ...
- Python每日一练(1):计算文件夹内各个文章中出现次数最多的单词
#coding:utf-8 import os,re path = 'test' files = os.listdir(path) def count_word(words): dic = {} ma ...
- divmod数字处理函数
divmod(a,b)函数 中文说明: divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数 返回结果类型为tuple 参数: a,b可以为数字(包括复数) 版本: 在python2 ...
- 05-C语言运算符
目录: 一.进制转换 二.常量 三.sizeof 四.运算符 五.赋值运算符 六.自增减运算符 七.关系运算符 八.逻辑运算符 九.取址寻址运算符 回到顶部 一.进制转换 1 进制转换是人们利用符号来 ...
- Nexus 5 电信破解问题 CDMA_HDR重启会变回LTE
解决方法是Nexus 5 Field Test Mode -Advanced LTE Settings 关掉LTE 重启就好了 在Android 5.0 下实测成功 如果不行就换一张卡 重新写 重启再 ...
- COB工艺流程及基本要求
工艺流程及基本要求 清洁PCB---滴粘接胶---芯片粘贴---测试---封黑胶加热固化---测试---入库 1.清洁PCB 清洗后的PCB板仍有油污或氧化层等不洁部分用皮擦试帮定位或测试针位对擦拭的 ...
- linux下内存调试工具——valgrind
1.valgrind之memcheck 最常用的工具,用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切对malloc()/free()/new/delete的调用都会被捕获.所以,它 ...
- ASP.NET中IsPostBack的理解
IsPostBack是Page类的一个属性,返回值为一个布尔值. 一般放在Page_Load事件中, 是指是否第一次调用这个页面.当页面是第一次打开时其值为False,若当前页面为一个提交后的页面其值 ...
- xls与csv文件区别?
xls 文件就是Microsoft excel电子表格的文件格式.CSV是最通用的一种文件格式,它可以非常容易地被导入各种PC表格及数据库中. 此文件,一行即为数据表的一行.生成数据表字段用半角逗号隔 ...