2 jc1992cj jc1992cj 于 2014.10.20 08:47 提问

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

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>

请问怎么能得到viewpager_order里的listview?

3个回答

csdngcf114
csdngcf114   2015.05.19 17:20

getactivity().findviewByid

zuo1943
zuo1943   2014.11.24 17:01

view.findViewById

csdngcf114
csdngcf114   2015.05.19 17:20

getactivity().findviewByid

sinat_38626045
sinat_38626045 别人是问如何在activity中获取子Fragment里面的控件啊。。。。。findViewById谁不晓得。。。
3 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片