ListView与ViewPager组合(一)

listview与viewpager组合常用的有两种方式,第一种为listview添加header,我们把viewpager设置为listview的header便可以实现,第二种是把viewpager设置为listview的第一个选项。两种方式实现的效果是一样的,但是第一种方式结构比较清晰,在后续我们设置监听的时候也比较简单,个人不推荐使用第二种方式。这里我们用第一种方式,下一篇我们再介绍第二种。
实现效果我就不截图了,就是viewpager能跟着listview上下滑动,而viewpager可以左右滑动。
onCreate方法

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();
        initView();
        initData();

    }

第一步:加载布局

  private void initView() {
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.lv_main);
        View header = View.inflate(context, R.layout.listview_header, null);
        viewPager = (ViewPager) header.findViewById(R.id.viewpager_main);
        listView.addHeaderView(header);//在setAdapter前
    }

第二步:加载数据并设置adapter

  private void initData() {
        String[] viewPagerData = new String[]{"第一页", "第二页", "第三页", "第四页", "第五页"};
        String[] listViewData = new String[15];
        for (int i = 0; i < listViewData.length; i++) {
            listViewData[i] = "item" + i;
        }
        listViewAdapter = new ListViewAdapter(context, listViewData);
        viewPagerAdapter = new ViewPagerAdapter(context, viewPagerData);
        listView.setAdapter(listViewAdapter);
        viewPager.setAdapter(viewPagerAdapter);
    }

好了,实现了这两步,基本就大功告成了。下面是布局文件,比较简单,也就贴上来了
activity_main.xml

<RelativeLayout 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:background="@color/main_bg"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/lv_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</RelativeLayout>

listview_header.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_main"
        android:layout_width="match_parent"
        android:layout_height="300dp"></android.support.v4.view.ViewPager>
</LinearLayout>

viewpager_item.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">

    <TextView
        android:id="@+id/tv_viewpager_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="24sp" />
</LinearLayout>

listview_item.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">

    <TextView
        android:id="@+id/tv_listview_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="30dp"
        android:textColor="@color/black"
        android:textSize="20sp" />
</LinearLayout>

下篇我们将接着介绍第二种方式,viewpager设置为listview的第一个选项。

ListView与ViewPager组合(二)

源码下载

发布了35 篇原创文章 · 获赞 8 · 访问量 2万+
展开阅读全文

activity里有一个viewpager,里面有两个view,如何得到view里面的控件

10-20

activity里有一个viewpager,里面有两个view,如何得到view里面的控件,直接用findViewById得不到。 相关代码如下: public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ViewPager vp=(ViewPager) findViewById(R.id.vp); //viewpager //初始化views数据 List<View>views=new ArrayList<View>(); LayoutInflater inflater=LayoutInflater.from(this); views.add(inflater.inflate(R.layout.viewpager_menu, null)); views.add(inflater.inflate(R.layout.viewpager_order, null)); List<String>titles=new ArrayList<String>(); titles.add("菜单界面"); titles.add("订单界面"); EasyEatAdapter adapter=new EasyEatAdapter(views,titles); vp.setAdapter(adapter); //listView ListView lv=(ListView) findViewById(R.id.lv_order); List<Order>data=new ArrayList<Order>(); data.add(new Order(R.drawable.img, "张三1", "18812345678", 3, "18:20", "")); data.add(new Order(R.drawable.img, "张三2", "18862280030", 3, "18:20", "")); data.add(new Order(R.drawable.img, "张三3", "18862280030", 3, "18:20", "")); data.add(new Order(R.drawable.img, "张三4", "18862280030", 3, "18:20", "")); data.add(new Order(R.drawable.img, "张三5", "18812345678", 3, "18:20", "")); data.add(new Order(R.drawable.img, "张三6", "18812345678", 3, "18:20", "")); data.add(new Order(R.drawable.img, "张三7", "18812345678", 3, "18:20", "")); data.add(new Order(R.drawable.img, "张三8", "18812345678", 3, "18:20", "")); OrderAdapter adapter2=new OrderAdapter(data); lv.setAdapter(adapter2); } //viewpager class EasyEatAdapter extends PagerAdapter{ private List<View>views; private List<String>titles; public EasyEatAdapter (List<View>views,List<String>titles) { this.views=views; this.titles=titles; } //得到标题 @Override public CharSequence getPageTitle(int position) { return titles.get(position); } //得到数量 @Override public int getCount() { return views.size(); } //滑动后页面 @Override public Object instantiateItem(ViewGroup container, int position) { View view=views.get(position); container.addView(view); return view; } //销毁页面 @Override public void destroyItem(ViewGroup container, int position, Object object) { View view=views.get(position); container.removeView(view); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } } //listView class OrderAdapter extends BaseAdapter{ private List<Order>data; public OrderAdapter(List<Order>data){ this.data=data; } //说明listview有多少个条目 @Override public int getCount() { return data.size(); } //得到指定position 的条目 @Override public Object getItem(int position) { return data.get(position); } //得到条目的ID @Override public long getItemId(int position) { return position; } //说明每个条目的布局 //convertView:缓存的条目 //parent:ListView //返回值作为ListView的一个条目 @Override public View getView(int position, View convertView, ViewGroup parent) { Order order=data.get(position); if(convertView==null){ LayoutInflater inflater=LayoutInflater.from(MainActivity.this); convertView=inflater.inflate(R.layout.listview_item_order, null); } TextView tvName=(TextView) findViewById(R.id.tv_name); TextView tvNumber=(TextView) findViewById(R.id.tv_number); TextView tvPhone=(TextView) findViewById(R.id.tv_phone); TextView tvTime=(TextView) findViewById(R.id.tv_time); TextView tvId=(TextView) findViewById(R.id.tv_id); TextView tvRemark=(TextView) findViewById(R.id.tv_remark); ImageView ivPic=(ImageView) findViewById(R.id.imageView1); //设置数据 tvName.setText(order.getName()); tvNumber.setText(order.getNumber()); tvPhone.setText(order.getPhone()); tvTime.setText(order.getTime().toString()); tvId.setText(order.getId()); tvRemark.setText(order.getRemarkString()); ivPic.setImageResource(order.getId()); return convertView; } } } viewpager_orde里的代码: <?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" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="35sp" android:text="订单界面" /> <ListView android:id="@+id/lv_order" android:layout_margin="4dp" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout> 请问怎么能得到viewpager_order里的listview? 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览