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>

  最终做出来的效果图:

时间: 2024-08-06 07:37:07

listview,fragment结合起来的相关文章

listview+Fragment特效Demo

左边List点击之后对应右边的Fragment! 下载地址:http://www.devstore.cn/code/info/734.html 运行截图:

【原创:参赛作品】窥视懒人的秘密---android下拉刷新开启手势的新纪元

小飒的成长史原创作品:窥视懒人的秘密---android下拉刷新开启手势的新纪元转载请注明出处 *****************************************************************        前言:窥视懒人那些不为人知的秘密 ***************************************************************** 作为一个程序员,哪有不勤奋的道理,当我们都在为技术奋不顾身的时候.偏偏懒人创造了世界. 有的

Android 类似时间轴的实现

想要实现图片中的的时间轴的效果,设定了三种颜色,但是出来的只有一个黑色,还不是设定好的,而且长度很长的话不能滚动,下面上代码: 布局文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android

【原版的:参赛作品】窥秘懒---android打开下拉手势刷新时代

小飒的成长史原创作品:窥视懒人的秘密---android下拉刷新开启手势的新纪元转载请注明出处 *****************************************************************        前言:窥视懒人那些不为人知的秘密 ***************************************************************** 作为一个程序猿,哪有不勤奋的道理.当我们都在为技术奋不顾身的时候.偏偏懒人创造了世界. 有的

国外源码精品-Android-PullToRefresh 简介与DEMO导入

转载地址:http://my.oschina.net/cuitongliang/blog/170708 (一)&&http://my.oschina.net/cuitongliang/blog/170737 (二)PLEASE NOTE, THIS PROJECT IS NO LONGER BEING MAINTAINED 一.介绍 Pull To Refresh Views for Android This project aims to provide a reusable Pull

Android——ViewPager+Fragment+ListView之间

Android--ViewPager+Fragment+ListView之间 <span style="font-size:18px;">package com.example.jreduch05; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.A

在Fragment中加一个嵌套了ListView的ScrollView(一)

首先介绍一下这个程序的功能: 1.顶部有两个可以切换Fragment的Button 2.在其中一个Fragment中里有个ScrollView,ScrollView中有ViewFlipper,ListView.(另一个Fragment中就随意了) 随着listView的滚动,ViewFlipper中的内容也会滚动. 3.两个布局(主布局,一个Fragment的布局(另一个没写,其实都一样)),一个Fragment,一个主Activity,重写ListView(不重写的话,不会随着ViewFlip

Fragment中添加ListView而不使用ListFragment

最初的构想是,将Fragment和ViewPager结合起来, 然后突发奇想,在第一个Fragment里添加了ListView, 依照网上的建议,extends了ListFragment,接着各种报错. 仔细看了下,原来是MainActivity这里: 1 //构造适配器 2 List<Fragment> fragments=new ArrayList<Fragment>(); 3 fragments.add(new Fragment1()); 4 fragments.add(ne

心情日记app总结 数据存储+服务+广播+listview+布局+fragment+intent+imagebutton+tabactivity+美工

---恢复内容开始--- 结果截图如下: 第一张图是程序主界面,主要是显示记事列表的一些个事件.旁边的侧拉框是自己登陆用的.可以设置密码.可以查看反馈与关于等信息. 点击第一张图片下方的图标,会显示不同的内容,分别如下: 这四张图分别是添加心情,统计心情记录,设置闹铃,开启音乐.分别对应  添加心情模块.统计心情记录模块.铃声提醒模块.音乐播放器模块 按我的想法,此款app主要用来记录心情,而且可以边播放音乐边写日志.而且,还可以通过闹铃提醒我们写日志.而且,还可以统计我们最近的心情状态,为及时