使用Java在ViewPager中修改XML视图

时间:2014-04-29 14:16:42

标签: android android-viewpager

在我的Android应用中,我使用ViewPager在4 XML之间导航。以下是使用的代码。

package com.win.kora;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Htpnew extends Activity
{
private ViewPager viewPager;

private LinearLayout page1;
private LinearLayout page2;
private LinearLayout page3;
private LinearLayout page4;
private TextView tv2, tv3, tv4, tv6, tv7, tv8;
private SurfaceView sv;
private Canvas c;
private Paint paint= new Paint(Paint.ANTI_ALIAS_FLAG), paint1= new Paint(Paint.ANTI_ALIAS_FLAG);


@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);



    setContentView(R.layout.htpnew);
    viewPager = (ViewPager) findViewById(R.id.pager);

    viewPager.setAdapter(new MainPageAdapter());

    viewPager.setOnPageChangeListener(new OnPageChangeListener() {

        @Override
        public void onPageSelected(int position)
        {
            Toast.makeText(Htpnew.this, "page " + (position + 1), Toast.LENGTH_SHORT).show();               
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
        {

        }

        @Override
        public void onPageScrollStateChanged(int state)
        {

        }
    });     

}

class MainPageAdapter extends PagerAdapter
{

    @Override
    public int getCount()
    {
        return 4;
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position)
    {
        View page = null;
        switch (position)
        {
        case 0:
            if (page1 == null)
            {
                page1 = (LinearLayout) LayoutInflater.from(Htpnew.this).inflate(R.layout.page_one, null);

            }
            page = page1;
            break;
        case 1:
            if (page2 == null)
            {
                page2 = (LinearLayout) LayoutInflater.from(Htpnew.this).inflate(R.layout.page_two, null);
            }
            page = page2;
            break;
        case 2:
            if (page3 == null)
            {
                page3 = (LinearLayout) LayoutInflater.from(Htpnew.this).inflate(R.layout.page_three, null);

            }
            page = page3;
            break;
        default:
            if (page4 == null)
            {
                page4 = (LinearLayout) LayoutInflater.from(Htpnew.this).inflate(R.layout.page_four, null);
            }
            page = page4;
            break;
        }

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

        return page;
    }

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

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


}

}

但是,我想使用java对这些XML进行更改,例如更改textview的背景颜色等 - 例如,
tv8=(TextView) findViewById(R.id.tv8); GradientDrawable gd = (GradientDrawable) tv8.getBackground(); gd.setColor(0xFFFF0000);

即使有一个表面视图,也必须根据用户输入完成一些绘图。所有4 XML都需要进行此类更改。这个代码必须放在哪里?

1 个答案:

答案 0 :(得分:1)

您应该放置代码以更改适配器内的背景(或修改视图的任何部分)。要查找特定的TextView,您可以使用

case 2:
        if (page3 == null)
        {
            page3 = (LinearLayout)LayoutInflater.from(Htpnew.this).inflate(R.layout.page_three, null);

        }
        TextView tv = (TextView)page3.findViewById(R.id.tv8);
        // modify text view here
        page = page3;
        break;