ViewPager中使用自定义的ListView实例

时间:2022-09-30 08:49:16

这篇内容是上一篇的延续,因为在上一篇的测试ViewPager成功了,才能实现这一篇的和ListView合在一起使用

  效果图如下:

  ViewPager中使用自定义的ListView实例

  不愿意说理论,直接上代码

1.清单文件 activity_main.xml内容如下:

<?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"
android:orientation="vertical" > <android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent" > <android.support.v4.view.PagerTabStrip
android:id="@+id/pagerTabStrip"
android:layout_width="match_parent"
android:layout_height="wrap_content" /> </android.support.v4.view.ViewPager>
</LinearLayout>

2.主类代码MainActivity.java如下

package com.zyf.android.filemanage;

import java.io.File;
import java.util.ArrayList;
import com.zyf.android.adapter.MyListViewAdapter;
import com.zyf.android.adapter.MyPagerAdapter;
import com.zyf.androidfilemanage.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.ListView; public class MainActivity extends Activity { private ArrayList<View> views;
private ArrayList<File> files; //private ArrayList<> @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
views = new ArrayList<View>();
View view0 =getLayoutInflater().inflate(R.layout.vp_phone, null);
View view1 =getLayoutInflater().inflate(R.layout.vp_phone, null);
View view2 =getLayoutInflater().inflate(R.layout.vp_phone, null);
views.add(view0);
views.add(view1);
views.add(view2);
viewPager.setAdapter(new MyPagerAdapter(views)); files = new ArrayList<File>();
files.add(new File("第一个文件"));
files.add(new File("第二个文件"));
files.add(new File("第三个文件"));
files.add(new File("第四个文件"));
ListView listView = (ListView)(view0.findViewById(R.id.listview_phone));
listView.setVisibility(ListView.VISIBLE); listView.setAdapter(new MyListViewAdapter(files,getLayoutInflater()));
}
}

3.ViewPager的适配器MyPagerAdapter.java代码如下:

package com.zyf.android.adapter;

import java.util.ArrayList;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup; public class MyPagerAdapter extends PagerAdapter { private ArrayList<View> views;
private String[] pagerTabStrip={"手机","存储卡","云端"}; public MyPagerAdapter(ArrayList<View> views) {
// TODO Auto-generated constructor stub
this.views =views;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return views == null ? 0 :views.size();
} @Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
} @Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
//super.destroyItem(container, position, object);
container.removeView(views.get(position));
} @Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return pagerTabStrip[position];
} @Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
View view = views.get(position); container.addView(view);
return view;
} }

4.子view对应的ListView的vp_phone.xml代码如下

<?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="wrap_content"
android:orientation="vertical" > <ListView
android:id="@+id/listview_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView> </LinearLayout>

4.ListView的适配器MyListViewAdapter.java代码如下:

package com.zyf.android.adapter;

import java.io.File;
import java.util.ArrayList; import com.zyf.android.util.LogUtil;
import com.zyf.androidfilemanage.R; 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{ private ArrayList<File> files ;
private LayoutInflater layoutInflater; public MyListViewAdapter(ArrayList<File> files, LayoutInflater layoutInflater) {
super();
this.files = files;
this.layoutInflater =layoutInflater;
} @Override
public int getCount() {
// TODO Auto-generated method stub
return files.size();
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return files.get(arg0);
} @Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
} @Override
public View getView(int arg0, View arg1, ViewGroup arg2) { File file =files.get(arg0);
String filename = file.getName();
int icon = R.drawable.icon; HoldView holdView = null;
if (arg1 == null){
arg1 = layoutInflater.inflate(R.layout.item_file, null);
holdView = new HoldView(arg1);
arg1.setTag(holdView);
}else{
holdView = (HoldView)arg1.getTag();
}
holdView.getIv_con().setImageResource(icon);
holdView.getTx_name().setText(filename); return arg1;
} private class HoldView{
ImageView iv_con;
TextView tx_name;
public HoldView(View view) {
iv_con = (ImageView)view.findViewById(R.id.item_file_imageview);
tx_name = (TextView)view.findViewById(R.id.item_file_name);
}
public ImageView getIv_con() {
return iv_con;
}
public TextView getTx_name() {
return tx_name;
}
}
}

6.ListView中的每个条目的资源文件item_file.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" > <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/item_file_imageview"
/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/item_file_imageview"
android:id="@+id/item_file_name"
/>
</RelativeLayout>

总结,整体比较简单,就是ViewPager需要自己写适配器,同时ListView也是自己写的适配器,然后把两者整合到一起(就是从ViewPager中拿到ListView对应的view就可以了)