一个ViewPager内的android变化的TextViewViewPager、android、TextView

2023-09-05 10:27:33 作者:霏琅咫天涯

我不能内ViewPager一个TextView来的setText()。 这里是我的主要活动类。我想更新由ViewPager做布局的刷卡里面一个TextView。

I'm not able to setText() of a TextView inside a ViewPager. here is my main activity class. I want to update a textView inside a swipe of layouts made by ViewPager.

public class MainActivity extends Activity {

    ViewPager myPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyPagerAdapter adapter = new MyPagerAdapter();
         myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
         myPager.setAdapter(adapter);
         myPager.setCurrentItem(3);


    }

    @Override
    protected void onResume() {
        super.onResume();

         // this code causes the app to crash, because baseLayout is null
        View baseLayout = myPager.findViewWithTag(R.layout.hoteladdress);
        //TextView bubu = (TextView) baseLayout.findViewById(R.id.bubu);
        //bubu.setText("TEST");

    }




    private class MyPagerAdapter extends PagerAdapter {

        public int getCount() {
            return 3;
        }

        public Object instantiateItem(View collection, int position) {

            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);




            int resId = 0;
            switch (position) {
            case 0: 
                resId = showHotelContact();             
                break;
            case 1:
                resId = showHotelAddress();         
                break;              
            case 2:     
                resId = showHotelMap();             
                break;      
            }



            View view = (View) inflater.inflate(resId, null);

            view.setTag(resId);

            ((ViewPager) collection).addView(view, 0);
            return view;
        }



     }   
    public int showHotelMap()
    {
        int resId;
        resId = R.layout.hotelmap;
        return resId;
    }
    public int showHotelAddress()
    {
        int resId;
        resId = R.layout.hoteladdress;
        return resId;
    }
    public int showHotelContact()
    {
        int resId;
        resId = R.layout.hotelcontact;
        return resId;
    }


}

请找到在onResume方法我的评论线,说明这个问题。

Please find my commented lines in the onResume method which explain the problem.

推荐答案

这是测试工作。

我已经实现了3次作为TextViews但也可以是任何布局,那么你就必须作出相应的修改。

I have implemented the 3 views as TextViews but could be any layout, then you just have to make changes accordingly.

public class MainActivity extends Activity {

    ViewPager myPager;
    MyPagerAdapter adapter;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = new MyPagerAdapter(getLayoutInflater());
        myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
        myPager.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    @Override protected void onResume() {
        super.onResume();

        TextView hotelAddressView = adapter
                .getViewAtPosition(MyPagerAdapter.POSITION_ADDRESS);
        hotelAddressView.setText("modified");

    }

    private class MyPagerAdapter extends PagerAdapter {

        public static final int POSITION_MAP = 0;
        public static final int POSITION_ADDRESS = 1;
        public static final int POSITION_CONTACT = 2;

        private TextView hotelMapView = null;
        private TextView hotelAddressView = null;
        private TextView hotelContactView = null;

        public MyPagerAdapter(LayoutInflater inflater) {

            hotelMapView = (TextView) inflater.inflate(R.layout.hotelmap, null);
            hotelAddressView = (TextView) inflater.inflate(
                    R.layout.hoteladdress, null);
            hotelContactView = (TextView) inflater.inflate(
                    R.layout.hotelcontact, null);
        }

        public int getCount() {
            return 3;
        }

        public TextView getViewAtPosition(int position) {
            Log.d("Main", "getViewAtPosition " + position);
            TextView view = null;
            switch (position) {
            case POSITION_MAP:
                view = hotelMapView;
                break;
            case POSITION_ADDRESS:
                view = hotelAddressView;
                break;
            case POSITION_CONTACT:
                view = hotelContactView;
                break;
            }

            return view;
        }

        public Object instantiateItem(ViewGroup collection, int position) {

            View view = getViewAtPosition(position);

            ((ViewPager) collection).addView(view, 0);

            return view;
        }

        @Override public void destroyItem(ViewGroup collection, int position,
                Object view) {
            ((ViewPager) collection).removeView((TextView) view);
        }

        @Override public boolean isViewFromObject(View view, Object object) {
            return view == ((TextView) object);
        }

    }
}
 
精彩推荐
图片推荐