首页 > 其他 > 详细

listview,fragment结合起来

时间:2015-08-11 07:01:49      阅读:251      评论:0      收藏:0      [点我收藏+]

  这是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结合起来

原文:http://www.cnblogs.com/Sunnor/p/4719687.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!